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 59bd8a3
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it, vi } from 'vitest'
import { atom, createStore } from 'jotai/vanilla'
import { atom, createStore, type Getter } from 'jotai/vanilla'

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

View workflow job for this annotation

GitHub Actions / lint

Member 'Getter' of the import declaration should be sorted alphabetically

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

View workflow job for this annotation

GitHub Actions / test_matrix (4.4.4)

',' expected.

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

View workflow job for this annotation

GitHub Actions / test_matrix (4.3.5)

',' expected.

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

View workflow job for this annotation

GitHub Actions / test_matrix (4.2.3)

',' expected.

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

View workflow job for this annotation

GitHub Actions / test_matrix (4.1.5)

',' expected.

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

View workflow job for this annotation

GitHub Actions / test_matrix (4.0.5)

',' expected.

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

View workflow job for this annotation

GitHub Actions / test_matrix (3.9.7)

',' expected.

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

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

',' expected.
it('can propagate updates with async atom chains', async () => {
const store = createStore()
Expand Down Expand Up @@ -334,3 +334,30 @@ 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: Getter) => 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)
// note: this is why write get needs to update deps
expect(store.get(a)).toBe(2)
})

0 comments on commit 59bd8a3

Please sign in to comment.