UNPKG

siesta-lite

Version:

Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers

85 lines (65 loc) 3.13 kB
StartTest(function (outerTest) { function prevent(event) { event.preventDefault(); event.returnValue = false; } outerTest.testBrowser(function (t) { t.it('should post form on ENTER press if only containing one text field', function (t) { document.body.innerHTML = '<form id="formWithOneInput" method="post"><input id="txt" type="text"></form>'; var formWithOneInput = document.getElementById('formWithOneInput'); formWithOneInput.onsubmit = function () { return false; }; t.firesOnce(formWithOneInput, 'submit', 'Expect a form to be posted on ENTER press if only containing one text field') formWithOneInput.addEventListener('submit', prevent); t.chain( { type : 'A[ENTER]', target : '#txt' } ); }) t.it('should NOT post form on ENTER press if form contains more than one text field', function (t) { document.body.innerHTML = '<form id="formWithTwoInputs" method="post"><input id="txt2" type="text"><input id="txt3" type="text"></form>'; var formWithTwoInputs = document.getElementById('formWithTwoInputs'); t.wontFire(formWithTwoInputs, 'submit', 'Expect a form to be posted on ENTER press if containing submit field') t.chain( { type : 'A[ENTER]', target : '#txt2' } ); }); t.it('should post form on ENTER press if form is containing a submit input', function (t) { document.body.innerHTML = '<form id="formWithSubmit" method="post"><input id="txt4" type="text"><input id="txt5" type="text"><input type="submit" value="Submit"></form>'; var formWithSubmit = document.getElementById('formWithSubmit'); formWithSubmit.addEventListener('submit', prevent); formWithSubmit.onsubmit = function () { return false; }; t.chain( { type : 'A[ENTER]', target : '#txt4' } ); }); t.it('should NOT post form on ENTER press if keypress event is prevented', function (t) { document.body.innerHTML = '<form id="formPrevented" method="post"><input id="txt6" type="text"><input type="submit" value="Submit"></form>'; var formPrevented = document.getElementById('formPrevented'); document.getElementById('txt6').addEventListener('keypress', function (event) { if (event.keyCode == 13) { event.preventDefault(); event.returnValue = false; } }); t.wontFire(formPrevented, 'submit', 'Expect a form NOT to be posted on ENTER press if event is prevented') t.chain( { type : 'A[ENTER]', target : '#txt6' } ); }); }); });