/
dirty.test.ts
62 lines (50 loc) · 2.56 KB
/
dirty.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { act, renderHook } from '@testing-library/react';
import { useForm } from '../use-form';
describe('@mantine/form/dirty', () => {
it('accepts initial dirty state', () => {
const hook = renderHook(() => useForm({ initialDirty: { a: true, b: false } }));
expect(hook.result.current.isDirty('a')).toBe(true);
expect(hook.result.current.isDirty('b')).toBe(false);
expect(hook.result.current.isDirty()).toBe(true);
});
it('sets field as dirty if value changes', () => {
const hook = renderHook(() => useForm({ initialValues: { a: 1 } }));
expect(hook.result.current.isDirty('a')).toBe(false);
expect(hook.result.current.isDirty()).toBe(false);
act(() => hook.result.current.setFieldValue('a', 5));
expect(hook.result.current.isDirty('a')).toBe(true);
expect(hook.result.current.isDirty()).toBe(true);
act(() => hook.result.current.setFieldValue('a', 1));
expect(hook.result.current.isDirty('a')).toBe(false);
expect(hook.result.current.isDirty()).toBe(false);
});
it('allows to set dirty state with setDirty handler', () => {
const hook = renderHook(() => useForm());
expect(hook.result.current.isDirty()).toBe(false);
expect(hook.result.current.isDirty('a')).toBe(false);
act(() => hook.result.current.setDirty({ a: true }));
expect(hook.result.current.isDirty()).toBe(true);
expect(hook.result.current.isDirty('a')).toBe(true);
});
it('resets status with resetDirty handler', () => {
const hook = renderHook(() => useForm({ initialDirty: { a: true } }));
expect(hook.result.current.isDirty()).toBe(true);
act(() => hook.result.current.resetDirty());
expect(hook.result.current.isDirty()).toBe(false);
});
it('sets list field as dirty if list item changes', () => {
const hook = renderHook(() => useForm({ initialValues: { a: [{ b: 1 }, { b: 2 }] } }));
act(() => hook.result.current.setFieldValue('a.0', 3));
expect(hook.result.current.isDirty('a.0')).toBe(true);
expect(hook.result.current.isDirty('a')).toBe(true);
act(() => hook.result.current.setFieldValue('a', [{ b: 1 }, { b: 2 }]));
expect(hook.result.current.isDirty('a.0')).toBe(false);
expect(hook.result.current.isDirty('a')).toBe(false);
act(() => hook.result.current.insertListItem('a', [{ b: 3 }]));
expect(hook.result.current.isDirty('a.2')).toBe(true);
expect(hook.result.current.isDirty('a')).toBe(true);
act(() => hook.result.current.removeListItem('a', 2));
expect(hook.result.current.isDirty('a.2')).toBe(false);
expect(hook.result.current.isDirty('a')).toBe(false);
});
});