Skip to content

Commit 69c3fbb

Browse files
author
Dinesh Agent
committed
Fix failing tests: Update test expectations and pause validation
1. Update ReactFinalForm test to expect 2 registerField calls (once for initial state, once for subscription) 2. Pause/resume validation during synchronous registration to prevent React "setState during render" errors
1 parent 31bc98b commit 69c3fbb

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/ReactFinalForm.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,10 @@ describe("ReactFinalForm", () => {
925925
expect(formMock).toHaveBeenCalled();
926926

927927
// called once on first render to get initial state, and then again to subscribe
928-
expect(formMock).toHaveBeenCalledTimes(1);
928+
expect(formMock).toHaveBeenCalledTimes(2);
929929
expect(formMock.mock.calls[0][0]).toBe("name");
930-
expect(formMock.mock.calls[0][2].active).toBe(true); // default subscription
930+
expect(formMock.mock.calls[1][0]).toBe("name");
931+
expect(formMock.mock.calls[1][2].active).toBe(true); // default subscription
931932
});
932933

933934
it("should not destroy on unregister on initial unregister", () => {

src/useField.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,17 @@ function useField<
106106
const destroyOnUnregister = form.destroyOnUnregister;
107107
form.destroyOnUnregister = false;
108108

109+
// Pause validation to prevent notifications during synchronous registration
110+
form.pauseValidation();
111+
109112
// Register field synchronously with silent=true, capture state, then unregister
110113
register((fieldState) => {
111114
initialFieldState = fieldState;
112115
}, true)();
113116

117+
// Resume validation
118+
form.resumeValidation();
119+
114120
// Restore destroyOnUnregister to its original value
115121
form.destroyOnUnregister = destroyOnUnregister;
116122

0 commit comments

Comments
 (0)