debian-mirror-gitlab/spec/javascripts/dirty_submit/dirty_submit_form_spec.js

114 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { range as rge } from 'lodash';
2018-12-13 13:39:08 +05:30
import DirtySubmitForm from '~/dirty_submit/dirty_submit_form';
2019-03-02 22:35:43 +05:30
import { getInputValue, setInputValue, createForm } from './helper';
2018-12-13 13:39:08 +05:30
function expectToToggleDisableOnDirtyUpdate(submit, input) {
2019-03-02 22:35:43 +05:30
const originalValue = getInputValue(input);
2018-12-13 13:39:08 +05:30
expect(submit.disabled).toBe(true);
2019-03-02 22:35:43 +05:30
return setInputValue(input, `${originalValue} changes`)
2018-12-13 13:39:08 +05:30
.then(() => expect(submit.disabled).toBe(false))
2019-03-02 22:35:43 +05:30
.then(() => setInputValue(input, originalValue))
2018-12-13 13:39:08 +05:30
.then(() => expect(submit.disabled).toBe(true));
}
describe('DirtySubmitForm', () => {
2019-09-04 21:01:54 +05:30
const originalThrottleDuration = DirtySubmitForm.THROTTLE_DURATION;
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
describe('submit button tests', () => {
beforeEach(() => {
DirtySubmitForm.THROTTLE_DURATION = 0;
});
2018-12-13 13:39:08 +05:30
2019-09-04 21:01:54 +05:30
afterEach(() => {
DirtySubmitForm.THROTTLE_DURATION = originalThrottleDuration;
});
2018-12-13 13:39:08 +05:30
2019-09-04 21:01:54 +05:30
it('disables submit until there are changes', done => {
const { form, input, submit } = createForm();
2018-12-13 13:39:08 +05:30
2019-09-04 21:01:54 +05:30
new DirtySubmitForm(form); // eslint-disable-line no-new
2018-12-13 13:39:08 +05:30
2019-09-04 21:01:54 +05:30
return expectToToggleDisableOnDirtyUpdate(submit, input)
.then(done)
.catch(done.fail);
});
2018-12-13 13:39:08 +05:30
2019-09-04 21:01:54 +05:30
it('disables submit until there are changes when initializing with a falsy value', done => {
const { form, input, submit } = createForm();
input.value = '';
new DirtySubmitForm(form); // eslint-disable-line no-new
return expectToToggleDisableOnDirtyUpdate(submit, input)
.then(done)
.catch(done.fail);
});
2019-03-02 22:35:43 +05:30
2019-09-04 21:01:54 +05:30
it('disables submit until there are changes for radio inputs', done => {
const { form, input, submit } = createForm('radio');
2019-03-02 22:35:43 +05:30
2019-09-04 21:01:54 +05:30
new DirtySubmitForm(form); // eslint-disable-line no-new
2019-03-02 22:35:43 +05:30
2019-09-04 21:01:54 +05:30
return expectToToggleDisableOnDirtyUpdate(submit, input)
.then(done)
.catch(done.fail);
});
it('disables submit until there are changes for checkbox inputs', done => {
const { form, input, submit } = createForm('checkbox');
new DirtySubmitForm(form); // eslint-disable-line no-new
return expectToToggleDisableOnDirtyUpdate(submit, input)
.then(done)
.catch(done.fail);
});
2019-03-02 22:35:43 +05:30
});
2019-09-04 21:01:54 +05:30
describe('throttling tests', () => {
beforeEach(() => {
jasmine.clock().install();
DirtySubmitForm.THROTTLE_DURATION = 100;
});
afterEach(() => {
jasmine.clock().uninstall();
DirtySubmitForm.THROTTLE_DURATION = originalThrottleDuration;
});
it('throttles updates when rapid changes are made to a single form element', () => {
const { form, input } = createForm();
const updateDirtyInputSpy = spyOn(new DirtySubmitForm(form), 'updateDirtyInput');
2020-04-08 14:13:33 +05:30
rge(10).forEach(i => {
2019-09-04 21:01:54 +05:30
setInputValue(input, `change ${i}`, false);
});
jasmine.clock().tick(101);
expect(updateDirtyInputSpy).toHaveBeenCalledTimes(2);
});
it('does not throttle updates when rapid changes are made to different form elements', () => {
const form = document.createElement('form');
2020-04-08 14:13:33 +05:30
const range = rge(10);
2019-09-04 21:01:54 +05:30
range.forEach(i => {
form.innerHTML += `<input type="text" name="input-${i}" class="js-input-${i}"/>`;
});
const updateDirtyInputSpy = spyOn(new DirtySubmitForm(form), 'updateDirtyInput');
range.forEach(i => {
const input = form.querySelector(`.js-input-${i}`);
setInputValue(input, `change`, false);
});
2019-03-02 22:35:43 +05:30
2019-09-04 21:01:54 +05:30
jasmine.clock().tick(101);
2019-03-02 22:35:43 +05:30
2019-09-04 21:01:54 +05:30
expect(updateDirtyInputSpy).toHaveBeenCalledTimes(range.length);
});
2019-03-02 22:35:43 +05:30
});
2018-12-13 13:39:08 +05:30
});