|
| 1 | +import * as React from "react"; |
| 2 | +import { render, fireEvent, act, waitFor } from "@testing-library/react"; |
| 3 | +import "@testing-library/jest-dom"; |
| 4 | +import Form from "./ReactFinalForm"; |
| 5 | +import { useField } from "./index"; |
| 6 | + |
| 7 | +const onSubmitMock = () => {}; |
| 8 | + |
| 9 | +describe("useField — dynamic initialValue changes (#1085)", () => { |
| 10 | + // Tests for the initialValue-change effect: when the initialValue prop |
| 11 | + // changes on a mounted field, dirty/pristine state should update correctly. |
| 12 | + |
| 13 | + it("field becomes pristine when initialValue changes to match current value", async () => { |
| 14 | + let setInitial; |
| 15 | + |
| 16 | + const TestField = ({ initialValue }) => { |
| 17 | + const { input, meta } = useField("myField", { |
| 18 | + initialValue, |
| 19 | + subscription: { dirty: true, value: true }, |
| 20 | + }); |
| 21 | + return ( |
| 22 | + <div> |
| 23 | + <input {...input} data-testid="input" /> |
| 24 | + <span data-testid="dirty">{String(meta.dirty)}</span> |
| 25 | + </div> |
| 26 | + ); |
| 27 | + }; |
| 28 | + |
| 29 | + const Wrapper = () => { |
| 30 | + const [initial, setInitial_] = React.useState("original"); |
| 31 | + setInitial = setInitial_; |
| 32 | + return ( |
| 33 | + <Form onSubmit={onSubmitMock} initialValues={{ myField: "original" }}> |
| 34 | + {() => ( |
| 35 | + <form> |
| 36 | + <TestField initialValue={initial} /> |
| 37 | + </form> |
| 38 | + )} |
| 39 | + </Form> |
| 40 | + ); |
| 41 | + }; |
| 42 | + |
| 43 | + const { getByTestId } = render(<Wrapper />); |
| 44 | + |
| 45 | + // Starts pristine |
| 46 | + expect(getByTestId("dirty")).toHaveTextContent("false"); |
| 47 | + |
| 48 | + // Type "updated" → field becomes dirty |
| 49 | + fireEvent.change(getByTestId("input"), { target: { value: "updated" } }); |
| 50 | + expect(getByTestId("dirty")).toHaveTextContent("true"); |
| 51 | + |
| 52 | + // Now update initialValue to "updated" — field should become pristine |
| 53 | + await act(async () => { |
| 54 | + setInitial("updated"); |
| 55 | + }); |
| 56 | + |
| 57 | + await waitFor(() => { |
| 58 | + expect(getByTestId("dirty")).toHaveTextContent("false"); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("field value and initialValue both update when initialValue prop changes", async () => { |
| 63 | + // When initialValue changes and current value differs, re-registration |
| 64 | + // updates the form's tracked initialValue for this field. This verifies |
| 65 | + // the re-registration path executes without errors. |
| 66 | + let setInitial; |
| 67 | + |
| 68 | + const TestField = ({ initialValue }) => { |
| 69 | + const { input, meta } = useField("myField", { |
| 70 | + initialValue, |
| 71 | + subscription: { dirty: true, value: true }, |
| 72 | + }); |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + <input {...input} data-testid="input" /> |
| 76 | + <span data-testid="dirty">{String(meta.dirty)}</span> |
| 77 | + <span data-testid="value">{input.value}</span> |
| 78 | + </div> |
| 79 | + ); |
| 80 | + }; |
| 81 | + |
| 82 | + const Wrapper = () => { |
| 83 | + const [initial, setInitial_] = React.useState("original"); |
| 84 | + setInitial = setInitial_; |
| 85 | + return ( |
| 86 | + <Form onSubmit={onSubmitMock} initialValues={{ myField: "original" }}> |
| 87 | + {() => ( |
| 88 | + <form> |
| 89 | + <TestField initialValue={initial} /> |
| 90 | + </form> |
| 91 | + )} |
| 92 | + </Form> |
| 93 | + ); |
| 94 | + }; |
| 95 | + |
| 96 | + const { getByTestId } = render(<Wrapper />); |
| 97 | + |
| 98 | + // Type "updated" → field becomes dirty |
| 99 | + fireEvent.change(getByTestId("input"), { target: { value: "updated" } }); |
| 100 | + expect(getByTestId("dirty")).toHaveTextContent("true"); |
| 101 | + |
| 102 | + // Change initialValue to "updated" → value matches new initial → pristine |
| 103 | + await act(async () => { |
| 104 | + setInitial("updated"); |
| 105 | + }); |
| 106 | + |
| 107 | + await waitFor(() => { |
| 108 | + expect(getByTestId("dirty")).toHaveTextContent("false"); |
| 109 | + }); |
| 110 | + |
| 111 | + // Value remains what the user typed |
| 112 | + expect(getByTestId("value")).toHaveTextContent("updated"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("handles initialValue transitioning through undefined (value→undefined→value)", async () => { |
| 116 | + let setInitial; |
| 117 | + |
| 118 | + const TestField = ({ initialValue }) => { |
| 119 | + const { input, meta } = useField("myField", { |
| 120 | + initialValue, |
| 121 | + subscription: { dirty: true, value: true }, |
| 122 | + }); |
| 123 | + return ( |
| 124 | + <div> |
| 125 | + <input {...input} data-testid="input" /> |
| 126 | + <span data-testid="dirty">{String(meta.dirty)}</span> |
| 127 | + </div> |
| 128 | + ); |
| 129 | + }; |
| 130 | + |
| 131 | + const Wrapper = () => { |
| 132 | + const [initial, setInitial_] = React.useState("original"); |
| 133 | + setInitial = setInitial_; |
| 134 | + return ( |
| 135 | + <Form onSubmit={onSubmitMock} initialValues={{ myField: "original" }}> |
| 136 | + {() => ( |
| 137 | + <form> |
| 138 | + <TestField initialValue={initial} /> |
| 139 | + </form> |
| 140 | + )} |
| 141 | + </Form> |
| 142 | + ); |
| 143 | + }; |
| 144 | + |
| 145 | + const { getByTestId } = render(<Wrapper />); |
| 146 | + |
| 147 | + expect(getByTestId("dirty")).toHaveTextContent("false"); |
| 148 | + |
| 149 | + // Type "changed" → field becomes dirty |
| 150 | + fireEvent.change(getByTestId("input"), { target: { value: "changed" } }); |
| 151 | + expect(getByTestId("dirty")).toHaveTextContent("true"); |
| 152 | + |
| 153 | + // Transition: "original" → undefined → "changed" |
| 154 | + // Without the fix, the ref stays at "original" through the undefined step, |
| 155 | + // so "undefined → changed" looks like no change and re-registration is skipped. |
| 156 | + // With the fix, the ref advances through undefined so "changed" is detected. |
| 157 | + await act(async () => { |
| 158 | + setInitial(undefined); |
| 159 | + }); |
| 160 | + await act(async () => { |
| 161 | + setInitial("changed"); |
| 162 | + }); |
| 163 | + |
| 164 | + // initialValue now matches current value → should be pristine |
| 165 | + await waitFor(() => { |
| 166 | + expect(getByTestId("dirty")).toHaveTextContent("false"); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + it("uses custom isEqual when detecting initialValue changes", async () => { |
| 171 | + let setInitial; |
| 172 | + // Custom isEqual: objects are equal if their .id matches |
| 173 | + const isEqual = (a, b) => { |
| 174 | + if (a && b && typeof a === "object" && typeof b === "object") { |
| 175 | + return a.id === b.id; |
| 176 | + } |
| 177 | + return a === b; |
| 178 | + }; |
| 179 | + |
| 180 | + const TestField = ({ initialValue }) => { |
| 181 | + const { input, meta } = useField("myField", { |
| 182 | + initialValue, |
| 183 | + isEqual, |
| 184 | + subscription: { dirty: true, value: true }, |
| 185 | + }); |
| 186 | + return ( |
| 187 | + <div> |
| 188 | + <input |
| 189 | + {...input} |
| 190 | + value={JSON.stringify(input.value) || ""} |
| 191 | + onChange={(e) => { |
| 192 | + try { |
| 193 | + input.onChange(JSON.parse(e.target.value)); |
| 194 | + } catch { |
| 195 | + input.onChange(e.target.value); |
| 196 | + } |
| 197 | + }} |
| 198 | + data-testid="input" |
| 199 | + /> |
| 200 | + <span data-testid="dirty">{String(meta.dirty)}</span> |
| 201 | + </div> |
| 202 | + ); |
| 203 | + }; |
| 204 | + |
| 205 | + const Wrapper = () => { |
| 206 | + const [initial, setInitial_] = React.useState({ id: 1, label: "One" }); |
| 207 | + setInitial = setInitial_; |
| 208 | + return ( |
| 209 | + <Form |
| 210 | + onSubmit={onSubmitMock} |
| 211 | + initialValues={{ myField: { id: 1, label: "One" } }} |
| 212 | + > |
| 213 | + {() => ( |
| 214 | + <form> |
| 215 | + <TestField initialValue={initial} /> |
| 216 | + </form> |
| 217 | + )} |
| 218 | + </Form> |
| 219 | + ); |
| 220 | + }; |
| 221 | + |
| 222 | + const { getByTestId } = render(<Wrapper />); |
| 223 | + |
| 224 | + // Starts pristine |
| 225 | + expect(getByTestId("dirty")).toHaveTextContent("false"); |
| 226 | + |
| 227 | + // Change value to { id: 2 } → dirty |
| 228 | + fireEvent.change(getByTestId("input"), { |
| 229 | + target: { value: JSON.stringify({ id: 2, label: "Two" }) }, |
| 230 | + }); |
| 231 | + expect(getByTestId("dirty")).toHaveTextContent("true"); |
| 232 | + |
| 233 | + // Change initialValue to { id: 2, label: "Different label" } |
| 234 | + // isEqual treats id:2 === id:2, so initialValue "matches" current value |
| 235 | + // → field should become pristine |
| 236 | + await act(async () => { |
| 237 | + setInitial({ id: 2, label: "Different label" }); |
| 238 | + }); |
| 239 | + |
| 240 | + await waitFor(() => { |
| 241 | + expect(getByTestId("dirty")).toHaveTextContent("false"); |
| 242 | + }); |
| 243 | + }); |
| 244 | + |
| 245 | + it("field stays dirty when new initialValue does not match current value (no re-registration shortcut)", async () => { |
| 246 | + // When initialValue changes to X but the current value is Y (X ≠ Y), |
| 247 | + // the isEqual(currentValue, initialValue) check in the effect returns false |
| 248 | + // so the re-registration shortcut is skipped. |
| 249 | + // We verify the effect ran (no errors) and the field tracks dirty state. |
| 250 | + let setInitial; |
| 251 | + |
| 252 | + const TestField = ({ initialValue }) => { |
| 253 | + const { input, meta } = useField("myField", { |
| 254 | + initialValue, |
| 255 | + subscription: { dirty: true, pristine: true, value: true }, |
| 256 | + }); |
| 257 | + return ( |
| 258 | + <div> |
| 259 | + <input {...input} data-testid="input" /> |
| 260 | + <span data-testid="dirty">{String(meta.dirty)}</span> |
| 261 | + <span data-testid="pristine">{String(meta.pristine)}</span> |
| 262 | + </div> |
| 263 | + ); |
| 264 | + }; |
| 265 | + |
| 266 | + const Wrapper = () => { |
| 267 | + const [initial, setInitial_] = React.useState("original"); |
| 268 | + setInitial = setInitial_; |
| 269 | + return ( |
| 270 | + <Form onSubmit={onSubmitMock} initialValues={{ myField: "original" }}> |
| 271 | + {() => ( |
| 272 | + <form> |
| 273 | + <TestField initialValue={initial} /> |
| 274 | + </form> |
| 275 | + )} |
| 276 | + </Form> |
| 277 | + ); |
| 278 | + }; |
| 279 | + |
| 280 | + const { getByTestId } = render(<Wrapper />); |
| 281 | + |
| 282 | + // Starts pristine |
| 283 | + expect(getByTestId("dirty")).toHaveTextContent("false"); |
| 284 | + expect(getByTestId("pristine")).toHaveTextContent("true"); |
| 285 | + |
| 286 | + // Change initialValue to something new — field was never modified so it |
| 287 | + // adopts the new initialValue and remains pristine. |
| 288 | + await act(async () => { |
| 289 | + setInitial("new-initial"); |
| 290 | + }); |
| 291 | + |
| 292 | + await waitFor(() => { |
| 293 | + // Field stays pristine with new initialValue (value tracks initialValue) |
| 294 | + expect(getByTestId("pristine")).toHaveTextContent("true"); |
| 295 | + }); |
| 296 | + }); |
| 297 | +}); |
0 commit comments