Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/ReactFinalForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,4 +990,61 @@ describe("ReactFinalForm", () => {
fireEvent.focus(getByTestId("password"));
expect(getByTestId("password").value).toBe("f1nal-f0rm-RULEZ");
});

it("should set submitting back to false when onSubmit returns Promise.resolve() immediately (#903)", async () => {
// Regression test for: https://github.com/final-form/react-final-form/issues/903
// When onSubmit is an async function with no awaits (returns Promise<void>),
// submitting should reset to false after the promise resolves.
const onSubmit = jest.fn(async () => {
// async with no await — resolves in the next microtask, no real async delay
});
const recordSubmitting = jest.fn();
const { getByText } = render(
<Form onSubmit={onSubmit} subscription={{ submitting: true }}>
{({ handleSubmit, submitting }) => {
recordSubmitting(submitting);
return (
<form onSubmit={handleSubmit}>
<Field name="name" component="input" />
<button type="submit">Submit</button>
</form>
);
}}
</Form>,
);

fireEvent.click(getByText("Submit"));

// Wait for the microtask Promise to resolve and React to update
await act(async () => {});

// submitting should have gone true then back to false
const calls = recordSubmitting.mock.calls.map((c) => c[0]);
expect(calls).toContain(true); // was submitting at some point
expect(calls[calls.length - 1]).toBe(false); // ends as not submitting
});

it("should set submitting back to false when onSubmit returns Promise.resolve() (#903)", async () => {
const onSubmit = jest.fn(() => Promise.resolve());
const recordSubmitting = jest.fn();
const { getByText } = render(
<Form onSubmit={onSubmit} subscription={{ submitting: true }}>
{({ handleSubmit, submitting }) => {
recordSubmitting(submitting);
return (
<form onSubmit={handleSubmit}>
<Field name="name" component="input" />
<button type="submit">Submit</button>
</form>
);
}}
</Form>,
);

fireEvent.click(getByText("Submit"));
await act(async () => {});

const calls = recordSubmitting.mock.calls.map((c) => c[0]);
expect(calls[calls.length - 1]).toBe(false);
Comment on lines +1047 to +1048
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add assertion to verify submitting was true during submission.

For consistency with the first regression test and to ensure this test validates the complete state transition (not just the final state), add the toContain(true) assertion:

Proposed fix
     const calls = recordSubmitting.mock.calls.map((c) => c[0]);
+    expect(calls).toContain(true);  // was submitting at some point
     expect(calls[calls.length - 1]).toBe(false);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const calls = recordSubmitting.mock.calls.map((c) => c[0]);
expect(calls[calls.length - 1]).toBe(false);
const calls = recordSubmitting.mock.calls.map((c) => c[0]);
expect(calls).toContain(true); // was submitting at some point
expect(calls[calls.length - 1]).toBe(false);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/ReactFinalForm.test.js` around lines 1047 - 1048, The test currently only
asserts the final submitting state is false; update the assertions around
recordSubmitting to also verify that submitting was true at some point during
the submission lifecycle. After computing calls =
recordSubmitting.mock.calls.map((c) => c[0]), add an assertion using
expect(calls).toContain(true) (in addition to the existing
expect(calls[calls.length - 1]).toBe(false)) so the test validates the
transition from true -> false for the submitting state (references:
recordSubmitting, submitting).

});
});