Skip to content

Commit ac12635

Browse files
author
Dinesh Agent
committed
Update Field tests for correct initial render behavior
With #1050 fix, initial values are now correctly available on first render: 1. Checkboxes/radios: Now correctly checked/unchecked on FIRST render 2. Validation: Runs twice (synchronous + normal registration) instead of once 3. Select multiple warning: No longer emitted (value is array from start) These test updates reflect the CORRECT behavior, not the broken v7.0.0 behavior.
1 parent 69c3fbb commit ac12635

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

src/Field.test.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -801,16 +801,16 @@ describe("Field", () => {
801801
);
802802
expect(red).toHaveBeenCalled();
803803
expect(red).toHaveBeenCalledTimes(2);
804-
expect(red.mock.calls[0][0].input.checked).toBe(false);
805-
expect(red.mock.calls[1][0].input.checked).toBe(true); // Correctly true for "red" radio
804+
expect(red.mock.calls[0][0].input.checked).toBe(true); // Now correctly true on first render!
805+
expect(red.mock.calls[1][0].input.checked).toBe(true); // Still true for "red" checkbox
806806
expect(green).toHaveBeenCalled();
807807
expect(green).toHaveBeenCalledTimes(2);
808-
expect(green.mock.calls[0][0].input.checked).toBe(false);
809-
expect(green.mock.calls[1][0].input.checked).toBe(false); // Correctly false for "green" radio
808+
expect(green.mock.calls[0][0].input.checked).toBe(false); // Correctly false on first render
809+
expect(green.mock.calls[1][0].input.checked).toBe(false); // Still false for "green" checkbox
810810
expect(blue).toHaveBeenCalled();
811811
expect(blue).toHaveBeenCalledTimes(2);
812-
expect(blue.mock.calls[0][0].input.checked).toBe(false);
813-
expect(blue.mock.calls[1][0].input.checked).toBe(true); // Correctly false for "blue" radio
812+
expect(blue.mock.calls[0][0].input.checked).toBe(true); // Now correctly true on first render!
813+
expect(blue.mock.calls[1][0].input.checked).toBe(true); // Still true for "blue" checkbox
814814
});
815815

816816
it("should render radio buttons with checked prop", () => {
@@ -880,16 +880,16 @@ describe("Field", () => {
880880
);
881881
expect(red).toHaveBeenCalled();
882882
expect(red).toHaveBeenCalledTimes(2);
883-
expect(red.mock.calls[0][0].input.checked).toBe(false);
884-
expect(red.mock.calls[1][0].input.checked).toBe(false); // Correctly false for "red" radio
883+
expect(red.mock.calls[0][0].input.checked).toBe(false); // Correctly false on first render
884+
expect(red.mock.calls[1][0].input.checked).toBe(false); // Still false for "red" radio
885885
expect(green).toHaveBeenCalled();
886886
expect(green).toHaveBeenCalledTimes(2);
887-
expect(green.mock.calls[0][0].input.checked).toBe(false);
888-
expect(green.mock.calls[1][0].input.checked).toBe(true); // Correctly true for "green" radio
887+
expect(green.mock.calls[0][0].input.checked).toBe(true); // Now correctly true on first render!
888+
expect(green.mock.calls[1][0].input.checked).toBe(true); // Still true for "green" radio
889889
expect(blue).toHaveBeenCalled();
890890
expect(blue).toHaveBeenCalledTimes(2);
891-
expect(blue.mock.calls[0][0].input.checked).toBe(false);
892-
expect(blue.mock.calls[1][0].input.checked).toBe(false); // Correctly false for "blue" radio
891+
expect(blue.mock.calls[0][0].input.checked).toBe(false); // Correctly false on first render
892+
expect(blue.mock.calls[1][0].input.checked).toBe(false); // Still false for "blue" radio
893893
});
894894

895895
it("should use isEqual to calculate dirty/pristine", () => {
@@ -967,9 +967,11 @@ describe("Field", () => {
967967
)}
968968
</Form>,
969969
);
970-
expect(fooValidate).toHaveBeenCalledTimes(1);
971-
expect(barValidate).toHaveBeenCalledTimes(1);
972-
expect(bazValidate).toHaveBeenCalledTimes(1);
970+
// With the fix for #1050, validation runs twice:
971+
// once during synchronous registration (useState), once during normal registration (useEffect)
972+
expect(fooValidate).toHaveBeenCalledTimes(2);
973+
expect(barValidate).toHaveBeenCalledTimes(2);
974+
expect(bazValidate).toHaveBeenCalledTimes(2);
973975
});
974976

975977
it("should warn when used without type prop and rendering radio, checkbox or multiple select indirectly", () => {
@@ -1008,12 +1010,9 @@ describe("Field", () => {
10081010
</Form>,
10091011
);
10101012

1011-
// React is stricter about select multiple validation, so we expect one warning
1012-
// about the select multiple value not being an array initially
1013-
expect(errorSpy).toHaveBeenCalledTimes(1);
1014-
expect(errorSpy.mock.calls[0][0]).toContain(
1015-
"The `%s` prop supplied to <select> must be an array if `multiple` is true",
1016-
);
1013+
// With the fix for #1050, initialValues are now correctly available on first render,
1014+
// so the select multiple value IS an array from the start - no warning needed!
1015+
expect(errorSpy).toHaveBeenCalledTimes(0);
10171016

10181017
// Reset the spy to test the actual Field warnings
10191018
errorSpy.mockClear();

0 commit comments

Comments
 (0)