diff options
Diffstat (limited to 'spec/javascripts/dirty_submit')
4 files changed, 0 insertions, 209 deletions
diff --git a/spec/javascripts/dirty_submit/dirty_submit_collection_spec.js b/spec/javascripts/dirty_submit/dirty_submit_collection_spec.js deleted file mode 100644 index 47be0b3ce9d..00000000000 --- a/spec/javascripts/dirty_submit/dirty_submit_collection_spec.js +++ /dev/null @@ -1,29 +0,0 @@ -import DirtySubmitCollection from '~/dirty_submit/dirty_submit_collection'; -import { setInputValue, createForm } from './helper'; - -describe('DirtySubmitCollection', () => { - it('disables submits until there are changes', done => { - const testElementsCollection = [createForm(), createForm()]; - const forms = testElementsCollection.map(testElements => testElements.form); - - new DirtySubmitCollection(forms); // eslint-disable-line no-new - - testElementsCollection.forEach(testElements => { - const { input, submit } = testElements; - const originalValue = input.value; - - expect(submit.disabled).toBe(true); - - return setInputValue(input, `${originalValue} changes`) - .then(() => { - expect(submit.disabled).toBe(false); - }) - .then(() => setInputValue(input, originalValue)) - .then(() => { - expect(submit.disabled).toBe(true); - }) - .then(done) - .catch(done.fail); - }); - }); -}); diff --git a/spec/javascripts/dirty_submit/dirty_submit_factory_spec.js b/spec/javascripts/dirty_submit/dirty_submit_factory_spec.js deleted file mode 100644 index 40843a68582..00000000000 --- a/spec/javascripts/dirty_submit/dirty_submit_factory_spec.js +++ /dev/null @@ -1,18 +0,0 @@ -import dirtySubmitFactory from '~/dirty_submit/dirty_submit_factory'; -import DirtySubmitForm from '~/dirty_submit/dirty_submit_form'; -import DirtySubmitCollection from '~/dirty_submit/dirty_submit_collection'; -import { createForm } from './helper'; - -describe('DirtySubmitCollection', () => { - it('returns a DirtySubmitForm instance for single form elements', () => { - const { form } = createForm(); - - expect(dirtySubmitFactory(form) instanceof DirtySubmitForm).toBe(true); - }); - - it('returns a DirtySubmitCollection instance for a collection of form elements', () => { - const forms = [createForm().form, createForm().form]; - - expect(dirtySubmitFactory(forms) instanceof DirtySubmitCollection).toBe(true); - }); -}); diff --git a/spec/javascripts/dirty_submit/dirty_submit_form_spec.js b/spec/javascripts/dirty_submit/dirty_submit_form_spec.js deleted file mode 100644 index 42f806fa1bf..00000000000 --- a/spec/javascripts/dirty_submit/dirty_submit_form_spec.js +++ /dev/null @@ -1,114 +0,0 @@ -import { range as rge } from 'lodash'; -import DirtySubmitForm from '~/dirty_submit/dirty_submit_form'; -import { getInputValue, setInputValue, createForm } from './helper'; - -function expectToToggleDisableOnDirtyUpdate(submit, input) { - const originalValue = getInputValue(input); - - expect(submit.disabled).toBe(true); - - return setInputValue(input, `${originalValue} changes`) - .then(() => expect(submit.disabled).toBe(false)) - .then(() => setInputValue(input, originalValue)) - .then(() => expect(submit.disabled).toBe(true)); -} - -describe('DirtySubmitForm', () => { - const originalThrottleDuration = DirtySubmitForm.THROTTLE_DURATION; - - describe('submit button tests', () => { - beforeEach(() => { - DirtySubmitForm.THROTTLE_DURATION = 0; - }); - - afterEach(() => { - DirtySubmitForm.THROTTLE_DURATION = originalThrottleDuration; - }); - - it('disables submit until there are changes', done => { - const { form, input, submit } = createForm(); - - new DirtySubmitForm(form); // eslint-disable-line no-new - - return expectToToggleDisableOnDirtyUpdate(submit, input) - .then(done) - .catch(done.fail); - }); - - 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); - }); - - it('disables submit until there are changes for radio inputs', done => { - const { form, input, submit } = createForm('radio'); - - new DirtySubmitForm(form); // eslint-disable-line no-new - - 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); - }); - }); - - describe('throttling tests', () => { - beforeEach(() => { - jasmine.clock().install(); - jasmine.clock().mockDate(); - 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'); - - rge(10).forEach(i => { - 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'); - const range = rge(10); - 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); - }); - - jasmine.clock().tick(101); - - expect(updateDirtyInputSpy).toHaveBeenCalledTimes(range.length); - }); - }); -}); diff --git a/spec/javascripts/dirty_submit/helper.js b/spec/javascripts/dirty_submit/helper.js deleted file mode 100644 index b51783cb915..00000000000 --- a/spec/javascripts/dirty_submit/helper.js +++ /dev/null @@ -1,48 +0,0 @@ -import DirtySubmitForm from '~/dirty_submit/dirty_submit_form'; -import setTimeoutPromiseHelper from '../helpers/set_timeout_promise_helper'; - -function isCheckableType(type) { - return /^(radio|checkbox)$/.test(type); -} - -export function setInputValue(element, value) { - const { type } = element; - let eventType; - - if (isCheckableType(type)) { - element.checked = !element.checked; - eventType = 'change'; - } else { - element.value = value; - eventType = 'input'; - } - - element.dispatchEvent( - new Event(eventType, { - bubbles: true, - }), - ); - - return setTimeoutPromiseHelper(DirtySubmitForm.THROTTLE_DURATION); -} - -export function getInputValue(input) { - return isCheckableType(input.type) ? input.checked : input.value; -} - -export function createForm(type = 'text') { - const form = document.createElement('form'); - form.innerHTML = ` - <input type="${type}" name="${type}" class="js-input"/> - <button type="submit" class="js-dirty-submit"></button> - `; - - const input = form.querySelector('.js-input'); - const submit = form.querySelector('.js-dirty-submit'); - - return { - form, - input, - submit, - }; -} |