Skip to content

Commit bb5b551

Browse files
committed
Add tests
1 parent 4eebe87 commit bb5b551

File tree

2 files changed

+122
-4
lines changed

2 files changed

+122
-4
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { checkHasDirtyFields } from './useFormIsDirty';
2+
3+
describe('useFormIsDirty', () => {
4+
describe('checkHasDirtyFields', () => {
5+
it('should return true if any field is dirty on simple forms', () => {
6+
const dirtyFields = { name: true, age: false };
7+
expect(checkHasDirtyFields(dirtyFields)).toBe(true);
8+
});
9+
10+
it('should return false if no field is dirty on simple forms', () => {
11+
const dirtyFields = { name: false, age: false };
12+
expect(checkHasDirtyFields(dirtyFields)).toBe(false);
13+
});
14+
15+
it('should return true if any field is dirty on forms with nested fields', () => {
16+
const dirtyFields = {
17+
name: false,
18+
age: false,
19+
address: { street: true, city: false },
20+
};
21+
expect(checkHasDirtyFields(dirtyFields)).toBe(true);
22+
});
23+
24+
it('should return false if no field is dirty on forms with nested fields', () => {
25+
const dirtyFields = {
26+
name: false,
27+
age: false,
28+
address: { street: false, city: false },
29+
};
30+
expect(checkHasDirtyFields(dirtyFields)).toBe(false);
31+
});
32+
33+
it('should return true if any field is dirty on forms with array of scalar fields', () => {
34+
const dirtyFields = {
35+
name: false,
36+
age: false,
37+
hobbies: [true, false],
38+
};
39+
expect(checkHasDirtyFields(dirtyFields)).toBe(true);
40+
});
41+
42+
it('should return false if no field is dirty on forms with array of scalar fields', () => {
43+
const dirtyFields = {
44+
name: false,
45+
age: false,
46+
hobbies: [false, false],
47+
};
48+
expect(checkHasDirtyFields(dirtyFields)).toBe(false);
49+
});
50+
51+
it('should return true if any field is dirty on forms with array of objects', () => {
52+
const dirtyFields = {
53+
name: false,
54+
age: false,
55+
hobbies: [{ name: true }, { name: false }],
56+
};
57+
expect(checkHasDirtyFields(dirtyFields)).toBe(true);
58+
});
59+
60+
it('should return false if no field is dirty on forms with array of objects', () => {
61+
const dirtyFields = {
62+
name: false,
63+
age: false,
64+
hobbies: [{ name: false }, { name: false }],
65+
};
66+
expect(checkHasDirtyFields(dirtyFields)).toBe(false);
67+
});
68+
69+
it('should return true if any field is dirty on forms with nested array of objects', () => {
70+
const dirtyFields = {
71+
name: false,
72+
age: false,
73+
address: {
74+
street: false,
75+
city: [{ name: true }, { name: false }],
76+
},
77+
};
78+
expect(checkHasDirtyFields(dirtyFields)).toBe(true);
79+
});
80+
81+
it('should return false if no field is dirty on forms with nested array of objects', () => {
82+
const dirtyFields = {
83+
name: false,
84+
age: false,
85+
address: {
86+
street: false,
87+
city: [{ name: false }, { name: false }],
88+
},
89+
};
90+
expect(checkHasDirtyFields(dirtyFields)).toBe(false);
91+
});
92+
93+
// nested array of scalar values
94+
it('should return true if any field is dirty on forms with nested array of scalar values', () => {
95+
const dirtyFields = {
96+
name: false,
97+
age: false,
98+
hobbies: [
99+
{ name: false, tags: [true, true] },
100+
{ name: false, tags: [false, false] },
101+
],
102+
};
103+
expect(checkHasDirtyFields(dirtyFields)).toBe(true);
104+
});
105+
106+
it('should return false if no field is dirty on forms with nested array of scalar values', () => {
107+
const dirtyFields = {
108+
name: false,
109+
age: false,
110+
hobbies: [
111+
{ name: false, tags: [false, false] },
112+
{ name: false, tags: [false, false] },
113+
],
114+
};
115+
expect(checkHasDirtyFields(dirtyFields)).toBe(false);
116+
});
117+
});
118+
});

packages/ra-core/src/form/useFormIsDirty.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import isEmpty from 'lodash/isEmpty.js';
44
// useFormState().isDirty might differ from useFormState().dirtyFields (https://github.com/react-hook-form/react-hook-form/issues/4740)
55
export const useFormIsDirty = (): boolean => {
66
const { dirtyFields } = useFormState();
7-
return hasDirtyFields(dirtyFields);
7+
return checkHasDirtyFields(dirtyFields);
88
};
99

10-
const hasDirtyFields = (
10+
export const checkHasDirtyFields = (
1111
dirtyFields: Partial<
1212
Readonly<{
1313
[x: string]: any;
@@ -36,13 +36,13 @@ const hasDirtyFields = (
3636
if (
3737
typeof item === 'object' &&
3838
item !== null &&
39-
hasDirtyFields(item)
39+
checkHasDirtyFields(item)
4040
) {
4141
return true;
4242
}
4343
}
4444
} else if (typeof value === 'object' && value !== null) {
45-
return hasDirtyFields(value);
45+
return checkHasDirtyFields(value);
4646
}
4747
return false;
4848
});

0 commit comments

Comments
 (0)