From 064d81bd01cc765cc11faf287876065032279daa Mon Sep 17 00:00:00 2001 From: David Maskasky Date: Sat, 16 Nov 2024 18:47:31 -0800 Subject: [PATCH] add tests: atoms accessed with write get should have their dependencies updated --- tests/vanilla/dependency.test.tsx | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/vanilla/dependency.test.tsx b/tests/vanilla/dependency.test.tsx index 59ae209ae8..619218429f 100644 --- a/tests/vanilla/dependency.test.tsx +++ b/tests/vanilla/dependency.test.tsx @@ -334,3 +334,29 @@ it('handles complex dependency chains', async () => { resolve() expect(await promise2).toBe(10) }) + +it('updates dependencies for atoms accessed with write get', () => { + const store = createStore() + const a = atom(0) + const bRead = vi.fn((get) => get(a)) + const b = atom(bRead) + const c = atom(null, (get) => get(b)) + store.sub(a, () => {}) + store.set(c) + bRead.mockClear() + store.set(a, 1) + // note: this behavior is required for the next test to work + expect(bRead).toHaveBeenCalledOnce() + expect(store.get(a)).toBe(1) +}) + +it('can read sync derived atom in write without initializing', () => { + const store = createStore() + const a = atom(0) + const b = atom((get) => get(a) + 1) + const c = atom(null, (get, set) => set(a, get(b))) + store.set(c) + expect(store.get(a)).toBe(1) + store.set(c) + expect(store.get(a)).toBe(2) // note: this is why write get needs to update deps +})