From 03f65852d9fbe9c62b2bf2156c9c08ea9b4918c1 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Sun, 7 Jun 2026 15:55:40 +0900 Subject: [PATCH] test(query-core/queryObserver): add tests for 'notifyOnChangeProps' as a function controlling listener notification --- .../src/__tests__/queryObserver.test.tsx | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/query-core/src/__tests__/queryObserver.test.tsx b/packages/query-core/src/__tests__/queryObserver.test.tsx index db456896631..4481080ffe1 100644 --- a/packages/query-core/src/__tests__/queryObserver.test.tsx +++ b/packages/query-core/src/__tests__/queryObserver.test.tsx @@ -747,6 +747,52 @@ describe('queryObserver', () => { expect(count).toBe(2) }) + it('should notify listeners when notifyOnChangeProps is a function returning props that changed', async () => { + const key = queryKey() + + queryClient.setQueryData(key, 'data') + + const observer = new QueryObserver(queryClient, { + queryKey: key, + queryFn: () => sleep(10).then(() => 'new data'), + staleTime: Infinity, + notifyOnChangeProps: () => ['data'], + }) + const listener = vi.fn() + + const unsubscribe = observer.subscribe(listener) + listener.mockClear() + + observer.refetch() + await vi.advanceTimersByTimeAsync(10) + expect(listener).toHaveBeenCalledTimes(1) + + unsubscribe() + }) + + it('should not notify listeners when notifyOnChangeProps is a function returning props that did not change', async () => { + const key = queryKey() + + queryClient.setQueryData(key, 'data') + + const observer = new QueryObserver(queryClient, { + queryKey: key, + queryFn: () => sleep(10).then(() => 'data'), + staleTime: Infinity, + notifyOnChangeProps: () => ['data'], + }) + const listener = vi.fn() + + const unsubscribe = observer.subscribe(listener) + listener.mockClear() + + observer.refetch() + await vi.advanceTimersByTimeAsync(10) + expect(listener).not.toHaveBeenCalled() + + unsubscribe() + }) + it('should use placeholderData as non-cache data when pending a query with no data', async () => { const key = queryKey() const observer = new QueryObserver(queryClient, {