summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/file_finder/index_spec.js
blob: bb0b12d205abe44820d1c19c8a712bfb901988e6 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { GlLoadingIcon } from '@gitlab/ui';
import { nextTick } from 'vue';
import VirtualList from 'vue-virtual-scroll-list';
import { Mousetrap } from '~/lib/mousetrap';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import { file } from 'jest/ide/helpers';
import FindFileComponent from '~/vue_shared/components/file_finder/index.vue';
import FileFinderItem from '~/vue_shared/components/file_finder/item.vue';
import { setHTMLFixture } from 'helpers/fixtures';

describe('File finder item spec', () => {
  let wrapper;

  const TEST_FILES = [
    {
      ...file('index.js'),
      path: 'index.js',
      type: 'blob',
      url: '/index.jsurl',
    },
    {
      ...file('component.js'),
      path: 'component.js',
      type: 'blob',
    },
  ];

  function createComponent(props) {
    wrapper = mountExtended(FindFileComponent, {
      attachTo: document.body,
      propsData: {
        files: TEST_FILES,
        visible: true,
        loading: false,
        ...props,
      },
    });
  }

  const findAllFileFinderItems = () => wrapper.findAllComponents(FileFinderItem);
  const findSearchInput = () => wrapper.findByTestId('search-input');
  const enterSearchText = (text) => findSearchInput().setValue(text);
  const clearSearch = () => wrapper.findByTestId('clear-search-input').vm.$emit('click');

  describe('with entries', () => {
    beforeEach(() => {
      createComponent({
        files: TEST_FILES,
      });

      return nextTick();
    });

    it('renders list of blobs', () => {
      expect(wrapper.text()).toContain('index.js');
      expect(wrapper.text()).toContain('component.js');
      expect(wrapper.text()).not.toContain('folder');
    });

    it('filters entries', async () => {
      await enterSearchText('index');

      expect(wrapper.text()).toContain('index.js');
      expect(wrapper.text()).not.toContain('component.js');
    });

    it('shows clear button when searchText is not empty', async () => {
      await enterSearchText('index');

      expect(wrapper.find('.dropdown-input').classes()).toContain('has-value');
      expect(wrapper.find('.dropdown-input-search').classes()).toContain('hidden');
    });

    it('clear button resets searchText', async () => {
      await enterSearchText('index');
      expect(findSearchInput().element.value).toBe('index');

      await clearSearch();

      expect(findSearchInput().element.value).toBe('');
    });

    it('clear button focuses search input', async () => {
      expect(findSearchInput().element).not.toBe(document.activeElement);

      await enterSearchText('index');
      await clearSearch();

      expect(findSearchInput().element).toBe(document.activeElement);
    });

    describe('listShowCount', () => {
      it('returns 1 when no filtered entries exist', async () => {
        await enterSearchText('testing 123');

        expect(wrapper.findComponent(VirtualList).props('remain')).toBe(1);
      });

      it('returns entries length when not filtered', () => {
        expect(wrapper.findComponent(VirtualList).props('remain')).toBe(2);
      });
    });

    describe('filtering', () => {
      it('renders only items that match the filter', async () => {
        await enterSearchText('index');

        expect(findAllFileFinderItems()).toHaveLength(1);
      });
    });

    describe('DOM Performance', () => {
      it('renders less DOM nodes if not visible by utilizing v-if', async () => {
        createComponent({ visible: false });

        await nextTick();

        expect(wrapper.findByTestId('overlay').exists()).toBe(false);
      });
    });

    describe('watches', () => {
      describe('searchText', () => {
        it('resets focusedIndex when updated', async () => {
          await enterSearchText('index');
          await nextTick();

          expect(findAllFileFinderItems().at(0).props('focused')).toBe(true);
        });
      });

      describe('visible', () => {
        it('resets searchText when changed to false', async () => {
          await enterSearchText('test');
          await wrapper.setProps({ visible: false });
          // need to set it back to true, so the component's content renders
          await wrapper.setProps({ visible: true });

          expect(findSearchInput().element.value).toBe('');
        });
      });
    });

    describe('openFile', () => {
      it('closes file finder', () => {
        expect(wrapper.emitted('toggle')).toBeUndefined();

        findSearchInput().trigger('keyup.enter');

        expect(wrapper.emitted('toggle')).toHaveLength(1);
      });

      it('pushes to router', () => {
        expect(wrapper.emitted('click')).toBeUndefined();

        findSearchInput().trigger('keyup.enter');

        expect(wrapper.emitted('click')).toHaveLength(1);
      });
    });

    describe('onKeyup', () => {
      it('opens file on enter key', async () => {
        expect(wrapper.emitted('click')).toBeUndefined();

        await findSearchInput().trigger('keyup.enter');

        expect(wrapper.emitted('click')[0][0]).toBe(TEST_FILES[0]);
      });

      it('closes file finder on esc key', async () => {
        expect(wrapper.emitted('toggle')).toBeUndefined();

        await findSearchInput().trigger('keyup.esc');

        expect(wrapper.emitted('toggle')[0][0]).toBe(false);
      });
    });

    describe('onKeyDown', () => {
      describe('up key', () => {
        it('resets to last index when at top', async () => {
          expect(findAllFileFinderItems().at(0).props('focused')).toBe(true);

          await findSearchInput().trigger('keydown.up');

          expect(findAllFileFinderItems().at(-1).props('focused')).toBe(true);
        });

        it('minus 1 from focusedIndex', async () => {
          await findSearchInput().trigger('keydown.up');
          await findSearchInput().trigger('keydown.up');

          expect(findAllFileFinderItems().at(0).props('focused')).toBe(true);
        });
      });

      describe('down key', () => {
        it('resets to first index when at bottom', async () => {
          await findSearchInput().trigger('keydown.down');
          expect(findAllFileFinderItems().at(-1).props('focused')).toBe(true);

          await findSearchInput().trigger('keydown.down');
          expect(findAllFileFinderItems().at(0).props('focused')).toBe(true);
        });

        it('adds 1 to focusedIndex', async () => {
          expect(findAllFileFinderItems().at(0).props('focused')).toBe(true);

          await findSearchInput().trigger('keydown.down');

          expect(findAllFileFinderItems().at(1).props('focused')).toBe(true);
        });
      });
    });
  });

  describe('without entries', () => {
    it('renders loading text when loading', () => {
      createComponent({ loading: true, files: [] });

      expect(wrapper.findComponent(GlLoadingIcon).exists()).toBe(true);
    });

    it('renders no files text', () => {
      createComponent({ files: [] });

      expect(wrapper.text()).toContain('No files found.');
    });
  });

  describe('keyboard shortcuts', () => {
    beforeEach(async () => {
      createComponent();
      await nextTick();
    });

    it('calls toggle on `t` key press', () => {
      expect(wrapper.emitted('toggle')).toBeUndefined();

      Mousetrap.trigger('t');

      expect(wrapper.emitted('toggle')).not.toBeUndefined();
    });

    it('calls toggle on `mod+p` key press', () => {
      expect(wrapper.emitted('toggle')).toBeUndefined();

      Mousetrap.trigger('mod+p');

      expect(wrapper.emitted('toggle')).not.toBeUndefined();
    });

    it('always allows `mod+p` to trigger toggle', () => {
      expect(
        Mousetrap.prototype.stopCallback(
          null,
          wrapper.find('.dropdown-input-field').element,
          'mod+p',
        ),
      ).toBe(false);
    });

    it('onlys handles `t` when focused in input-field', () => {
      expect(
        Mousetrap.prototype.stopCallback(null, wrapper.find('.dropdown-input-field').element, 't'),
      ).toBe(true);
    });

    it('stops callback in monaco editor', () => {
      setHTMLFixture('<div class="inputarea"></div>');

      expect(
        Mousetrap.prototype.stopCallback(null, document.querySelector('.inputarea'), 't'),
      ).toBe(true);
    });
  });
});