From d5bb4777f024d8fd4c1a96afdc1a814462335d00 Mon Sep 17 00:00:00 2001 From: BorenHsiao <65659727+ts1994tw@users.noreply.github.com> Date: Thu, 3 Oct 2024 20:40:02 +0900 Subject: [PATCH] test(dependency): Improve test coverage (#2759) * feat: add test for scenario: Error Handling in Async Atoms * feat: add test for scenario: Complex Dependency Chains * feat: add test for scenario: Performance with Large Number of Atoms * fix: remove unnecessary test * refactor: adjust test for scenario: Complex Dependency Chains * fix: remove unnecessary test scenario --- tests/vanilla/dependency.test.tsx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/vanilla/dependency.test.tsx b/tests/vanilla/dependency.test.tsx index a021c3c80e..645d0f56aa 100644 --- a/tests/vanilla/dependency.test.tsx +++ b/tests/vanilla/dependency.test.tsx @@ -274,3 +274,25 @@ it('refreshes deps for each async read', async () => { resolve.splice(0).forEach((fn) => fn()) expect(values).toEqual([0, 1]) }) + +it('handles complex dependency chains', async () => { + const baseAtom = atom(1) + const derived1 = atom((get) => get(baseAtom) * 2) + const derived2 = atom((get) => get(derived1) + 1) + let resolve = () => {} + const asyncDerived = atom(async (get) => { + const value = get(derived2) + await new Promise((r) => (resolve = r)) + return value * 2 + }) + + const store = createStore() + const promise = store.get(asyncDerived) + resolve() + expect(await promise).toBe(6) + + store.set(baseAtom, 2) + const promise2 = store.get(asyncDerived) + resolve() + expect(await promise2).toBe(10) +})