Skip to content

Commit

Permalink
add tests: atoms accessed with write get should have their dependenci…
Browse files Browse the repository at this point in the history
…es updated
  • Loading branch information
dmaskasky committed Nov 17, 2024
1 parent 0f05df8 commit 064d81b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 342 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.0.5)

Parameter 'get' implicitly has an 'any' type.

Check failure on line 342 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.9.7)

Parameter 'get' implicitly has an 'any' type.

Check failure on line 342 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

Parameter 'get' implicitly has an 'any' type.
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
})

0 comments on commit 064d81b

Please sign in to comment.