|
1 | 1 | import { renderHook, act } from "@testing-library/react-hooks";
|
2 | 2 |
|
3 | 3 | import useLocalState from "../src";
|
| 4 | +import { SUPPORTED } from "../src/utils"; |
4 | 5 |
|
5 |
| -describe("performs requests", () => { |
| 6 | +beforeEach(() => { |
| 7 | + if (SUPPORTED) localStorage.clear(); |
| 8 | +}); |
| 9 | + |
| 10 | +describe("useLocalState()", () => { |
6 | 11 | it("uses initial value as string", async () => {
|
7 |
| - const key = "key 0"; |
8 |
| - const value = "something 0"; |
| 12 | + const key = "key"; |
| 13 | + const value = "something"; |
9 | 14 | const { result } = renderHook(() => useLocalState(key, value));
|
10 |
| - expect(result.current[0]).toEqual(value); |
| 15 | + |
| 16 | + const [values] = result.current; |
| 17 | + expect(values).toEqual(value); |
| 18 | + }); |
| 19 | + |
| 20 | + it(`initial value isn't written into localStorage`, async () => { |
| 21 | + if (!SUPPORTED) return; |
| 22 | + |
| 23 | + const key = "key"; |
| 24 | + const value = "something"; |
| 25 | + renderHook(() => useLocalState(key, value)); |
| 26 | + |
| 27 | + expect(localStorage.getItem(key)).toEqual(null); |
11 | 28 | });
|
12 | 29 |
|
13 | 30 | it("uses initial value as boolean", async () => {
|
14 |
| - const key = "key1"; |
| 31 | + const key = "key"; |
15 | 32 | const value = false;
|
16 | 33 | const { result } = renderHook(() => useLocalState(key, value));
|
17 |
| - expect(result.current[0]).toEqual(value); |
| 34 | + |
| 35 | + const [values] = result.current; |
| 36 | + expect(values).toEqual(value); |
18 | 37 | });
|
19 | 38 |
|
20 | 39 | it("uses initial value as object", async () => {
|
21 |
| - const key = "key2"; |
| 40 | + const key = "key"; |
22 | 41 | const value = {
|
23 | 42 | something: "else",
|
24 | 43 | };
|
25 | 44 | const { result } = renderHook(() => useLocalState(key, value));
|
26 |
| - expect(result.current[0]).toEqual(value); |
| 45 | + |
| 46 | + const [values] = result.current; |
| 47 | + expect(values).toEqual(value); |
| 48 | + }); |
| 49 | + |
| 50 | + it("uses initial value as array", async () => { |
| 51 | + const key = "todos"; |
| 52 | + const values = ["first", "second"]; |
| 53 | + const { result } = renderHook(() => useLocalState(key, values)); |
| 54 | + |
| 55 | + const [todos] = result.current; |
| 56 | + expect(todos).toEqual(values); |
| 57 | + }); |
| 58 | + |
| 59 | + it("accepts callback as a value", async () => { |
| 60 | + const key = "todos"; |
| 61 | + const values = ["first", "second"]; |
| 62 | + const callback = () => values; |
| 63 | + const { result } = renderHook(() => useLocalState(key, callback)); |
| 64 | + |
| 65 | + const [todos] = result.current; |
| 66 | + expect(todos).toEqual(values); |
27 | 67 | });
|
28 | 68 |
|
29 | 69 | it("can update value as string", async () => {
|
30 |
| - const key = "key3"; |
31 |
| - const value = "something 3"; |
32 |
| - const value2 = "something else 3"; |
| 70 | + const key = "key"; |
| 71 | + const value = "something"; |
33 | 72 |
|
34 | 73 | const { result } = renderHook(() => useLocalState(key, value));
|
35 | 74 | expect(result.current[0]).toEqual(value);
|
36 | 75 |
|
| 76 | + const newValue = "something else"; |
37 | 77 | act(() => {
|
38 |
| - result.current[1](value2); |
| 78 | + const setValue = result.current[1]; |
| 79 | + |
| 80 | + setValue(newValue); |
39 | 81 | });
|
40 | 82 |
|
41 |
| - expect(result.current[0]).toEqual(value2); |
| 83 | + const [values] = result.current; |
| 84 | + expect(values).toEqual(newValue); |
42 | 85 | });
|
43 | 86 |
|
44 | 87 | it("can update value as object", async () => {
|
45 |
| - const key = "key4"; |
46 |
| - const value = { something: "something 4" }; |
47 |
| - const value2 = { something: "else 4" }; |
| 88 | + const key = "key"; |
| 89 | + const value = { something: "something" }; |
48 | 90 |
|
49 | 91 | const { result } = renderHook(() => useLocalState(key, value));
|
50 | 92 | expect(result.current[0]).toEqual(value);
|
51 | 93 |
|
| 94 | + const newValue = { something: "else" }; |
| 95 | + act(() => { |
| 96 | + const setValue = result.current[1]; |
| 97 | + |
| 98 | + setValue(newValue); |
| 99 | + }); |
| 100 | + |
| 101 | + const [values] = result.current; |
| 102 | + expect(values).toEqual(newValue); |
| 103 | + }); |
| 104 | + |
| 105 | + it("can update value as list", async () => { |
| 106 | + const key = "todos"; |
| 107 | + const values = ["first", "second"]; |
| 108 | + const { result } = renderHook(() => useLocalState(key, values)); |
| 109 | + |
| 110 | + const newValues = ["third", "fourth"]; |
| 111 | + act(() => { |
| 112 | + const setValues = result.current[1]; |
| 113 | + |
| 114 | + setValues(newValues); |
| 115 | + }); |
| 116 | + |
| 117 | + const [todos] = result.current; |
| 118 | + expect(todos).toEqual(newValues); |
| 119 | + }); |
| 120 | + |
| 121 | + // todo(): this logic could be super handy... |
| 122 | + // it("updates state with callback function", () => { |
| 123 | + // const key = "todos"; |
| 124 | + // const values = ["first", "second"]; |
| 125 | + // const { result } = renderHook(() => useLocalState(key, values)); |
| 126 | + |
| 127 | + // const newValues = ["first", "second"]; |
| 128 | + // act(() => { |
| 129 | + // const setTodos = result.current[1]; |
| 130 | + |
| 131 | + // setTodos((current) => [...current, ...newValues]); |
| 132 | + // }); |
| 133 | + |
| 134 | + // const [todos] = result.current; |
| 135 | + // expect(todos).toEqual([...values, ...newValues]); |
| 136 | + // }); |
| 137 | + |
| 138 | + it("does not fail even if invalid data is stored into localStorage", async () => { |
| 139 | + if (!SUPPORTED) return; |
| 140 | + |
| 141 | + const key = "todos"; |
| 142 | + const value = "something"; |
| 143 | + |
| 144 | + localStorage.setItem(key, value); |
| 145 | + |
| 146 | + const newValues = ["first", "second"]; |
| 147 | + const { result } = renderHook(() => useLocalState(key, newValues)); |
| 148 | + |
| 149 | + const [todos] = result.current; |
| 150 | + expect(todos).toEqual(newValues); |
| 151 | + }); |
| 152 | + |
| 153 | + it("gets initial value from localStorage if there is a value", async () => { |
| 154 | + if (!SUPPORTED) return; |
| 155 | + |
| 156 | + const key = "todos"; |
| 157 | + const values = ["first", "second"]; |
| 158 | + |
| 159 | + localStorage.setItem(key, JSON.stringify(values)); |
| 160 | + |
| 161 | + const { result } = renderHook(() => useLocalState(key, values)); |
| 162 | + |
| 163 | + const [todos] = result.current; |
| 164 | + expect(todos).toEqual(values); |
| 165 | + }); |
| 166 | + |
| 167 | + it("throws an error on two states with the same key", async () => { |
| 168 | + const consoleError = console.error; |
| 169 | + console.error = () => null; |
| 170 | + |
| 171 | + const key = "todos"; |
| 172 | + const valuesA = ["first", "second"]; |
| 173 | + const valuesB = ["third", "fourth"]; |
| 174 | + |
| 175 | + expect(() => { |
| 176 | + renderHook(() => { |
| 177 | + useLocalState(key, valuesA); |
| 178 | + useLocalState(key, valuesB); |
| 179 | + }); |
| 180 | + }).toThrow(); |
| 181 | + |
| 182 | + console.error = consoleError; |
| 183 | + }); |
| 184 | + |
| 185 | + it("does not throw an error with two states with different keys", async () => { |
| 186 | + const keyA = "todos"; |
| 187 | + const keyB = "todos"; |
| 188 | + const valuesA = ["first", "second"]; |
| 189 | + const valuesB = ["third", "fourth"]; |
| 190 | + |
| 191 | + expect(() => { |
| 192 | + renderHook(() => { |
| 193 | + useLocalState(keyA, valuesA); |
| 194 | + useLocalState(keyB, valuesB); |
| 195 | + }); |
| 196 | + }).not.toThrow(); |
| 197 | + }); |
| 198 | + |
| 199 | + it("can handle `undefined` values", async () => { |
| 200 | + const key = "todos"; |
| 201 | + const values = ["first", "second"]; |
| 202 | + |
| 203 | + const { result } = renderHook(() => |
| 204 | + useLocalState<string[] | undefined>(key, values) |
| 205 | + ); |
| 206 | + |
| 207 | + act(() => { |
| 208 | + const [, setValues] = result.current; |
| 209 | + setValues(undefined); |
| 210 | + }); |
| 211 | + |
| 212 | + const [value] = result.current; |
| 213 | + expect(value).toBe(undefined); |
| 214 | + }); |
| 215 | + |
| 216 | + it("can handle `null` values", async () => { |
| 217 | + const key = "todos"; |
| 218 | + const values = ["first", "second"]; |
| 219 | + |
| 220 | + const { result } = renderHook(() => |
| 221 | + useLocalState<string[] | null>(key, values) |
| 222 | + ); |
| 223 | + |
52 | 224 | act(() => {
|
53 |
| - result.current[1](value2); |
| 225 | + const [, setValues] = result.current; |
| 226 | + setValues(null); |
54 | 227 | });
|
55 | 228 |
|
56 |
| - expect(result.current[0]).toEqual(value2); |
| 229 | + const [value] = result.current; |
| 230 | + expect(value).toBe(null); |
57 | 231 | });
|
58 | 232 | });
|
0 commit comments