Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit104b18e

Browse files
committed
test improvements
1 parent727e2bd commit104b18e

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-
consterrorSpy=jest.fn();
1243-
render(
1244-
<ErrorBoundaryspy={errorSpy}>
1240+
constconsoleError=console.error;
1241+
console.error=jest.fn();// Suppress React error boundary warning
1242+
expect(()=>{
1243+
render(
12451244
<FormonSubmit={onSubmitMock}>
12461245
{()=><Fieldname={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*asReactfrom"react";
2+
import{render}from"@testing-library/react";
3+
importFormContextfrom"./context";
4+
importReactFinalFormfrom"./ReactFinalForm";
5+
6+
describe("FormContext",()=>{
7+
it("should provide form context to children",()=>{
8+
constspy=jest.fn();
9+
constTestComponent=()=>{
10+
constform=React.useContext(FormContext);
11+
spy(form);
12+
returnnull;
13+
};
14+
15+
render(
16+
<ReactFinalFormonSubmit={()=>{}}>
17+
{({ form})=>(
18+
<FormContext.Providervalue={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(typeofspy.mock.calls[0][0].registerField).toBe("function");
28+
});
29+
30+
it("should have undefined as default value",()=>{
31+
constspy=jest.fn();
32+
constTestComponent=()=>{
33+
constform=React.useContext(FormContext);
34+
spy(form);
35+
returnnull;
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+
constspy=jest.fn();
463+
constMyField=({ 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+
<FormonSubmit={onSubmitMock}initialValues={{myField:null}}>
476+
{()=>(
477+
<form>
478+
<MyFieldname="myField"/>
479+
</form>
480+
)}
481+
</Form>,
482+
);
483+
484+
// Change to non-null value
485+
rerender(
486+
<FormonSubmit={onSubmitMock}initialValues={{myField:"test"}}>
487+
{()=>(
488+
<form>
489+
<MyFieldname="myField"/>
490+
</form>
491+
)}
492+
</Form>,
493+
);
494+
495+
// Change back to null
496+
rerender(
497+
<FormonSubmit={onSubmitMock}initialValues={{myField:null}}>
498+
{()=>(
499+
<form>
500+
<MyFieldname="myField"/>
501+
</form>
502+
)}
503+
</Form>,
504+
);
505+
506+
constcalls=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)

[8]ページ先頭

©2009-2025 Movatter.jp