Skip to content

Commit 5bdc383

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

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,50 @@ 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+
765+
observer.refetch()
766+
await vi.advanceTimersByTimeAsync(10)
767+
expect(listener).toHaveBeenCalled()
768+
769+
unsubscribe()
770+
})
771+
772+
it('should not notify listeners when notifyOnChangeProps is a function returning props that did not change', async () => {
773+
const key = queryKey()
774+
775+
queryClient.setQueryData(key, 'data')
776+
777+
const observer = new QueryObserver(queryClient, {
778+
queryKey: key,
779+
queryFn: () => sleep(10).then(() => 'data'),
780+
staleTime: Infinity,
781+
notifyOnChangeProps: () => ['data'],
782+
})
783+
const listener = vi.fn()
784+
785+
const unsubscribe = observer.subscribe(listener)
786+
787+
observer.refetch()
788+
await vi.advanceTimersByTimeAsync(10)
789+
expect(listener).not.toHaveBeenCalled()
790+
791+
unsubscribe()
792+
})
793+
750794
it('should use placeholderData as non-cache data when pending a query with no data', async () => {
751795
const key = queryKey()
752796
const observer = new QueryObserver(queryClient, {

0 commit comments

Comments
 (0)