diff --git a/src/js/form-validation.test.ts b/src/js/form-validation.test.ts new file mode 100644 index 000000000..f5bcfa070 --- /dev/null +++ b/src/js/form-validation.test.ts @@ -0,0 +1,54 @@ +/** + * For the full copyright and license information, please view the + * LICENSE.md file that was distributed with this source code. + */ +import selectorsMap from '@constants/selectors-map'; +import initFormValidation from './form-validation'; + +const setUp = (markup: string) => { + document.body.innerHTML = markup; + + window.Theme = { + ...window.Theme, + selectors: selectorsMap, + }; + + initFormValidation(); +}; + +describe('Form validation', () => { + it('binds the submit button nested inside the form', () => { + setUp(` +
+ `); + + const form = document.querySelector('#f') as HTMLFormElement; + const button = document.querySelector('button') as HTMLButtonElement; + button.click(); + + expect(form.classList.contains('was-validated')).toBe(true); + }); + + it('binds a submit button attached through the form attribute', () => { + // WHY: the checkout address step keeps its submit button outside the form element, because + // the step also renders address + + + `); + + const form = document.querySelector('#f') as HTMLFormElement; + const button = document.querySelector('button') as HTMLButtonElement; + button.click(); + + expect(form.classList.contains('was-validated')).toBe(true); + }); + + it('does not throw when the form has no submit button', () => { + expect(() => setUp('')).not.toThrow(); + }); +}); diff --git a/src/js/form-validation.ts b/src/js/form-validation.ts index 5c590433d..906e33464 100644 --- a/src/js/form-validation.ts +++ b/src/js/form-validation.ts @@ -9,7 +9,12 @@ const initFormValidation = (selector?: string) => { const formValidationList = document.querySelectorAll