| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Atom} from 'jotai'; |
| b69ab31 | | | 9 | import type {AtomFamilyWeak} from '../jotaiUtils'; |
| b69ab31 | | | 10 | import type {Platform} from '../platform'; |
| b69ab31 | | | 11 | import type {LocalStorageName} from '../types'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import {act, render} from '@testing-library/react'; |
| b69ab31 | | | 14 | import {List} from 'immutable'; |
| b69ab31 | | | 15 | import {Provider, atom, createStore, useAtomValue} from 'jotai'; |
| b69ab31 | | | 16 | import {StrictMode} from 'react'; |
| b69ab31 | | | 17 | import {SelfUpdate} from 'shared/immutableExt'; |
| b69ab31 | | | 18 | import {gc, nextTick} from 'shared/testUtils'; |
| b69ab31 | | | 19 | import { |
| b69ab31 | | | 20 | atomFamilyWeak, |
| b69ab31 | | | 21 | atomResetOnDepChange, |
| b69ab31 | | | 22 | lazyAtom, |
| b69ab31 | | | 23 | localStorageBackedAtomFamily, |
| b69ab31 | | | 24 | readAtom, |
| b69ab31 | | | 25 | useAtomGet, |
| b69ab31 | | | 26 | useAtomHas, |
| b69ab31 | | | 27 | writeAtom, |
| b69ab31 | | | 28 | } from '../jotaiUtils'; |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | class Foo extends SelfUpdate<List<number>> {} |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | const testFooAtom = atom<Foo>(new Foo(List())); |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | describe('Jotai compatibility', () => { |
| b69ab31 | | | 35 | it('does not freeze SelfUpdate types', () => { |
| b69ab31 | | | 36 | function MyTestComponent() { |
| b69ab31 | | | 37 | const foo2 = useAtomValue(testFooAtom); |
| b69ab31 | | | 38 | expect(Object.isSealed(foo2)).toBe(false); |
| b69ab31 | | | 39 | expect(Object.isFrozen(foo2)).toBe(false); |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | return null; |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | render( |
| b69ab31 | | | 44 | <Provider> |
| b69ab31 | | | 45 | <MyTestComponent /> |
| b69ab31 | | | 46 | </Provider>, |
| b69ab31 | | | 47 | ); |
| b69ab31 | | | 48 | }); |
| b69ab31 | | | 49 | }); |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | describe('lazyAtom', () => { |
| b69ab31 | | | 52 | it('returns sync load() value', () => { |
| b69ab31 | | | 53 | const a = lazyAtom(() => 1, 2); |
| b69ab31 | | | 54 | expect(readAtom(a)).toBe(1); |
| b69ab31 | | | 55 | }); |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | it('returns fallback value and sets async load() value', async () => { |
| b69ab31 | | | 58 | const a = lazyAtom(() => Promise.resolve(1), 2); |
| b69ab31 | | | 59 | expect(readAtom(a)).toBe(2); |
| b69ab31 | | | 60 | await nextTick(); |
| b69ab31 | | | 61 | expect(readAtom(a)).toBe(1); |
| b69ab31 | | | 62 | }); |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | it('can depend on another atom', () => { |
| b69ab31 | | | 65 | const a = atom<number | undefined>(undefined); |
| b69ab31 | | | 66 | const b = lazyAtom(() => 2, 2, a); |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | expect(readAtom(a)).toBe(undefined); |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | // Reading `b` triggers updating `a`. |
| b69ab31 | | | 71 | expect(readAtom(b)).toBe(2); |
| b69ab31 | | | 72 | expect(readAtom(a)).toBe(2); |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | // If `a` is updated to be not `undefined`, `b` will be the same value. |
| b69ab31 | | | 75 | writeAtom(a, 3); |
| b69ab31 | | | 76 | expect(readAtom(b)).toBe(3); |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | // Updating `b` updates `a` too. |
| b69ab31 | | | 79 | writeAtom(b, 4); |
| b69ab31 | | | 80 | expect(readAtom(a)).toBe(4); |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | // If `a` is updated to be `undefined`, `b` will be the fallback value. |
| b69ab31 | | | 83 | writeAtom(a, undefined); |
| b69ab31 | | | 84 | expect(readAtom(b)).toBe(2); |
| b69ab31 | | | 85 | }); |
| b69ab31 | | | 86 | }); |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | describe('atomFamilyWeak', () => { |
| b69ab31 | | | 89 | let recalcFamilyCount = 0; |
| b69ab31 | | | 90 | let recalcAtomCount = 0; |
| b69ab31 | | | 91 | const family = atomFamilyWeak((key: number) => { |
| b69ab31 | | | 92 | recalcFamilyCount += 1; |
| b69ab31 | | | 93 | return atom(_get => { |
| b69ab31 | | | 94 | recalcAtomCount += 1; |
| b69ab31 | | | 95 | return key; |
| b69ab31 | | | 96 | }); |
| b69ab31 | | | 97 | }); |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | let store = createStore(); |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | function TestComponent({k, f}: {k: number; f?: AtomFamilyWeak<number, Atom<number>>}) { |
| b69ab31 | | | 102 | const value = useAtomValue((f ?? family)(k)); |
| b69ab31 | | | 103 | return <div>{value}</div>; |
| b69ab31 | | | 104 | } |
| b69ab31 | | | 105 | function TestApp({ |
| b69ab31 | | | 106 | keys, |
| b69ab31 | | | 107 | family, |
| b69ab31 | | | 108 | }: { |
| b69ab31 | | | 109 | keys: Array<number>; |
| b69ab31 | | | 110 | family?: AtomFamilyWeak<number, Atom<number>>; |
| b69ab31 | | | 111 | }) { |
| b69ab31 | | | 112 | return ( |
| b69ab31 | | | 113 | <Provider store={store}> |
| b69ab31 | | | 114 | {keys.map(k => ( |
| b69ab31 | | | 115 | <TestComponent k={k} key={k} f={family} /> |
| b69ab31 | | | 116 | ))} |
| b69ab31 | | | 117 | </Provider> |
| b69ab31 | | | 118 | ); |
| b69ab31 | | | 119 | } |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | beforeEach(() => { |
| b69ab31 | | | 122 | store = createStore(); |
| b69ab31 | | | 123 | family.clear(); |
| b69ab31 | | | 124 | recalcFamilyCount = 0; |
| b69ab31 | | | 125 | recalcAtomCount = 0; |
| b69ab31 | | | 126 | }); |
| b69ab31 | | | 127 | |
| b69ab31 | | | 128 | it('with "strong" cache enabled (default), unmount/remount skips re-calculate', async () => { |
| b69ab31 | | | 129 | const rendered = render(<TestApp keys={[1, 2, 3]} />); |
| b69ab31 | | | 130 | expect(recalcFamilyCount).toBe(3); |
| b69ab31 | | | 131 | expect(recalcAtomCount).toBe(3); |
| b69ab31 | | | 132 | |
| b69ab31 | | | 133 | rendered.rerender(<TestApp keys={[1, 2, 3]} />); |
| b69ab31 | | | 134 | expect(recalcFamilyCount).toBe(3); |
| b69ab31 | | | 135 | expect(recalcAtomCount).toBe(3); |
| b69ab31 | | | 136 | |
| b69ab31 | | | 137 | // After unmount, part of the atomFamily cache can still be used when re-mounting. |
| b69ab31 | | | 138 | rendered.unmount(); |
| b69ab31 | | | 139 | await gc(); |
| b69ab31 | | | 140 | |
| b69ab31 | | | 141 | render(<TestApp keys={[3, 2, 1]} />); |
| b69ab31 | | | 142 | expect(recalcFamilyCount).toBeLessThan(6); |
| b69ab31 | | | 143 | expect(recalcAtomCount).toBeLessThan(6); |
| b69ab31 | | | 144 | }); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | it('with "strong" cache disabled, unmount/remount might re-calculate', async () => { |
| b69ab31 | | | 147 | let count = 0; |
| b69ab31 | | | 148 | const family = atomFamilyWeak( |
| b69ab31 | | | 149 | (key: number) => { |
| b69ab31 | | | 150 | count += 1; |
| b69ab31 | | | 151 | return atom(_get => key); |
| b69ab31 | | | 152 | }, |
| b69ab31 | | | 153 | {useStrongCache: false}, |
| b69ab31 | | | 154 | ); |
| b69ab31 | | | 155 | |
| b69ab31 | | | 156 | const rendered = render(<TestApp keys={[1, 2, 3]} family={family} />); |
| b69ab31 | | | 157 | expect(count).toBe(3); |
| b69ab31 | | | 158 | |
| b69ab31 | | | 159 | rendered.unmount(); |
| b69ab31 | | | 160 | await gc(); |
| b69ab31 | | | 161 | |
| b69ab31 | | | 162 | render(<TestApp keys={[1, 2, 3]} family={family} />); |
| b69ab31 | | | 163 | expect(count).toBe(6); |
| b69ab31 | | | 164 | }); |
| b69ab31 | | | 165 | |
| b69ab31 | | | 166 | it('"cleanup" does not clean in-use states', () => { |
| b69ab31 | | | 167 | const rendered = render(<TestApp keys={[1, 2, 3]} />); |
| b69ab31 | | | 168 | expect(recalcFamilyCount).toBe(3); |
| b69ab31 | | | 169 | expect(recalcAtomCount).toBe(3); |
| b69ab31 | | | 170 | family.cleanup(); |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | // re-render can still use cached state after "cleanup" (count remains the same). |
| b69ab31 | | | 173 | rendered.rerender(<TestApp keys={[1, 2, 3]} />); |
| b69ab31 | | | 174 | expect(recalcFamilyCount).toBe(3); |
| b69ab31 | | | 175 | expect(recalcAtomCount).toBe(3); |
| b69ab31 | | | 176 | }); |
| b69ab31 | | | 177 | |
| b69ab31 | | | 178 | it('"cleanup" can release memory', async () => { |
| b69ab31 | | | 179 | const rendered = render(<TestApp keys={[1, 2, 3]} />); |
| b69ab31 | | | 180 | expect(recalcFamilyCount).toBe(3); |
| b69ab31 | | | 181 | expect(recalcAtomCount).toBe(3); |
| b69ab31 | | | 182 | rendered.unmount(); |
| b69ab31 | | | 183 | |
| b69ab31 | | | 184 | await gc(); |
| b69ab31 | | | 185 | family.cleanup(); |
| b69ab31 | | | 186 | await gc(); |
| b69ab31 | | | 187 | family.cleanup(); |
| b69ab31 | | | 188 | |
| b69ab31 | | | 189 | // umount, then re-render will recalculate all atoms (count increases). |
| b69ab31 | | | 190 | render(<TestApp keys={[3, 2, 1]} />); |
| b69ab31 | | | 191 | expect(recalcFamilyCount).toBe(6); |
| b69ab31 | | | 192 | expect(recalcAtomCount).toBe(6); |
| b69ab31 | | | 193 | }); |
| b69ab31 | | | 194 | |
| b69ab31 | | | 195 | it('"clear" releases memory', () => { |
| b69ab31 | | | 196 | const rendered = render(<TestApp keys={[1, 2, 3]} />); |
| b69ab31 | | | 197 | expect(recalcFamilyCount).toBe(3); |
| b69ab31 | | | 198 | expect(recalcAtomCount).toBe(3); |
| b69ab31 | | | 199 | family.clear(); |
| b69ab31 | | | 200 | |
| b69ab31 | | | 201 | // re-render will recalculate all atoms. |
| b69ab31 | | | 202 | rendered.rerender(<TestApp keys={[1, 2, 3]} />); |
| b69ab31 | | | 203 | expect(recalcFamilyCount).toBe(6); |
| b69ab31 | | | 204 | expect(recalcAtomCount).toBe(6); |
| b69ab31 | | | 205 | rendered.unmount(); |
| b69ab31 | | | 206 | family.clear(); |
| b69ab31 | | | 207 | |
| b69ab31 | | | 208 | // umount, then re-render will recalculate all atoms (count increases). |
| b69ab31 | | | 209 | render(<TestApp keys={[3, 2, 1]} />); |
| b69ab31 | | | 210 | expect(recalcFamilyCount).toBe(9); |
| b69ab31 | | | 211 | expect(recalcAtomCount).toBe(9); |
| b69ab31 | | | 212 | }); |
| b69ab31 | | | 213 | |
| b69ab31 | | | 214 | it('"cleanup" runs automatically to reduce cache size', async () => { |
| b69ab31 | | | 215 | const N = 10; |
| b69ab31 | | | 216 | const M = 30; |
| b69ab31 | | | 217 | |
| b69ab31 | | | 218 | // Render N items. |
| b69ab31 | | | 219 | const rendered = render(<TestApp keys={Array.from({length: N}, (_, i) => i)} />); |
| b69ab31 | | | 220 | |
| b69ab31 | | | 221 | // Umount to drop references to the atoms. |
| b69ab31 | | | 222 | rendered.unmount(); |
| b69ab31 | | | 223 | |
| b69ab31 | | | 224 | // Force GC to run to stabilize the test. |
| b69ab31 | | | 225 | await gc(); |
| b69ab31 | | | 226 | |
| b69ab31 | | | 227 | // After GC, render M items with different keys. |
| b69ab31 | | | 228 | // This would trigger `family.cleanup` transparently. |
| b69ab31 | | | 229 | render(<TestApp keys={Array.from({length: M}, (_, i) => N + i)} />); |
| b69ab31 | | | 230 | |
| b69ab31 | | | 231 | // Neither of the caches should have N + M items (which means no cleanup). |
| b69ab31 | | | 232 | expect(family.weakCache.size).toBeLessThan(N + M); |
| b69ab31 | | | 233 | expect(family.strongCache.size).toBeLessThan(N + M); |
| b69ab31 | | | 234 | }); |
| b69ab31 | | | 235 | |
| b69ab31 | | | 236 | it('provides debugLabel', () => { |
| b69ab31 | | | 237 | const family = atomFamilyWeak((v: string) => atom(v)); |
| b69ab31 | | | 238 | family.debugLabel = 'prefix1'; |
| b69ab31 | | | 239 | const atom1 = family('a'); |
| b69ab31 | | | 240 | expect(atom1.debugLabel).toBe('prefix1:a'); |
| b69ab31 | | | 241 | }); |
| b69ab31 | | | 242 | }); |
| b69ab31 | | | 243 | |
| b69ab31 | | | 244 | describe('useAtomGet and useAtomSet', () => { |
| b69ab31 | | | 245 | const initialMap = new Map([ |
| b69ab31 | | | 246 | ['a', 1], |
| b69ab31 | | | 247 | ['b', 2], |
| b69ab31 | | | 248 | ['c', 3], |
| b69ab31 | | | 249 | ]); |
| b69ab31 | | | 250 | const initialSet = new Set(['a', 'b']); |
| b69ab31 | | | 251 | |
| b69ab31 | | | 252 | // Render an App, change the map and set, check what |
| b69ab31 | | | 253 | // Runs a test and report re-render and atom states. |
| b69ab31 | | | 254 | // insertMap specifies changes to the map (initially {a: 1, b: 2, c: 3}). |
| b69ab31 | | | 255 | // changeSet specifies changes to the set (initially {a, b}). |
| b69ab31 | | | 256 | function findRerender(props: { |
| b69ab31 | | | 257 | insertMap?: Iterable<[string, number]>; |
| b69ab31 | | | 258 | replaceSet?: Iterable<string>; |
| b69ab31 | | | 259 | }): Array<string> { |
| b69ab31 | | | 260 | // container types |
| b69ab31 | | | 261 | const map = atom<Map<string, number>>(initialMap); |
| b69ab31 | | | 262 | const set = atom<Set<string>>(initialSet); |
| b69ab31 | | | 263 | const rerenderKeys = new Set<string>(); |
| b69ab31 | | | 264 | |
| b69ab31 | | | 265 | // test UI components |
| b69ab31 | | | 266 | function Item({k}: {k: string}) { |
| b69ab31 | | | 267 | const mapValue = useAtomGet(map, k); |
| b69ab31 | | | 268 | const setValue = useAtomHas(set, k); |
| b69ab31 | | | 269 | rerenderKeys.add(k); |
| b69ab31 | | | 270 | return ( |
| b69ab31 | | | 271 | <span> |
| b69ab31 | | | 272 | {mapValue} {setValue} |
| b69ab31 | | | 273 | </span> |
| b69ab31 | | | 274 | ); |
| b69ab31 | | | 275 | } |
| b69ab31 | | | 276 | |
| b69ab31 | | | 277 | const store = createStore(); |
| b69ab31 | | | 278 | |
| b69ab31 | | | 279 | function TestApp({keys}: {keys: Array<string>}) { |
| b69ab31 | | | 280 | return ( |
| b69ab31 | | | 281 | <StrictMode> |
| b69ab31 | | | 282 | <Provider store={store}> |
| b69ab31 | | | 283 | {keys.map(k => ( |
| b69ab31 | | | 284 | <Item k={k} key={k} /> |
| b69ab31 | | | 285 | ))} |
| b69ab31 | | | 286 | </Provider> |
| b69ab31 | | | 287 | </StrictMode> |
| b69ab31 | | | 288 | ); |
| b69ab31 | | | 289 | } |
| b69ab31 | | | 290 | |
| b69ab31 | | | 291 | const keys = ['a', 'b', 'c']; |
| b69ab31 | | | 292 | render(<TestApp keys={keys} />); |
| b69ab31 | | | 293 | |
| b69ab31 | | | 294 | rerenderKeys.clear(); |
| b69ab31 | | | 295 | |
| b69ab31 | | | 296 | const {insertMap, replaceSet} = props; |
| b69ab31 | | | 297 | |
| b69ab31 | | | 298 | act(() => { |
| b69ab31 | | | 299 | if (insertMap) { |
| b69ab31 | | | 300 | store.set(map, oldMap => new Map([...oldMap, ...insertMap])); |
| b69ab31 | | | 301 | } |
| b69ab31 | | | 302 | if (replaceSet) { |
| b69ab31 | | | 303 | const newSet = new Set([...replaceSet]); |
| b69ab31 | | | 304 | store.set(set, newSet); |
| b69ab31 | | | 305 | } |
| b69ab31 | | | 306 | }); |
| b69ab31 | | | 307 | |
| b69ab31 | | | 308 | return [...rerenderKeys]; |
| b69ab31 | | | 309 | } |
| b69ab31 | | | 310 | |
| b69ab31 | | | 311 | it('avoids re-rendering with changing to unrelated keys', () => { |
| b69ab31 | | | 312 | expect(findRerender({insertMap: [['unrelated-key', 3]]})).toEqual([]); |
| b69ab31 | | | 313 | expect(findRerender({replaceSet: [...initialSet, 'unrelated-key']})).toEqual([]); |
| b69ab31 | | | 314 | }); |
| b69ab31 | | | 315 | |
| b69ab31 | | | 316 | it('only re-render changed items', () => { |
| b69ab31 | | | 317 | const replaceSet = [...initialSet, 'c']; // add 'c' to the set. |
| b69ab31 | | | 318 | expect(findRerender({insertMap: [['b', 5]]})).toEqual(['b']); |
| b69ab31 | | | 319 | expect(findRerender({replaceSet})).toEqual(['c']); |
| b69ab31 | | | 320 | expect(findRerender({insertMap: [['b', 5]], replaceSet})).toEqual(['b', 'c']); |
| b69ab31 | | | 321 | }); |
| b69ab31 | | | 322 | }); |
| b69ab31 | | | 323 | |
| b69ab31 | | | 324 | describe('atomResetOnDepChange', () => { |
| b69ab31 | | | 325 | it('works like a primitive atom', () => { |
| b69ab31 | | | 326 | const depAtom = atom(0); |
| b69ab31 | | | 327 | const testAtom = atomResetOnDepChange(1, depAtom); |
| b69ab31 | | | 328 | const doubleAtom = atom(get => get(testAtom) * 2); |
| b69ab31 | | | 329 | expect(readAtom(doubleAtom)).toBe(2); |
| b69ab31 | | | 330 | expect(readAtom(testAtom)).toBe(1); |
| b69ab31 | | | 331 | writeAtom(testAtom, 2); |
| b69ab31 | | | 332 | expect(readAtom(doubleAtom)).toBe(4); |
| b69ab31 | | | 333 | expect(readAtom(testAtom)).toBe(2); |
| b69ab31 | | | 334 | }); |
| b69ab31 | | | 335 | |
| b69ab31 | | | 336 | it('gets reset on dependency change', () => { |
| b69ab31 | | | 337 | const depAtom = atom(0); |
| b69ab31 | | | 338 | const testAtom = atomResetOnDepChange(1, depAtom); |
| b69ab31 | | | 339 | |
| b69ab31 | | | 340 | writeAtom(testAtom, 2); |
| b69ab31 | | | 341 | |
| b69ab31 | | | 342 | // Change depAtom should reset testAtom. |
| b69ab31 | | | 343 | writeAtom(depAtom, 10); |
| b69ab31 | | | 344 | expect(readAtom(testAtom)).toBe(1); |
| b69ab31 | | | 345 | |
| b69ab31 | | | 346 | // Set depAtom to the same value does not reset testAtom. |
| b69ab31 | | | 347 | writeAtom(testAtom, 3); |
| b69ab31 | | | 348 | writeAtom(depAtom, 10); |
| b69ab31 | | | 349 | expect(readAtom(testAtom)).toBe(3); |
| b69ab31 | | | 350 | }); |
| b69ab31 | | | 351 | }); |
| b69ab31 | | | 352 | |
| b69ab31 | | | 353 | describe('localStorageBackedAtomFamily', () => { |
| b69ab31 | | | 354 | function setupTestPlatform<T>(initialValues: Record<string, T>): Platform { |
| b69ab31 | | | 355 | const state = {...initialValues}; |
| b69ab31 | | | 356 | const mockPlatform = { |
| b69ab31 | | | 357 | getAllPersistedState: jest.fn().mockImplementation((): Record<string, T> => { |
| b69ab31 | | | 358 | return state; |
| b69ab31 | | | 359 | }), |
| b69ab31 | | | 360 | getPersistedState: jest.fn().mockImplementation((key: string): T => { |
| b69ab31 | | | 361 | return state[key]; |
| b69ab31 | | | 362 | }), |
| b69ab31 | | | 363 | setPersistedState: jest.fn().mockImplementation((key: string, value: T) => { |
| b69ab31 | | | 364 | state[key] = value; |
| b69ab31 | | | 365 | }), |
| b69ab31 | | | 366 | } as Partial<Platform> as Platform; |
| b69ab31 | | | 367 | return mockPlatform; |
| b69ab31 | | | 368 | } |
| b69ab31 | | | 369 | |
| b69ab31 | | | 370 | const testKey = 'test_' as LocalStorageName; |
| b69ab31 | | | 371 | |
| b69ab31 | | | 372 | it('loads initial state from storage', () => { |
| b69ab31 | | | 373 | const mockPlatform = setupTestPlatform({test_a: {data: 1, date: Date.now()}}); |
| b69ab31 | | | 374 | const family = localStorageBackedAtomFamily(testKey, () => 0, 1, mockPlatform); |
| b69ab31 | | | 375 | |
| b69ab31 | | | 376 | expect(readAtom(family('a'))).toEqual(1); |
| b69ab31 | | | 377 | expect(readAtom(family('b'))).toEqual(0); |
| b69ab31 | | | 378 | }); |
| b69ab31 | | | 379 | |
| b69ab31 | | | 380 | it('evicts old initial state from storage', () => { |
| b69ab31 | | | 381 | const mockPlatform = setupTestPlatform({ |
| b69ab31 | | | 382 | test_a: {data: 1, date: 0}, |
| b69ab31 | | | 383 | test_b: {data: 2, date: Date.now()}, |
| b69ab31 | | | 384 | }); |
| b69ab31 | | | 385 | const family = localStorageBackedAtomFamily(testKey, () => 0, 1, mockPlatform); |
| b69ab31 | | | 386 | |
| b69ab31 | | | 387 | expect(readAtom(family('a'))).toEqual(0); |
| b69ab31 | | | 388 | expect(readAtom(family('b'))).toEqual(2); |
| b69ab31 | | | 389 | }); |
| b69ab31 | | | 390 | |
| b69ab31 | | | 391 | it('writes to persisted state', () => { |
| b69ab31 | | | 392 | const mockPlatform = setupTestPlatform<number>({}); |
| b69ab31 | | | 393 | const family = localStorageBackedAtomFamily(testKey, (): number => 0, 1, mockPlatform); |
| b69ab31 | | | 394 | |
| b69ab31 | | | 395 | expect(readAtom(family('a'))).toEqual(0); |
| b69ab31 | | | 396 | writeAtom(family('a'), 2); |
| b69ab31 | | | 397 | expect(readAtom(family('a'))).toEqual(2); |
| b69ab31 | | | 398 | expect(mockPlatform.setPersistedState).toHaveBeenCalledWith('test_a', { |
| b69ab31 | | | 399 | data: 2, |
| b69ab31 | | | 400 | date: expect.any(Number), |
| b69ab31 | | | 401 | }); |
| b69ab31 | | | 402 | }); |
| b69ab31 | | | 403 | |
| b69ab31 | | | 404 | it("gets the latest data even after writing and then being gc'd", () => { |
| b69ab31 | | | 405 | const mockPlatform = setupTestPlatform({test_a: {data: 1, date: Date.now()}}); |
| b69ab31 | | | 406 | const family = localStorageBackedAtomFamily(testKey, (): number => 0, 1, mockPlatform); |
| b69ab31 | | | 407 | |
| b69ab31 | | | 408 | expect(readAtom(family('a'))).toEqual(1); |
| b69ab31 | | | 409 | writeAtom(family('a'), 2); |
| b69ab31 | | | 410 | expect(readAtom(family('a'))).toEqual(2); |
| b69ab31 | | | 411 | family.clear(); |
| b69ab31 | | | 412 | expect(readAtom(family('a'))).toEqual(2); |
| b69ab31 | | | 413 | }); |
| b69ab31 | | | 414 | |
| b69ab31 | | | 415 | it('writing undefined clears from persisted storage', () => { |
| b69ab31 | | | 416 | const mockPlatform = setupTestPlatform({test_a: {data: 1, date: Date.now()}}); |
| b69ab31 | | | 417 | const family = localStorageBackedAtomFamily(testKey, () => 0, 1, mockPlatform); |
| b69ab31 | | | 418 | |
| b69ab31 | | | 419 | // type check that although we can SET to undefined, we can't READ undefined |
| b69ab31 | | | 420 | const returnTypeShouldBeNonNull: number = readAtom(family('a')); |
| b69ab31 | | | 421 | expect(returnTypeShouldBeNonNull).toEqual(1); |
| b69ab31 | | | 422 | |
| b69ab31 | | | 423 | writeAtom(family('a'), undefined); |
| b69ab31 | | | 424 | |
| b69ab31 | | | 425 | expect(mockPlatform.setPersistedState).toHaveBeenCalledWith('test_a', undefined); |
| b69ab31 | | | 426 | expect(mockPlatform.getPersistedState('test_a')).toBeUndefined(); // nothing is stored |
| b69ab31 | | | 427 | expect(readAtom(family('a'))).toEqual(0); // read still gives default value |
| b69ab31 | | | 428 | }); |
| b69ab31 | | | 429 | }); |