Skip to content

Commit

Permalink
add failing test: when changing the atom value with onMount setSelf, …
Browse files Browse the repository at this point in the history
…subscribers are not called
  • Loading branch information
dmaskasky committed Nov 11, 2024
1 parent 71cbc2f commit 20186ff
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,28 @@ it('should update derived atom even if dependances changed (#2697)', () => {
store.set(primitiveAtom, 1)
expect(onChangeDerived).toHaveBeenCalledTimes(1)
})

it('should call subscribers after setSelf updates atom value', () => {
const store = createStore()
const a = atom(0)
let unmount
a.onMount = vi.fn((setSelf) => {
setSelf(1)

Check failure on line 588 in tests/vanilla/store.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.0.5)

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

Check failure on line 588 in tests/vanilla/store.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.9.7)

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

Check failure on line 588 in tests/vanilla/store.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

Parameter 'setSelf' implicitly has an 'any' type.
unmount = vi.fn(() => {
console.log('onUnmount')
setSelf(2)
})
return unmount
})
const listener = vi.fn(() => {
})
const unsub = store.sub(a, listener)
expect(store.get(a)).toBe(1)
expect(a.onMount).toHaveBeenCalledTimes(1)
expect(listener).toHaveBeenCalledTimes(1)
listener.mockClear()
unsub()
expect(store.get(a)).toBe(2)
expect(unmount).toHaveBeenCalledTimes(1)
expect(listener).toHaveBeenCalledTimes(1)
})

0 comments on commit 20186ff

Please sign in to comment.