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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/**
* @jest-environment jsdom
*/
import { renderHook, act } from "@testing-library/react-hooks";
import useLocalState from "../src";
beforeEach(() => {
localStorage.clear();
});
describe("useLocalState()", () => {
it("stores initial value as string", async () => {
const key = "key";
const value = "something";
const { result } = renderHook(() => useLocalState(key, value));
const [values] = result.current;
expect(values).toEqual(value);
});
it(`initial value isn't stored into localStorage`, async () => {
const key = "key";
const value = "something";
renderHook(() => useLocalState(key, value));
expect(localStorage.getItem(key)).toEqual(null);
});
it("stores initial value as boolean", async () => {
const key = "key";
const value = false;
const { result } = renderHook(() => useLocalState(key, value));
const [values] = result.current;
expect(values).toEqual(value);
});
it("stores initial value as object", async () => {
const key = "key";
const value = {
something: "else",
};
const { result } = renderHook(() => useLocalState(key, value));
const [values] = result.current;
expect(values).toEqual(value);
});
it("stores initial value as array", async () => {
const key = "todos";
const values = ["first", "second"];
const { result } = renderHook(() => useLocalState(key, values));
const [todos] = result.current;
expect(todos).toEqual(values);
});
it("accepts callback as an initial value", async () => {
const key = "todos";
const values = ["first", "second"];
const callback = () => values;
const { result } = renderHook(() => useLocalState(key, callback));
const [todos] = result.current;
expect(todos).toEqual(values);
});
it("accepts callback as a initial value and can be updated", async () => {
const key = "todos";
const values = ["first", "second"];
const callback = () => values;
const { result } = renderHook(() => useLocalState(key, callback));
const [initialValues] = result.current;
expect(initialValues).toEqual(values);
const newValue = ["third", "fourth"];
act(() => {
const setValue = result.current[1];
setValue(newValue);
});
const [changedValues] = result.current;
expect(changedValues).toEqual(newValue);
});
it("can update value as string", async () => {
const key = "key";
const value = "something";
const { result } = renderHook(() => useLocalState(key, value));
const [initialValue] = result.current;
expect(initialValue).toEqual(value);
const newValue = "something else";
act(() => {
const setValue = result.current[1];
setValue(newValue);
});
const [values] = result.current;
expect(values).toEqual(newValue);
});
it("can update value as function", async () => {
const key = "key";
const value = "something";
const { result } = renderHook(() => useLocalState(key, value));
const [initialValue] = result.current;
expect(initialValue).toEqual(value);
const newValue = " else";
act(() => {
const setValue = result.current[1];
setValue((v) => v + newValue);
});
const [values] = result.current;
expect(values).toEqual("something else");
});
it("can update value as function multiple times", async () => {
const key = "key";
const value = "something";
const { result } = renderHook(() => useLocalState(key, value));
const [initialValue] = result.current;
expect(initialValue).toEqual(value);
const newValue = " else";
act(() => {
const setValue = result.current[1];
setValue((v) => v + newValue);
});
const newValue2 = " again";
act(() => {
const setValue = result.current[1];
setValue((v) => v + newValue2);
});
const [values] = result.current;
expect(values).toEqual("something else again");
});
it("can update value as object", async () => {
const key = "key";
const value = { something: "something" };
const { result } = renderHook(() => useLocalState(key, value));
const [initialValue] = result.current;
expect(initialValue).toEqual(value);
const newValue = { something: "else" };
act(() => {
const setValue = result.current[1];
setValue(newValue);
});
const [values] = result.current;
expect(values).toEqual(newValue);
});
it("can update value as array", async () => {
const key = "todos";
const values = ["first", "second"];
const { result } = renderHook(() => useLocalState(key, values));
const newValues = ["third", "fourth"];
act(() => {
const setValues = result.current[1];
setValues(newValues);
});
const [todos] = result.current;
expect(todos).toEqual(newValues);
});
it("updates state with callback function", async () => {
const key = "todos";
const values = ["first", "second"];
const { result } = renderHook(() => useLocalState(key, values));
const newValues = ["first", "second"];
act(() => {
const setTodos = result.current[1];
setTodos((current) => [...current, ...newValues]);
});
const [todos] = result.current;
expect(todos).toEqual([...values, ...newValues]);
});
it("does not fail even if invalid data is stored into localStorage", async () => {
const key = "todos";
const value = "something";
localStorage.setItem(key, value);
const newValues = ["first", "second"];
const { result } = renderHook(() => useLocalState(key, newValues));
const [todos] = result.current;
expect(todos).toEqual(newValues);
});
it("gets initial value from localStorage if there is a value", async () => {
const key = "todos";
const values = ["first", "second"];
localStorage.setItem(key, JSON.stringify(values));
const newValues = ["third", "fourth"];
const { result } = renderHook(() => useLocalState(key, newValues));
const [todos] = result.current;
expect(todos).toEqual(values);
});
it("does not throw an error with two states with different keys", async () => {
const keyA = "todos";
const keyB = "todos";
const valuesA = ["first", "second"];
const valuesB = ["third", "fourth"];
expect(() => {
renderHook(() => {
useLocalState(keyA, valuesA);
useLocalState(keyB, valuesB);
});
}).not.toThrow();
});
it("can handle `undefined` values", async () => {
const key = "todos";
const values = ["first", "second"];
const { result } = renderHook(() =>
useLocalState<string[] | undefined>(key, values)
);
act(() => {
const [, setValues] = result.current;
setValues(undefined);
});
const [value] = result.current;
expect(value).toBe(undefined);
});
it("can handle `null` values", async () => {
const key = "todos";
const values = ["first", "second"];
const { result } = renderHook(() =>
useLocalState<string[] | null>(key, values)
);
act(() => {
const [, setValues] = result.current;
setValues(null);
});
const [value] = result.current;
expect(value).toBe(null);
});
it("can reset to default value", async () => {
const key = "key";
const value = "something";
const { result } = renderHook(() => useLocalState(key, value));
const [initialValue] = result.current;
expect(initialValue).toEqual(value);
const newValue = "something else";
act(() => {
const setValue = result.current[1];
setValue(newValue);
});
const [changedValue] = result.current;
expect(changedValue).toEqual(newValue);
act(() => {
const resetValue = result.current[2];
resetValue();
});
const [resetValue] = result.current;
expect(resetValue).toEqual(value);
expect(localStorage.getItem(key)).toEqual(null);
});
it("can reset to default value as callback", async () => {
const key = "todos";
const values = ["first", "second"];
const callback = () => values;
const { result } = renderHook(() => useLocalState(key, callback));
const [initialValues] = result.current;
expect(initialValues).toEqual(values);
const newValues = ["third", "fourth"];
act(() => {
const setValues = result.current[1];
setValues(newValues);
});
const [changedValues] = result.current;
expect(changedValues).toEqual(newValues);
act(() => {
const resetValues = result.current[2];
resetValues();
});
const [resetValues] = result.current;
expect(resetValues).toEqual(values);
expect(localStorage.getItem(key)).toEqual(null);
});
it("does not change the setter when value is updated", async () => {
const key = "key";
const value = "something";
const { result } = renderHook(() => useLocalState(key, value));
const setterOne = result.current[1];
const newValue = "something else";
act(() => {
const setValue = result.current[1];
setValue(newValue);
});
const setterTwo = result.current[1];
expect(setterOne).toEqual(setterTwo);
});
it("does not change the setter when value is updated via function", async () => {
const key = "key";
const value = "something";
const { result } = renderHook(() => useLocalState(key, value));
const setterOne = result.current[1];
const newValue = " else";
act(() => {
const setValue = result.current[1];
setValue((v) => v + newValue);
});
const setterTwo = result.current[1];
expect(setterOne).toEqual(setterTwo);
});
});
|