diff --git a/tests/vanilla/store.test.tsx b/tests/vanilla/store.test.tsx index 59ccb0672af..477c026de6c 100644 --- a/tests/vanilla/store.test.tsx +++ b/tests/vanilla/store.test.tsx @@ -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 setSelf updates atom value', () => { + const store = createStore() + const a = atom(0) + let unmount + a.onMount = vi.fn((setSelf) => { + setSelf(1) + 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(unmount).toHaveBeenCalledTimes(1) + expect(listener).toHaveBeenCalledTimes(1) +})