Skip to content

Commit

Permalink
fix(button): sometimes submits form even when click listener prevents…
Browse files Browse the repository at this point in the history
… default

Fixes #5032

PiperOrigin-RevId: 588925434
  • Loading branch information
asyncLiz authored and copybara-github committed Dec 7, 2023
1 parent e78a52f commit 9e3f080
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/controller/form-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export function setupFormSubmitter(ctor: FormSubmitterConstructor) {
return;
}

// Wait a microtask for event bubbling to complete.
// Wait a full task for event bubbling to complete.
await new Promise<void>((resolve) => {
resolve();
setTimeout(resolve);
});

if (event.defaultPrevented) {
Expand Down
10 changes: 10 additions & 0 deletions internal/controller/form-submitter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ describe('setupFormSubmitter()', () => {
spyOn(form, 'requestSubmit');
spyOn(form, 'reset');
await harness.clickWithMouse();
// Submission happens after a task
await env.waitForStability();

expect(form.requestSubmit).toHaveBeenCalled();
expect(form.reset).not.toHaveBeenCalled();
Expand All @@ -80,6 +82,8 @@ describe('setupFormSubmitter()', () => {
spyOn(form, 'requestSubmit');
spyOn(form, 'reset');
await harness.clickWithMouse();
// Submission happens after a task
await env.waitForStability();

expect(form.requestSubmit).not.toHaveBeenCalled();
expect(form.reset).toHaveBeenCalled();
Expand All @@ -100,6 +104,8 @@ describe('setupFormSubmitter()', () => {
);

await harness.clickWithMouse();
// Submission happens after a task
await env.waitForStability();

expect(form.requestSubmit).not.toHaveBeenCalled();
});
Expand All @@ -115,6 +121,8 @@ describe('setupFormSubmitter()', () => {
form.addEventListener('submit', submitListener);

await harness.clickWithMouse();
// Submission happens after a task
await env.waitForStability();

expect(submitListener).toHaveBeenCalled();
const event = submitListener.calls.argsFor(0)[0] as SubmitEvent;
Expand All @@ -133,6 +141,8 @@ describe('setupFormSubmitter()', () => {
harness.element.value = 'bar';

await harness.clickWithMouse();
// Submission happens after a task
await env.waitForStability();

const formData = Array.from(new FormData(form));
expect(formData.length).withContext('formData.length').toBe(1);
Expand Down

0 comments on commit 9e3f080

Please sign in to comment.