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

dirty_submit_form_spec.js « dirty_submit « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bcbe824bd9fbe1a0c2ac58b6cc240e224910e94b (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
import { range as rge, throttle } from 'lodash';
import DirtySubmitForm from '~/dirty_submit/dirty_submit_form';
import { getInputValue, setInputValue, createForm } from './helper';

jest.mock('lodash/throttle', () => jest.fn((fn) => fn));
const lodash = jest.requireActual('lodash');

function expectToToggleDisableOnDirtyUpdate(submit, input) {
  const originalValue = getInputValue(input);

  expect(submit.disabled).toBe(true);

  setInputValue(input, `${originalValue} changes`);
  expect(submit.disabled).toBe(false);
  setInputValue(input, originalValue);
  expect(submit.disabled).toBe(true);
}

describe('DirtySubmitForm', () => {
  describe('submit button tests', () => {
    it('disables submit until there are changes', () => {
      const { form, input, submit } = createForm();

      new DirtySubmitForm(form); // eslint-disable-line no-new

      expectToToggleDisableOnDirtyUpdate(submit, input);
    });

    it('disables submit until there are changes when initializing with a falsy value', () => {
      const { form, input, submit } = createForm();
      input.value = '';

      new DirtySubmitForm(form); // eslint-disable-line no-new

      expectToToggleDisableOnDirtyUpdate(submit, input);
    });

    it('disables submit until there are changes for radio inputs', () => {
      const { form, input, submit } = createForm('radio');

      new DirtySubmitForm(form); // eslint-disable-line no-new

      expectToToggleDisableOnDirtyUpdate(submit, input);
    });

    it('disables submit until there are changes for checkbox inputs', () => {
      const { form, input, submit } = createForm('checkbox');

      new DirtySubmitForm(form); // eslint-disable-line no-new

      expectToToggleDisableOnDirtyUpdate(submit, input);
    });
  });

  describe('throttling tests', () => {
    beforeEach(() => {
      throttle.mockImplementation(lodash.throttle);
      jest.useFakeTimers();
    });

    afterEach(() => {
      throttle.mockReset();
    });

    it('throttles updates when rapid changes are made to a single form element', () => {
      const { form, input } = createForm();
      const updateDirtyInputSpy = jest.spyOn(new DirtySubmitForm(form), 'updateDirtyInput');

      rge(10).forEach((i) => {
        setInputValue(input, `change ${i}`, false);
      });

      jest.runOnlyPendingTimers();

      expect(updateDirtyInputSpy).toHaveBeenCalledTimes(1);
    });

    it('does not throttle updates when rapid changes are made to different form elements', () => {
      const form = document.createElement('form');
      const range = rge(10);
      range.forEach((i) => {
        form.innerHTML += `<input type="text" name="input-${i}" class="js-input-${i}"/>`;
      });

      const updateDirtyInputSpy = jest.spyOn(new DirtySubmitForm(form), 'updateDirtyInput');

      range.forEach((i) => {
        const input = form.querySelector(`.js-input-${i}`);
        setInputValue(input, `change`, false);
      });

      jest.runOnlyPendingTimers();

      expect(updateDirtyInputSpy).toHaveBeenCalledTimes(range.length);
    });

    describe('when inputs listener is added', () => {
      it('calls listener when changes are made to an input', () => {
        const { form, input } = createForm();
        const inputsListener = jest.fn();

        const dirtySubmitForm = new DirtySubmitForm(form);
        dirtySubmitForm.addInputsListener(inputsListener);

        setInputValue(input, 'new value');

        jest.runOnlyPendingTimers();

        expect(inputsListener).toHaveBeenCalledTimes(1);
      });

      describe('when inputs listener is removed', () => {
        it('does not call listener when changes are made to an input', () => {
          const { form, input } = createForm();
          const inputsListener = jest.fn();

          const dirtySubmitForm = new DirtySubmitForm(form);
          dirtySubmitForm.addInputsListener(inputsListener);
          dirtySubmitForm.removeInputsListener(inputsListener);

          setInputValue(input, 'new value');

          jest.runOnlyPendingTimers();

          expect(inputsListener).not.toHaveBeenCalled();
        });
      });
    });
  });
});