import { inputDuplicatorCreator } from '../input-duplicator'; import { assertNotNull } from '../utils/assert'; import { $, $$, removeEl } from '../utils/dom'; describe('Input duplicator functionality', () => { beforeEach(() => { document.documentElement.insertAdjacentHTML('beforeend', `
3
`); }); afterEach(() => { removeEl($$('form')); }); function runCreator() { inputDuplicatorCreator({ addButtonSelector: '.js-add-input', fieldSelector: '.js-input-source', maxInputCountSelector: '.js-max-input-count', removeButtonSelector: '.js-remove-input', }); } it('should ignore forms without a duplicator button', () => { removeEl($$('button')); expect(runCreator()).toBeUndefined(); }); it('should duplicate the input elements', () => { runCreator(); expect($$('input')).toHaveLength(1); assertNotNull($('.js-add-input')).click(); expect($$('input')).toHaveLength(2); }); it('should duplicate the input elements when the button is before the inputs', () => { const form = assertNotNull($('form')); const buttonDiv = assertNotNull($('.js-button-container')); removeEl(buttonDiv); form.insertAdjacentElement('afterbegin', buttonDiv); runCreator(); assertNotNull($('.js-add-input')).click(); expect($$('input')).toHaveLength(2); }); it('should not create more input elements than the limit', () => { runCreator(); for (let i = 0; i < 5; i += 1) { assertNotNull($('.js-add-input')).click(); } expect($$('input')).toHaveLength(3); }); it('should remove duplicated input elements', () => { runCreator(); assertNotNull($('.js-add-input')).click(); assertNotNull($('.js-remove-input')).click(); expect($$('input')).toHaveLength(1); }); it('should not remove the last input element', () => { runCreator(); assertNotNull($('.js-remove-input')).click(); assertNotNull($('.js-remove-input')).click(); for (let i = 0; i < 5; i += 1) { assertNotNull($('.js-remove-input')).click(); } expect($$('input')).toHaveLength(1); }); });