Skip to content

Commit 104b18e

Browse files
committed
test improvements
1 parent 727e2bd commit 104b18e

File tree

3 files changed

+102
-15
lines changed

3 files changed

+102
-15
lines changed

src/Field.test.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,22 +1237,15 @@ describe("Field", () => {
12371237
});
12381238

12391239
it("should throw an error if name prop is undefined", () => {
1240-
jest.spyOn(console, "error").mockImplementation(() => {});
1241-
1242-
const errorSpy = jest.fn();
1243-
render(
1244-
<ErrorBoundary spy={errorSpy}>
1240+
const consoleError = console.error;
1241+
console.error = jest.fn(); // Suppress React error boundary warning
1242+
expect(() => {
1243+
render(
12451244
<Form onSubmit={onSubmitMock}>
12461245
{() => <Field name={undefined} render={() => <input />} />}
1247-
</Form>
1248-
</ErrorBoundary>,
1249-
);
1250-
1251-
expect(errorSpy).toHaveBeenCalled();
1252-
expect(errorSpy).toHaveBeenCalledTimes(1);
1253-
expect(errorSpy.mock.calls[0][0].message).toBe(
1254-
"prop name cannot be undefined in <Field> component",
1255-
);
1256-
console.error.mockRestore();
1246+
</Form>,
1247+
);
1248+
}).toThrow("prop name cannot be undefined in <Field> component");
1249+
console.error = consoleError;
12571250
});
12581251
});

src/context.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as React from "react";
2+
import { render } from "@testing-library/react";
3+
import FormContext from "./context";
4+
import ReactFinalForm from "./ReactFinalForm";
5+
6+
describe("FormContext", () => {
7+
it("should provide form context to children", () => {
8+
const spy = jest.fn();
9+
const TestComponent = () => {
10+
const form = React.useContext(FormContext);
11+
spy(form);
12+
return null;
13+
};
14+
15+
render(
16+
<ReactFinalForm onSubmit={() => {}}>
17+
{({ form }) => (
18+
<FormContext.Provider value={form}>
19+
<TestComponent />
20+
</FormContext.Provider>
21+
)}
22+
</ReactFinalForm>,
23+
);
24+
25+
expect(spy).toHaveBeenCalledTimes(1);
26+
expect(spy.mock.calls[0][0]).toBeDefined();
27+
expect(typeof spy.mock.calls[0][0].registerField).toBe("function");
28+
});
29+
30+
it("should have undefined as default value", () => {
31+
const spy = jest.fn();
32+
const TestComponent = () => {
33+
const form = React.useContext(FormContext);
34+
spy(form);
35+
return null;
36+
};
37+
38+
render(<TestComponent />);
39+
40+
expect(spy).toHaveBeenCalledTimes(1);
41+
expect(spy.mock.calls[0][0]).toBeUndefined();
42+
});
43+
});

src/useField.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,55 @@ describe("useField", () => {
457457
expect(spy.mock.calls[3][1]).toBe(spy.mock.calls[2][1]); // onFocus
458458
expect(spy.mock.calls[3][2]).toBe(spy.mock.calls[2][2]); // onBlur
459459
});
460+
461+
it("should handle null values correctly with allowNull", () => {
462+
const spy = jest.fn();
463+
const MyField = ({ name }) => {
464+
const { input } = useField(name, {
465+
subscription: { value: true },
466+
allowNull: true,
467+
});
468+
spy(input.value);
469+
// Convert null to empty string for the input element to avoid React warnings
470+
return (
471+
<input {...input} value={input.value === null ? "" : input.value} />
472+
);
473+
};
474+
const { rerender } = render(
475+
<Form onSubmit={onSubmitMock} initialValues={{ myField: null }}>
476+
{() => (
477+
<form>
478+
<MyField name="myField" />
479+
</form>
480+
)}
481+
</Form>,
482+
);
483+
484+
// Change to non-null value
485+
rerender(
486+
<Form onSubmit={onSubmitMock} initialValues={{ myField: "test" }}>
487+
{() => (
488+
<form>
489+
<MyField name="myField" />
490+
</form>
491+
)}
492+
</Form>,
493+
);
494+
495+
// Change back to null
496+
rerender(
497+
<Form onSubmit={onSubmitMock} initialValues={{ myField: null }}>
498+
{() => (
499+
<form>
500+
<MyField name="myField" />
501+
</form>
502+
)}
503+
</Form>,
504+
);
505+
506+
const calls = spy.mock.calls.map((call) => call[0]);
507+
expect(calls).toContain(null); // At least one call with null
508+
expect(calls).toContain("test"); // At least one call with 'test'
509+
expect(calls[calls.length - 1]).toBe(null); // Last call is null
510+
});
460511
});

0 commit comments

Comments
 (0)