-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFormikPersist.test.tsx
More file actions
176 lines (148 loc) · 4.51 KB
/
Copy pathFormikPersist.test.tsx
File metadata and controls
176 lines (148 loc) · 4.51 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { act, render, waitFor } from '@testing-library/react';
import { Formik, FormikProps } from 'formik';
import * as React from 'react';
import Persist from '../FormikPersist';
beforeEach(() => {
// values stored in tests will also be available in other tests unless you run
localStorage.clear();
sessionStorage.clear();
});
const formName = 'form-name';
const defaultState = {
values: { name: 'Name from local storage' },
errors: {},
touched: {},
isSubmitting: false,
isValidating: false,
submitCount: 0,
initialValues: { name: 'Test name' },
initialErrors: {},
initialTouched: {},
isValid: true,
dirty: true,
validateOnBlur: true,
validateOnChange: true,
validateOnMount: false,
};
test('attempts to rehydrate on mount', async () => {
let injected: FormikProps<{ name: string }>;
(localStorage.getItem as jest.Mock).mockReturnValueOnce(
JSON.stringify({
...defaultState,
values: { name: 'Name from local storage' },
})
);
render(
<Formik initialValues={{ name: 'Test name' }} onSubmit={jest.fn()}>
{(props: FormikProps<{ name: string }>) => {
injected = props;
return (
<div>
<Persist
name={formName}
debounceTime={0}
initialValues={{ name: '' }}
/>
</div>
);
}}
</Formik>
);
expect(localStorage.getItem).toHaveBeenCalled();
expect(injected!.values.name).toEqual('Name from local storage');
act(() => {
injected.setValues({ name: 'changed value' });
});
expect(injected!.values.name).toEqual('changed value');
await waitFor(() => {
expect(localStorage.setItem).toHaveBeenCalledWith(
formName,
JSON.stringify({ ...defaultState, values: { name: 'changed value' } })
);
});
});
test('attempts to rehydrate on mount if session storage is true on props', async () => {
let injected: FormikProps<{ name: string }>;
(sessionStorage.getItem as jest.Mock).mockReturnValueOnce(
JSON.stringify({
...defaultState,
values: { name: 'Name from session storage' },
})
);
render(
<Formik initialValues={{ name: 'Test name' }} onSubmit={jest.fn()}>
{(props: FormikProps<{ name: string }>) => {
injected = props;
return (
<div>
<Persist
name={formName}
debounceTime={0}
isSessionStorage={true}
initialValues={{ name: 'Name from local storage' }}
/>
</div>
);
}}
</Formik>
);
expect(sessionStorage.getItem).toHaveBeenCalled();
expect(injected!.values.name).toEqual('Name from session storage');
act(() => {
injected.setValues({ name: 'changed value' });
});
expect(injected!.values.name).toEqual('changed value');
await waitFor(() => {
expect(sessionStorage.setItem).toHaveBeenCalledWith(
formName,
JSON.stringify({ ...defaultState, values: { name: 'changed value' } })
);
});
});
test('alwaysFreshFields resets listed fields to their initial values', async () => {
let injected: FormikProps<{ regularField: string; freshField: string }>;
(localStorage.getItem as jest.Mock).mockReturnValueOnce(
JSON.stringify({
...defaultState,
values: {
regularField: 'Persisted value of regular field',
freshField: 'Persisted value of fresh field',
},
})
);
render(
<Formik
initialValues={{
regularField: 'Initial value of regular field in Formik',
freshField: 'Initial value of fresh field in Formik',
}}
onSubmit={jest.fn()}
>
{(props: FormikProps<{ regularField: string; freshField: string }>) => {
injected = props;
return (
<div>
<Persist
name={formName}
debounceTime={0}
initialValues={{
regularField: 'Initial value of regular field in Persist',
freshField: 'Initial value of fresh field in Persist',
}}
alwaysFreshFields={['freshField']}
/>
</div>
);
}}
</Formik>
);
expect(localStorage.getItem).toHaveBeenCalled();
// regularField is not in alwaysFreshFields, so it should be restored from storage
expect(injected!.values.regularField).toEqual(
'Persisted value of regular field'
);
// freshField is in alwaysFreshFields, so it must be reset to its initial value:
expect(injected!.values.freshField).toEqual(
'Initial value of fresh field in Persist'
);
});