diff --git a/packages/react/src/__tests__/index.tsx b/packages/react/src/__tests__/index.tsx index 6754003ad..b7bbd36aa 100644 --- a/packages/react/src/__tests__/index.tsx +++ b/packages/react/src/__tests__/index.tsx @@ -40,7 +40,6 @@ describe('@reatom/react', () => { }) rerender() - expect(subscriber.mock.calls.length).toBe(1) }) @@ -72,6 +71,29 @@ describe('@reatom/react', () => { expect(result.current).toBe(30) }) + test('unsubscribe from prevent dynamic atom', () => { + const store = createStore(countAtom, { count: 10 }); + const subscriber = jest.fn() + const _subscribe = store.subscribe + // @ts-ignore + store.subscribe = ((atom) => _subscribe(atom, subscriber)) + + const { rerender } = renderHook(({ + multiplier + }) => useAtom(map(countAtom, count => count * multiplier)), { + initialProps: { multiplier: 2 }, + wrapper: props => + }) + + act(() => { store.dispatch(increment()) }) + expect(subscriber.mock.calls.length).toBe(1) + + rerender({ multiplier: 3 }) + + act(() => store.dispatch(increment())) + expect(subscriber.mock.calls.length).toBe(1) + }) + test('does not update state if flag "isUpdatesNotNeeded" is set', () => { const store = createStore(countAtom, { count: 10 }); const { result, rerender } = renderHook(({ @@ -91,10 +113,10 @@ describe('@reatom/react', () => { test('unsubscribe from store after unmount', () => { const store = createStore(null) - const baseSubscribe = store.subscribe + const _subscribe = store.subscribe const subscriber = jest.fn() // @ts-ignore - store.subscribe = ((atom) => baseSubscribe(atom, subscriber)) + store.subscribe = ((atom) => _subscribe(atom, subscriber)) const { unmount } = renderHook(() => useAtom(countAtom, true), { wrapper: props =>