2020-04-27 18:09:41 +00:00
|
|
|
import { range as rge, throttle } from 'lodash';
|
2018-10-15 13:36:19 +00:00
|
|
|
import DirtySubmitForm from '~/dirty_submit/dirty_submit_form';
|
2018-11-14 19:51:39 +00:00
|
|
|
import { getInputValue, setInputValue, createForm } from './helper';
|
2018-10-15 13:36:19 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
jest.mock('lodash/throttle', () => jest.fn((fn) => fn));
|
2020-04-27 18:09:41 +00:00
|
|
|
const lodash = jest.requireActual('lodash');
|
|
|
|
|
2018-11-06 08:48:03 +00:00
|
|
|
function expectToToggleDisableOnDirtyUpdate(submit, input) {
|
2018-11-14 19:51:39 +00:00
|
|
|
const originalValue = getInputValue(input);
|
2018-11-06 08:48:03 +00:00
|
|
|
|
|
|
|
expect(submit.disabled).toBe(true);
|
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
setInputValue(input, `${originalValue} changes`);
|
|
|
|
expect(submit.disabled).toBe(false);
|
|
|
|
setInputValue(input, originalValue);
|
|
|
|
expect(submit.disabled).toBe(true);
|
2018-11-06 08:48:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 13:36:19 +00:00
|
|
|
describe('DirtySubmitForm', () => {
|
2019-05-17 17:05:15 +00:00
|
|
|
describe('submit button tests', () => {
|
2020-04-27 18:09:41 +00:00
|
|
|
it('disables submit until there are changes', () => {
|
2019-05-17 17:05:15 +00:00
|
|
|
const { form, input, submit } = createForm();
|
2018-11-06 08:48:03 +00:00
|
|
|
|
2019-05-17 17:05:15 +00:00
|
|
|
new DirtySubmitForm(form); // eslint-disable-line no-new
|
2018-11-06 08:48:03 +00:00
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
expectToToggleDisableOnDirtyUpdate(submit, input);
|
2019-05-17 17:05:15 +00:00
|
|
|
});
|
2018-10-15 13:36:19 +00:00
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
it('disables submit until there are changes when initializing with a falsy value', () => {
|
2019-05-17 17:05:15 +00:00
|
|
|
const { form, input, submit } = createForm();
|
|
|
|
input.value = '';
|
|
|
|
|
|
|
|
new DirtySubmitForm(form); // eslint-disable-line no-new
|
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
expectToToggleDisableOnDirtyUpdate(submit, input);
|
2019-05-17 17:05:15 +00:00
|
|
|
});
|
2018-11-14 19:51:39 +00:00
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
it('disables submit until there are changes for radio inputs', () => {
|
2019-05-17 17:05:15 +00:00
|
|
|
const { form, input, submit } = createForm('radio');
|
2018-11-14 19:51:39 +00:00
|
|
|
|
2019-05-17 17:05:15 +00:00
|
|
|
new DirtySubmitForm(form); // eslint-disable-line no-new
|
2018-11-14 19:51:39 +00:00
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
expectToToggleDisableOnDirtyUpdate(submit, input);
|
2019-05-17 17:05:15 +00:00
|
|
|
});
|
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
it('disables submit until there are changes for checkbox inputs', () => {
|
2019-05-17 17:05:15 +00:00
|
|
|
const { form, input, submit } = createForm('checkbox');
|
|
|
|
|
|
|
|
new DirtySubmitForm(form); // eslint-disable-line no-new
|
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
expectToToggleDisableOnDirtyUpdate(submit, input);
|
2019-05-17 17:05:15 +00:00
|
|
|
});
|
2018-11-14 19:51:39 +00:00
|
|
|
});
|
|
|
|
|
2019-05-17 17:05:15 +00:00
|
|
|
describe('throttling tests', () => {
|
|
|
|
beforeEach(() => {
|
2020-04-27 18:09:41 +00:00
|
|
|
throttle.mockImplementation(lodash.throttle);
|
|
|
|
jest.useFakeTimers();
|
2019-05-17 17:05:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-04-27 18:09:41 +00:00
|
|
|
throttle.mockReset();
|
2019-05-17 17:05:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throttles updates when rapid changes are made to a single form element', () => {
|
|
|
|
const { form, input } = createForm();
|
2020-04-27 18:09:41 +00:00
|
|
|
const updateDirtyInputSpy = jest.spyOn(new DirtySubmitForm(form), 'updateDirtyInput');
|
2019-05-17 17:05:15 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
rge(10).forEach((i) => {
|
2019-05-17 17:05:15 +00:00
|
|
|
setInputValue(input, `change ${i}`, false);
|
|
|
|
});
|
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2019-05-17 17:05:15 +00:00
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
expect(updateDirtyInputSpy).toHaveBeenCalledTimes(1);
|
2019-05-17 17:05:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not throttle updates when rapid changes are made to different form elements', () => {
|
|
|
|
const form = document.createElement('form');
|
2020-03-11 21:09:19 +00:00
|
|
|
const range = rge(10);
|
2020-12-23 21:10:24 +00:00
|
|
|
range.forEach((i) => {
|
2019-05-17 17:05:15 +00:00
|
|
|
form.innerHTML += `<input type="text" name="input-${i}" class="js-input-${i}"/>`;
|
|
|
|
});
|
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
const updateDirtyInputSpy = jest.spyOn(new DirtySubmitForm(form), 'updateDirtyInput');
|
2019-05-17 17:05:15 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
range.forEach((i) => {
|
2019-05-17 17:05:15 +00:00
|
|
|
const input = form.querySelector(`.js-input-${i}`);
|
|
|
|
setInputValue(input, `change`, false);
|
|
|
|
});
|
2018-11-14 19:51:39 +00:00
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2018-11-14 19:51:39 +00:00
|
|
|
|
2019-05-17 17:05:15 +00:00
|
|
|
expect(updateDirtyInputSpy).toHaveBeenCalledTimes(range.length);
|
|
|
|
});
|
2022-03-04 18:20:01 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-14 19:51:39 +00:00
|
|
|
});
|
2018-10-15 13:36:19 +00:00
|
|
|
});
|