|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { atom, createStore } from 'jotai/vanilla' |
| 3 | +import { RESET, atomWithDefault } from 'jotai/vanilla/utils' |
| 4 | + |
| 5 | +describe('atomWithDefault', () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.clearAllMocks() |
| 8 | + }) |
| 9 | + |
| 10 | + it('should reset to initial value using RESET', () => { |
| 11 | + const initialValue = 10 |
| 12 | + const testAtom = atomWithDefault(() => initialValue) |
| 13 | + const store = createStore() |
| 14 | + store.set(testAtom, 123) |
| 15 | + store.set(testAtom, RESET) |
| 16 | + expect(store.get(testAtom)).toBe(initialValue) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should reset to initial value derived from another atom', () => { |
| 20 | + const initialValueAtom = atom(10) |
| 21 | + const testAtom = atomWithDefault((get) => get(initialValueAtom)) |
| 22 | + const store = createStore() |
| 23 | + expect(store.get(testAtom)).toBe(10) |
| 24 | + store.set(testAtom, 123) |
| 25 | + store.set(initialValueAtom, 20) |
| 26 | + store.set(testAtom, RESET) |
| 27 | + expect(store.get(testAtom)).toBe(20) |
| 28 | + }) |
| 29 | + |
| 30 | + it(`should reflect changes to the initial value atom when main atom hasn't been manually changed`, () => { |
| 31 | + const initialValueAtom = atom(10) |
| 32 | + const testAtom = atomWithDefault((get) => get(initialValueAtom)) |
| 33 | + const store = createStore() |
| 34 | + store.set(initialValueAtom, 20) |
| 35 | + expect(store.get(testAtom)).toBe(20) |
| 36 | + }) |
| 37 | + |
| 38 | + it(`should reflect changes to the initial value atom when main atom has been manually changed but then RESET`, () => { |
| 39 | + const initialValueAtom = atom(10) |
| 40 | + const testAtom = atomWithDefault((get) => get(initialValueAtom)) |
| 41 | + const store = createStore() |
| 42 | + store.set(testAtom, 123) |
| 43 | + // if this RESET were storing 10 rather than EMPTY the next set wouldn't have an effect |
| 44 | + store.set(testAtom, RESET) |
| 45 | + store.set(initialValueAtom, 20) |
| 46 | + expect(store.get(testAtom)).toBe(20) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should update atom with a new value', () => { |
| 50 | + const initialValue = 10 |
| 51 | + const testAtom = atomWithDefault(() => initialValue) |
| 52 | + const store = createStore() |
| 53 | + store.set(testAtom, 123) |
| 54 | + store.set(testAtom, 30) |
| 55 | + expect(store.get(testAtom)).toBe(30) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should update atom using a function', () => { |
| 59 | + const initialValue = 10 |
| 60 | + const testAtom = atomWithDefault(() => initialValue) |
| 61 | + const store = createStore() |
| 62 | + store.set(testAtom, 123) |
| 63 | + store.set(testAtom, (prev: number) => prev + 10) |
| 64 | + expect(store.get(testAtom)).toBe(133) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should reset with a function', () => { |
| 68 | + const initialValue = 10 |
| 69 | + const testAtom = atomWithDefault(() => initialValue) |
| 70 | + const store = createStore() |
| 71 | + store.set(testAtom, 123) |
| 72 | + store.set(testAtom, () => RESET) |
| 73 | + expect(store.get(testAtom)).toBe(initialValue) |
| 74 | + }) |
| 75 | +}) |
0 commit comments