Skip to content

Commit

Permalink
test(dependency): Improve test coverage (#2759)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ts1994tw authored Oct 3, 2024
1 parent 9bd8ed7 commit d5bb477
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((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)
})

0 comments on commit d5bb477

Please sign in to comment.