Skip to content

Commit

Permalink
html: Add tests for SubmitEvent interface (#19562)
Browse files Browse the repository at this point in the history
Specification PR: whatwg/html#4984
  • Loading branch information
tkent-google authored Nov 1, 2019
1 parent 7b222c0 commit 61c2804
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
36 changes: 36 additions & 0 deletions html/semantics/forms/form-submission-0/SubmitEvent.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#the-submitevent-interface

test(() => {
let button = document.createElement('button');
let typeError = new TypeError();
assert_throws(typeError, () => { new SubmitEvent() }, '0 arguments');
assert_throws(typeError, () => { new SubmitEvent('bar', button) }, '1 invalid arguments');
assert_throws(typeError, () => { new SubmitEvent(button, button) }, '2 invalid arguments');
assert_throws(typeError, () => { new SubmitEvent('foo', null) }, 'Null dictionary');
assert_throws(typeError, () => { new SubmitEvent('foo', undefined) }, 'Undefined dictionary');
assert_throws(typeError, () => { new SubmitEvent('foo', { submitter: null }) }, 'Null submitter');
assert_throws(typeError, () => { new SubmitEvent('foo', { submitter: undefined }) }, 'Undefined submitter');
assert_throws(typeError, () => { new SubmitEvent('foo', { submitter: 'bar' }) }, 'Wrong type of submitter');
}, 'Failing SubmitEvent constructor');

test(() => {
let button = document.createElement('button');
let event = new SubmitEvent('bar', { submitter: button, bubbles: true });
assert_equals(event.submitter, button);
assert_true(event.bubbles);
}, 'Successful SubmitEvent constructor');

test(() => {
let event = new SubmitEvent('bar', { submitter: null});
assert_equals(event.submitter, null);
}, 'Successful SubmitEvent constructor; null submitter');

test(() => {
let event = new SubmitEvent('baz', {});
assert_equals(event.submitter, null);
}, 'Successful SubmitEvent constructor; empty dictionary');

test(() => {
let event = new SubmitEvent('baz');
assert_equals(event.submitter, null);
}, 'Successful SubmitEvent constructor; missing dictionary');
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,60 @@
});
}, "If form's firing submission events is true, then return; 'submit' event");

promise_test(async () => {
let form = populateForm('<button type=submit></button><input name=n1 value=submit type=submit>');
let iframe = form.previousSibling;
let submitter = form.querySelector('input[type=submit]');
let event;
form.addEventListener('submit', e => { event = e; });
submitter.click();
await loadPromise(iframe);
assert_true(event.bubbles);
assert_true(event.cancelable);
assert_equals(event.submitter, submitter);
assert_true(event instanceof SubmitEvent);
}, 'firing an event named submit; clicking a submit button');

promise_test(async () => {
let form = populateForm('');
let iframe = form.previousSibling;
let event;
form.addEventListener('submit', e => { event = e; });
form.requestSubmit();
await loadPromise(iframe);
assert_true(event.bubbles);
assert_true(event.cancelable);
assert_equals(event.submitter, null);
assert_true(event instanceof SubmitEvent);
}, 'firing an event named submit; form.requestSubmit()');

promise_test(async () => {
let form = populateForm('');
let iframe = form.previousSibling;
let event;
form.addEventListener('submit', e => { event = e; });
form.requestSubmit(null);
await loadPromise(iframe);
assert_true(event.bubbles);
assert_true(event.cancelable);
assert_equals(event.submitter, null);
assert_true(event instanceof SubmitEvent);
}, 'firing an event named submit; form.requestSubmit(null)');

promise_test(async () => {
let form = populateForm('<input type=submit><button type=submit></button>');
let iframe = form.previousSibling;
let submitter = form.querySelector('button');
let event;
form.addEventListener('submit', e => { event = e; });
form.requestSubmit(submitter);
await loadPromise(iframe);
assert_true(event.bubbles);
assert_true(event.cancelable);
assert_equals(event.submitter, submitter);
assert_true(event instanceof SubmitEvent);
}, 'firing an event named submit; form.requestSubmit(submitter)');

async_test(t => {
let form = populateForm('<input name=n1 value=v1>');
form.onformdata = (e) => { e.target.remove(); };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<link rel="help" href="https://html.spec.whatwg.org/C/#implicit-submission">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="./resources/targetted-form.js"></script>
<body>
<script>
// This test file is "optional" because triggering implicit submission by
// "Enter" key is not standardized.

const ENTER = '\uE007';

promise_test(async () => {
let form = populateForm('<input name=text value=abc><input name=submiButton type=submit>');
let event;
form.text.focus();
form.addEventListener('submit', e => { event = e; });
await test_driver.send_keys(form.text, ENTER);
assert_true(event.bubbles);
assert_true(event.cancelable);
assert_equals(event.submitter, form.submitButton);
assert_true(event instanceof SubmitEvent);
}, 'Submit event with a submit button');

promise_test(async () => {
let form = populateForm('<input name=text value=abc>');
let event;
form.text.focus();
form.addEventListener('submit', e => { event = e; });
await test_driver.send_keys(form.text, ENTER);
assert_true(event.bubbles);
assert_true(event.cancelable);
assert_equals(event.submitter, null);
assert_true(event instanceof SubmitEvent);
}, 'Submit event with no submit button');
</script>
</body>
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ function submitPromise(form, iframe) {
form.submit();
});
}

function loadPromise(iframe) {
return new Promise((resolve, reject) => {
iframe.onload = resolve;
iframe.onerror = () => reject(new Error('iframe onerror fired'));
});
}

0 comments on commit 61c2804

Please sign in to comment.