|
| 1 | +import { expect, it, vi } from 'vitest' |
| 2 | +import { type Atom, atom, createStore } from 'jotai/vanilla' |
| 3 | +import { atomFamily } from 'jotai/vanilla/utils' |
| 4 | + |
| 5 | +it('should create atoms with different params', () => { |
| 6 | + const store = createStore() |
| 7 | + const aFamily = atomFamily((param: number) => atom(param)) |
| 8 | + |
| 9 | + expect(store.get(aFamily(1))).toEqual(1) |
| 10 | + expect(store.get(aFamily(2))).toEqual(2) |
| 11 | +}) |
| 12 | + |
| 13 | +it('should remove atoms', () => { |
| 14 | + const store = createStore() |
| 15 | + const initializeAtom = vi.fn((param: number) => atom(param)) |
| 16 | + const aFamily = atomFamily(initializeAtom) |
| 17 | + |
| 18 | + expect(store.get(aFamily(1))).toEqual(1) |
| 19 | + expect(store.get(aFamily(2))).toEqual(2) |
| 20 | + aFamily.remove(2) |
| 21 | + initializeAtom.mockClear() |
| 22 | + expect(store.get(aFamily(1))).toEqual(1) |
| 23 | + expect(initializeAtom).toHaveBeenCalledTimes(0) |
| 24 | + expect(store.get(aFamily(2))).toEqual(2) |
| 25 | + expect(initializeAtom).toHaveBeenCalledTimes(1) |
| 26 | +}) |
| 27 | + |
| 28 | +it('should remove atoms with custom comparator', () => { |
| 29 | + const store = createStore() |
| 30 | + const initializeAtom = vi.fn((param: number) => atom(param)) |
| 31 | + const aFamily = atomFamily(initializeAtom, (a, b) => a === b) |
| 32 | + |
| 33 | + expect(store.get(aFamily(1))).toEqual(1) |
| 34 | + expect(store.get(aFamily(2))).toEqual(2) |
| 35 | + expect(store.get(aFamily(3))).toEqual(3) |
| 36 | + aFamily.remove(2) |
| 37 | + initializeAtom.mockClear() |
| 38 | + expect(store.get(aFamily(1))).toEqual(1) |
| 39 | + expect(initializeAtom).toHaveBeenCalledTimes(0) |
| 40 | + expect(store.get(aFamily(2))).toEqual(2) |
| 41 | + expect(initializeAtom).toHaveBeenCalledTimes(1) |
| 42 | +}) |
| 43 | + |
| 44 | +it('should remove atoms with custom shouldRemove', () => { |
| 45 | + const store = createStore() |
| 46 | + const initializeAtom = vi.fn((param: number) => atom(param)) |
| 47 | + const aFamily = atomFamily<number, Atom<number>>(initializeAtom) |
| 48 | + expect(store.get(aFamily(1))).toEqual(1) |
| 49 | + expect(store.get(aFamily(2))).toEqual(2) |
| 50 | + expect(store.get(aFamily(3))).toEqual(3) |
| 51 | + aFamily.setShouldRemove((_createdAt, param) => param % 2 === 0) |
| 52 | + initializeAtom.mockClear() |
| 53 | + expect(store.get(aFamily(1))).toEqual(1) |
| 54 | + expect(initializeAtom).toHaveBeenCalledTimes(0) |
| 55 | + expect(store.get(aFamily(2))).toEqual(2) |
| 56 | + expect(initializeAtom).toHaveBeenCalledTimes(1) |
| 57 | + expect(store.get(aFamily(3))).toEqual(3) |
| 58 | + expect(initializeAtom).toHaveBeenCalledTimes(1) |
| 59 | +}) |
| 60 | + |
| 61 | +it('should notify listeners', () => { |
| 62 | + const store = createStore() |
| 63 | + const aFamily = atomFamily((param: number) => atom(param)) |
| 64 | + type AtomEvent = Parameters<Parameters<typeof aFamily.unstable_listen>[0]>[0] |
| 65 | + const listener = vi.fn(({ type, param, atom }: AtomEvent) => {}) |
| 66 | + const unsubscribe = aFamily.unstable_listen(listener) |
| 67 | + const atom1 = aFamily(1) |
| 68 | + expect(listener).toHaveBeenCalledTimes(1) |
| 69 | + const eventCreate = listener.mock.calls[0][0] |
| 70 | + expect(eventCreate.type).toEqual('CREATE') |
| 71 | + expect(eventCreate.param).toEqual(1) |
| 72 | + expect(eventCreate.atom).toEqual(atom1) |
| 73 | + listener.mockClear() |
| 74 | + aFamily.remove(1) |
| 75 | + expect(listener).toHaveBeenCalledTimes(1) |
| 76 | + const eventRemove = listener.mock.calls[0][0] |
| 77 | + expect(eventRemove.type).toEqual('REMOVE') |
| 78 | + expect(eventRemove.param).toEqual(1) |
| 79 | + expect(eventRemove.atom).toEqual(atom1) |
| 80 | + unsubscribe() |
| 81 | + listener.mockClear() |
| 82 | + aFamily(2) |
| 83 | + expect(listener).toHaveBeenCalledTimes(0) |
| 84 | +}) |
| 85 | + |
| 86 | +it('should return all params', () => { |
| 87 | + const store = createStore() |
| 88 | + const aFamily = atomFamily((param: number) => atom(param)) |
| 89 | + |
| 90 | + expect(store.get(aFamily(1))).toEqual(1) |
| 91 | + expect(store.get(aFamily(2))).toEqual(2) |
| 92 | + expect(store.get(aFamily(3))).toEqual(3) |
| 93 | + expect(Array.from(aFamily.getParams())).toEqual([1, 2, 3]) |
| 94 | +}) |
0 commit comments