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 cbd63ec
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,27 @@ it('should update derived atom even if dependances changed (#2697)', () => {
store.set(primitiveAtom, 1)
expect(onChangeDerived).toHaveBeenCalledTimes(1)
})

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

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

View workflow job for this annotation

GitHub Actions / test_matrix (4.4.4)

Argument of type 'OnMount<[SetStateAction<number>], void> | undefined' is not assignable to parameter of type '(setAtom: SetAtom<[SetStateAction<number>], void>) => void | OnUnmount'.

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

View workflow job for this annotation

GitHub Actions / test_matrix (4.3.5)

Argument of type 'OnMount<[SetStateAction<number>], void> | undefined' is not assignable to parameter of type '(setAtom: SetAtom<[SetStateAction<number>], void>) => void | OnUnmount'.
SetAtom(1)

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

View workflow job for this annotation

GitHub Actions / test_matrix (4.2.3)

Argument of type 'OnMount<[SetStateAction<number>], void> | undefined' is not assignable to parameter of type '(setAtom: SetAtom<[SetStateAction<number>], void>) => void | OnUnmount'.

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

View workflow job for this annotation

GitHub Actions / test_matrix (4.1.5)

Argument of type 'OnMount<[SetStateAction<number>], void> | undefined' is not assignable to parameter of type '(setAtom: SetAtom<[SetStateAction<number>], void>) => void | OnUnmount'.
unmount = vi.fn(() => {
console.log('onUnmount')
SetAtom(2)
})
return unmount
}) as (typeof a)['onMount'])
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 cbd63ec

Please sign in to comment.