Skip to content

Commit 20186ff

Browse files
committed
add failing test: when changing the atom value with onMount setSelf, subscribers are not called
1 parent 71cbc2f commit 20186ff

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/vanilla/store.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,28 @@ it('should update derived atom even if dependances changed (#2697)', () => {
579579
store.set(primitiveAtom, 1)
580580
expect(onChangeDerived).toHaveBeenCalledTimes(1)
581581
})
582+
583+
it('should call subscribers after setSelf updates atom value', () => {
584+
const store = createStore()
585+
const a = atom(0)
586+
let unmount
587+
a.onMount = vi.fn((setSelf) => {
588+
setSelf(1)
589+
unmount = vi.fn(() => {
590+
console.log('onUnmount')
591+
setSelf(2)
592+
})
593+
return unmount
594+
})
595+
const listener = vi.fn(() => {
596+
})
597+
const unsub = store.sub(a, listener)
598+
expect(store.get(a)).toBe(1)
599+
expect(a.onMount).toHaveBeenCalledTimes(1)
600+
expect(listener).toHaveBeenCalledTimes(1)
601+
listener.mockClear()
602+
unsub()
603+
expect(store.get(a)).toBe(2)
604+
expect(unmount).toHaveBeenCalledTimes(1)
605+
expect(listener).toHaveBeenCalledTimes(1)
606+
})

0 commit comments

Comments
 (0)