summaryrefslogtreecommitdiff
path: root/spec/frontend/projects/commits/components/author_select_spec.js
blob: 9a8f7ff75823ea03e8a3b08d38e567291b1e0061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { GlDropdown, GlDropdownSectionHeader, GlSearchBoxByType, GlDropdownItem } from '@gitlab/ui';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import * as urlUtility from '~/lib/utils/url_utility';
import AuthorSelect from '~/projects/commits/components/author_select.vue';
import { createStore } from '~/projects/commits/store';

const localVue = createLocalVue();
localVue.use(Vuex);

const commitsPath = 'author/search/url';
const currentAuthor = 'lorem';
const authors = [
  {
    id: 1,
    name: currentAuthor,
    username: 'ipsum',
    avatar_url: 'some/url',
  },
  {
    id: 2,
    name: 'lorem2',
    username: 'ipsum2',
    avatar_url: 'some/url/2',
  },
];

describe('Author Select', () => {
  let store;
  let wrapper;

  const createComponent = () => {
    setFixtures(`
      <div class="js-project-commits-show">
        <input id="commits-search" type="text" />
        <div id="commits-list"></div>
      </div>
    `);

    wrapper = shallowMount(AuthorSelect, {
      localVue,
      store: new Vuex.Store(store),
      propsData: {
        projectCommitsEl: document.querySelector('.js-project-commits-show'),
      },
    });
  };

  beforeEach(() => {
    store = createStore();
    store.actions.fetchAuthors = jest.fn();

    createComponent();
  });

  afterEach(() => {
    wrapper.destroy();
  });

  const findDropdownContainer = () => wrapper.find({ ref: 'dropdownContainer' });
  const findDropdown = () => wrapper.find(GlDropdown);
  const findDropdownHeader = () => wrapper.find(GlDropdownSectionHeader);
  const findSearchBox = () => wrapper.find(GlSearchBoxByType);
  const findDropdownItems = () => wrapper.findAll(GlDropdownItem);

  describe('user is searching via "filter by commit message"', () => {
    it('disables dropdown container', () => {
      wrapper.setData({ hasSearchParam: true });

      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdownContainer().attributes('disabled')).toBeFalsy();
      });
    });

    it('has correct tooltip message', () => {
      wrapper.setData({ hasSearchParam: true });

      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdownContainer().attributes('title')).toBe(
          'Searching by both author and message is currently not supported.',
        );
      });
    });

    it('disables dropdown', () => {
      wrapper.setData({ hasSearchParam: false });

      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdown().attributes('disabled')).toBeFalsy();
      });
    });

    it('hasSearchParam if user types a truthy string', () => {
      wrapper.vm.setSearchParam('false');

      expect(wrapper.vm.hasSearchParam).toBeTruthy();
    });
  });

  describe('dropdown', () => {
    it('displays correct default text', () => {
      expect(findDropdown().attributes('text')).toBe('Author');
    });

    it('displays the current selected author', () => {
      wrapper.setData({ currentAuthor });

      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdown().attributes('text')).toBe(currentAuthor);
      });
    });

    it('displays correct header text', () => {
      expect(findDropdownHeader().text()).toBe('Search by author');
    });

    it('does not have popover text by default', () => {
      expect(wrapper.attributes('title')).not.toExist();
    });
  });

  describe('dropdown search box', () => {
    it('has correct placeholder', () => {
      expect(findSearchBox().attributes('placeholder')).toBe('Search');
    });

    it('fetch authors on input change', () => {
      const authorName = 'lorem';
      findSearchBox().vm.$emit('input', authorName);

      expect(store.actions.fetchAuthors).toHaveBeenCalledWith(expect.anything(), authorName);
    });
  });

  describe('dropdown list', () => {
    beforeEach(() => {
      store.state.commitsAuthors = authors;
      store.state.commitsPath = commitsPath;
    });

    it('has a "Any Author" as the first list item', () => {
      expect(findDropdownItems().at(0).text()).toBe('Any Author');
    });

    it('displays the project authors', () => {
      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdownItems()).toHaveLength(authors.length + 1);
      });
    });

    it('has the correct props', () => {
      const [{ avatar_url, username }] = authors;
      const result = {
        avatarUrl: avatar_url,
        secondaryText: username,
        isChecked: true,
      };

      wrapper.setData({ currentAuthor });

      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdownItems().at(1).props()).toEqual(expect.objectContaining(result));
      });
    });

    it("display the author's name", () => {
      return wrapper.vm.$nextTick().then(() => {
        expect(findDropdownItems().at(1).text()).toBe(currentAuthor);
      });
    });

    it('passes selected author to redirectPath', () => {
      const redirectToUrl = `${commitsPath}?author=${currentAuthor}`;
      const spy = jest.spyOn(urlUtility, 'redirectTo');
      spy.mockImplementation(() => 'mock');

      findDropdownItems().at(1).vm.$emit('click');

      expect(spy).toHaveBeenCalledWith(redirectToUrl);
    });

    it('does not pass any author to redirectPath', () => {
      const redirectToUrl = commitsPath;
      const spy = jest.spyOn(urlUtility, 'redirectTo');
      spy.mockImplementation();

      findDropdownItems().at(0).vm.$emit('click');
      expect(spy).toHaveBeenCalledWith(redirectToUrl);
    });
  });
});