summaryrefslogtreecommitdiff
path: root/spec/frontend/blob/file_template_mediator_spec.js
blob: 44e12deb564425ba31de2c6305ee84fcaf806037 (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
import TemplateSelectorMediator from '~/blob/file_template_mediator';

describe('Template Selector Mediator', () => {
  let mediator;

  describe('setFilename', () => {
    let input;
    const newFileName = 'foo';
    const editor = jest.fn().mockImplementationOnce(() => ({
      getValue: jest.fn().mockImplementation(() => {}),
    }))();

    beforeEach(() => {
      setFixtures('<div class="file-editor"><input class="js-file-path-name-input" /></div>');
      input = document.querySelector('.js-file-path-name-input');
      mediator = new TemplateSelectorMediator({
        editor,
        currentAction: jest.fn(),
        projectId: jest.fn(),
      });
    });

    it('fills out the input field', () => {
      expect(input.value).toBe('');
      mediator.setFilename(newFileName);
      expect(input.value).toBe(newFileName);
    });

    it.each`
      name           | newName        | shouldDispatch
      ${newFileName} | ${newFileName} | ${false}
      ${newFileName} | ${''}          | ${true}
      ${newFileName} | ${undefined}   | ${false}
      ${''}          | ${''}          | ${false}
      ${''}          | ${newFileName} | ${true}
      ${''}          | ${undefined}   | ${false}
    `(
      'correctly reacts to the name change when current name is $name and newName is $newName',
      ({ name, newName, shouldDispatch }) => {
        input.value = name;
        const eventHandler = jest.fn();
        input.addEventListener('change', eventHandler);

        mediator.setFilename(newName);
        if (shouldDispatch) {
          expect(eventHandler).toHaveBeenCalledTimes(1);
        } else {
          expect(eventHandler).not.toHaveBeenCalled();
        }
      },
    );
  });
});