Welcome to mirror list, hosted at ThFree Co, Russian Federation.

modal_spec.js « new_dropdown « components « ide « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c6f9fd0c4eafcef9b5867cbd2eda05591fed4524 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import { GlButton, GlModal } from '@gitlab/ui';
import { nextTick } from 'vue';
import { createAlert } from '~/flash';
import Modal from '~/ide/components/new_dropdown/modal.vue';
import { createStore } from '~/ide/stores';
import { stubComponent } from 'helpers/stub_component';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { createEntriesFromPaths } from '../../helpers';

jest.mock('~/flash');

const NEW_NAME = 'babar';

describe('new file modal component', () => {
  const showModal = jest.fn();
  const toggleModal = jest.fn();

  let store;
  let wrapper;

  const findForm = () => wrapper.findByTestId('file-name-form');
  const findGlModal = () => wrapper.findComponent(GlModal);
  const findInput = () => wrapper.findByTestId('file-name-field');
  const findTemplateButtons = () => wrapper.findAllComponents(GlButton);
  const findTemplateButtonsModel = () =>
    findTemplateButtons().wrappers.map((x) => ({
      text: x.text(),
      variant: x.props('variant'),
      category: x.props('category'),
    }));

  const open = (type, path) => {
    // TODO: This component can not be passed props
    // We have to interact with the open() method?
    wrapper.vm.open(type, path);
  };
  const triggerSubmitForm = () => {
    findForm().trigger('submit');
  };
  const triggerSubmitModal = () => {
    findGlModal().vm.$emit('primary');
  };
  const triggerCancel = () => {
    findGlModal().vm.$emit('cancel');
  };

  const mountComponent = () => {
    const GlModalStub = stubComponent(GlModal);
    jest.spyOn(GlModalStub.methods, 'show').mockImplementation(showModal);
    jest.spyOn(GlModalStub.methods, 'toggle').mockImplementation(toggleModal);

    wrapper = shallowMountExtended(Modal, {
      store,
      stubs: {
        GlModal: GlModalStub,
      },
      // We need to attach to document for "focus" to work
      attachTo: document.body,
    });
  };

  beforeEach(() => {
    store = createStore();

    Object.assign(
      store.state.entries,
      createEntriesFromPaths([
        'README.md',
        'src',
        'src/deleted.js',
        'src/parent_dir',
        'src/parent_dir/foo.js',
      ]),
    );
    Object.assign(store.state.entries['src/deleted.js'], { deleted: true });

    jest.spyOn(store, 'dispatch').mockImplementation();
  });

  afterEach(() => {
    store = null;
    wrapper.destroy();
    document.body.innerHTML = '';
  });

  describe('default', () => {
    beforeEach(async () => {
      mountComponent();

      // Not necessarily needed, but used to ensure that nothing extra is happening after the tick
      await nextTick();
    });

    it('renders modal', () => {
      expect(findGlModal().props()).toMatchObject({
        actionCancel: {
          attributes: [{ variant: 'default' }],
          text: 'Cancel',
        },
        actionPrimary: {
          attributes: [{ variant: 'confirm' }],
          text: 'Create file',
        },
        actionSecondary: null,
        size: 'lg',
        modalId: 'ide-new-entry',
        title: 'Create new file',
      });
    });

    it('renders name label', () => {
      expect(wrapper.find('label').text()).toBe('Name');
    });

    it('renders template buttons', () => {
      const actual = findTemplateButtonsModel();

      expect(actual.length).toBeGreaterThan(0);
      expect(actual).toEqual(
        store.getters['fileTemplates/templateTypes'].map((template) => ({
          category: 'secondary',
          text: template.name,
          variant: 'dashed',
        })),
      );
    });

    // These negative ".not.toHaveBeenCalled" assertions complement the positive "toHaveBeenCalled"
    // assertions that show up later in this spec. Without these, we're not guaranteed the "act"
    // actually caused the change in behavior.
    it('does not dispatch actions by default', () => {
      expect(store.dispatch).not.toHaveBeenCalled();
    });

    it('does not trigger modal by default', () => {
      expect(showModal).not.toHaveBeenCalled();
      expect(toggleModal).not.toHaveBeenCalled();
    });

    it('does not focus input by default', () => {
      expect(document.activeElement).toBe(document.body);
    });
  });

  describe.each`
    entryType | path         | modalTitle                | btnTitle              | showsFileTemplates | inputValue    | inputPlaceholder
    ${'tree'} | ${''}        | ${'Create new directory'} | ${'Create directory'} | ${false}           | ${''}         | ${'dir/'}
    ${'blob'} | ${''}        | ${'Create new file'}      | ${'Create file'}      | ${true}            | ${''}         | ${'dir/file_name'}
    ${'blob'} | ${'foo/bar'} | ${'Create new file'}      | ${'Create file'}      | ${true}            | ${'foo/bar/'} | ${'dir/file_name'}
  `(
    'when opened as $entryType with path "$path"',
    ({
      entryType,
      path,
      modalTitle,
      btnTitle,
      showsFileTemplates,
      inputValue,
      inputPlaceholder,
    }) => {
      beforeEach(async () => {
        mountComponent();

        open(entryType, path);

        await nextTick();
      });

      it('sets modal props', () => {
        expect(findGlModal().props()).toMatchObject({
          title: modalTitle,
          actionPrimary: {
            attributes: [{ variant: 'confirm' }],
            text: btnTitle,
          },
        });
      });

      it('sets input attributes', () => {
        expect(findInput().element.value).toBe(inputValue);
        expect(findInput().attributes('placeholder')).toBe(inputPlaceholder);
      });

      it(`shows file templates: ${showsFileTemplates}`, () => {
        const actual = findTemplateButtonsModel().length > 0;

        expect(actual).toBe(showsFileTemplates);
      });

      it('shows modal', () => {
        expect(showModal).toHaveBeenCalled();
      });

      it('focus on input', () => {
        expect(document.activeElement).toBe(findInput().element);
      });

      it('resets when canceled', async () => {
        triggerCancel();

        await nextTick();

        // Resets input value
        expect(findInput().element.value).toBe('');
        // Resets to blob mode
        expect(findGlModal().props('title')).toBe('Create new file');
      });
    },
  );

  describe.each`
    modalType | name             | expectedName
    ${'blob'} | ${'foo/bar.js'}  | ${'foo/bar.js'}
    ${'blob'} | ${'foo /bar.js'} | ${'foo/bar.js'}
    ${'tree'} | ${'foo/dir'}     | ${'foo/dir'}
    ${'tree'} | ${'foo /dir'}    | ${'foo/dir'}
  `('when submitting as $modalType with "$name"', ({ modalType, name, expectedName }) => {
    describe('when using the modal primary button', () => {
      beforeEach(async () => {
        mountComponent();

        open(modalType, '');
        await nextTick();

        findInput().setValue(name);
        triggerSubmitModal();
      });

      it('triggers createTempEntry action', () => {
        expect(store.dispatch).toHaveBeenCalledWith('createTempEntry', {
          name: expectedName,
          type: modalType,
        });
      });
    });

    describe('when triggering form submit (pressing enter)', () => {
      beforeEach(async () => {
        mountComponent();

        open(modalType, '');
        await nextTick();

        findInput().setValue(name);
        triggerSubmitForm();
      });

      it('triggers createTempEntry action', () => {
        expect(store.dispatch).toHaveBeenCalledWith('createTempEntry', {
          name: expectedName,
          type: modalType,
        });
      });
    });
  });

  describe('when creating from template type', () => {
    beforeEach(async () => {
      mountComponent();

      open('blob', 'some_dir');

      await nextTick();

      // Set input, then trigger button
      findInput().setValue('some_dir/foo.js');
      findTemplateButtons().at(1).vm.$emit('click');
    });

    it('triggers createTempEntry action', () => {
      const { name: expectedName } = store.getters['fileTemplates/templateTypes'][1];

      expect(store.dispatch).toHaveBeenCalledWith('createTempEntry', {
        name: `some_dir/${expectedName}`,
        type: 'blob',
      });
    });

    it('toggles modal', () => {
      expect(toggleModal).toHaveBeenCalled();
    });
  });

  describe.each`
    origPath            | title              | inputValue          | inputSelectionStart
    ${'src/parent_dir'} | ${'Rename folder'} | ${'src/parent_dir'} | ${'src/'.length}
    ${'README.md'}      | ${'Rename file'}   | ${'README.md'}      | ${0}
  `('when renaming for $origPath', ({ origPath, title, inputValue, inputSelectionStart }) => {
    beforeEach(async () => {
      mountComponent();

      open('rename', origPath);

      await nextTick();
    });

    it('sets modal props for renaming', () => {
      expect(findGlModal().props()).toMatchObject({
        title,
        actionPrimary: {
          attributes: [{ variant: 'confirm' }],
          text: title,
        },
      });
    });

    it('sets input value', () => {
      expect(findInput().element.value).toBe(inputValue);
    });

    it(`does not show file templates`, () => {
      expect(findTemplateButtonsModel()).toHaveLength(0);
    });

    it('shows modal when renaming', () => {
      expect(showModal).toHaveBeenCalled();
    });

    it('focus on input when renaming', () => {
      expect(document.activeElement).toBe(findInput().element);
    });

    it('selects name part of the input', () => {
      expect(findInput().element.selectionStart).toBe(inputSelectionStart);
      expect(findInput().element.selectionEnd).toBe(origPath.length);
    });

    describe('when renames is submitted successfully', () => {
      describe('when using the modal primary button', () => {
        beforeEach(() => {
          findInput().setValue(NEW_NAME);
          triggerSubmitModal();
        });

        it('dispatches renameEntry event', () => {
          expect(store.dispatch).toHaveBeenCalledWith('renameEntry', {
            path: origPath,
            parentPath: '',
            name: NEW_NAME,
          });
        });

        it('does not trigger flash', () => {
          expect(createAlert).not.toHaveBeenCalled();
        });
      });

      describe('when triggering form submit (pressing enter)', () => {
        beforeEach(() => {
          findInput().setValue(NEW_NAME);
          triggerSubmitForm();
        });

        it('dispatches renameEntry event', () => {
          expect(store.dispatch).toHaveBeenCalledWith('renameEntry', {
            path: origPath,
            parentPath: '',
            name: NEW_NAME,
          });
        });

        it('does not trigger flash', () => {
          expect(createAlert).not.toHaveBeenCalled();
        });
      });
    });
  });

  describe('when renaming and file already exists', () => {
    beforeEach(async () => {
      mountComponent();

      open('rename', 'src/parent_dir');

      await nextTick();

      // Set to something that already exists!
      findInput().setValue('src');
      triggerSubmitModal();
    });

    it('creates flash', () => {
      expect(createAlert).toHaveBeenCalledWith({
        message: 'The name "src" is already taken in this directory.',
        fadeTransition: false,
        addBodyClass: true,
      });
    });

    it('does not dispatch event', () => {
      expect(store.dispatch).not.toHaveBeenCalled();
    });
  });

  describe('when renaming and file has been deleted', () => {
    beforeEach(async () => {
      mountComponent();

      open('rename', 'src/parent_dir/foo.js');

      await nextTick();

      findInput().setValue('src/deleted.js');
      triggerSubmitModal();
    });

    it('does not create flash', () => {
      expect(createAlert).not.toHaveBeenCalled();
    });

    it('dispatches event', () => {
      expect(store.dispatch).toHaveBeenCalledWith('renameEntry', {
        path: 'src/parent_dir/foo.js',
        name: 'deleted.js',
        parentPath: 'src',
      });
    });
  });
});