Skip to content

Commit 03f6585

Browse files
committed
test(query-core/queryObserver): add tests for 'notifyOnChangeProps' as a function controlling listener notification
1 parent 413fdc5 commit 03f6585

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

packages/query-core/src/__tests__/queryObserver.test.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,52 @@ describe('queryObserver', () => {
747747
expect(count).toBe(2)
748748
})
749749

750+
it('should notify listeners when notifyOnChangeProps is a function returning props that changed', async () => {
751+
const key = queryKey()
752+
753+
queryClient.setQueryData(key, 'data')
754+
755+
const observer = new QueryObserver(queryClient, {
756+
queryKey: key,
757+
queryFn: () => sleep(10).then(() => 'new data'),
758+
staleTime: Infinity,
759+
notifyOnChangeProps: () => ['data'],
760+
})
761+
const listener = vi.fn()
762+
763+
const unsubscribe = observer.subscribe(listener)
764+
listener.mockClear()
765+
766+
observer.refetch()
767+
await vi.advanceTimersByTimeAsync(10)
768+
expect(listener).toHaveBeenCalledTimes(1)
769+
770+
unsubscribe()
771+
})
772+
773+
it('should not notify listeners when notifyOnChangeProps is a function returning props that did not change', async () => {
774+
const key = queryKey()
775+
776+
queryClient.setQueryData(key, 'data')
777+
778+
const observer = new QueryObserver(queryClient, {
779+
queryKey: key,
780+
queryFn: () => sleep(10).then(() => 'data'),
781+
staleTime: Infinity,
782+
notifyOnChangeProps: () => ['data'],
783+
})
784+
const listener = vi.fn()
785+
786+
const unsubscribe = observer.subscribe(listener)
787+
listener.mockClear()
788+
789+
observer.refetch()
790+
await vi.advanceTimersByTimeAsync(10)
791+
expect(listener).not.toHaveBeenCalled()
792+
793+
unsubscribe()
794+
})
795+
750796
it('should use placeholderData as non-cache data when pending a query with no data', async () => {
751797
const key = queryKey()
752798
const observer = new QueryObserver(queryClient, {

0 commit comments

Comments
 (0)