Skip to content

Commit 4077908

Browse files
authored
test(query-core/queryObserver): add tests for 'notifyOnChangeProps' as a function controlling listener notification (#10900)
1 parent 01616c6 commit 4077908

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
@@ -781,6 +781,52 @@ describe('queryObserver', () => {
781781
unsubscribe()
782782
})
783783

784+
it('should notify listeners when notifyOnChangeProps is a function returning props that changed', async () => {
785+
const key = queryKey()
786+
787+
queryClient.setQueryData(key, 'data')
788+
789+
const observer = new QueryObserver(queryClient, {
790+
queryKey: key,
791+
queryFn: () => sleep(10).then(() => 'new data'),
792+
staleTime: Infinity,
793+
notifyOnChangeProps: () => ['data'],
794+
})
795+
const listener = vi.fn()
796+
797+
const unsubscribe = observer.subscribe(listener)
798+
listener.mockClear()
799+
800+
observer.refetch()
801+
await vi.advanceTimersByTimeAsync(10)
802+
expect(listener).toHaveBeenCalledTimes(1)
803+
804+
unsubscribe()
805+
})
806+
807+
it('should not notify listeners when notifyOnChangeProps is a function returning props that did not change', async () => {
808+
const key = queryKey()
809+
810+
queryClient.setQueryData(key, 'data')
811+
812+
const observer = new QueryObserver(queryClient, {
813+
queryKey: key,
814+
queryFn: () => sleep(10).then(() => 'data'),
815+
staleTime: Infinity,
816+
notifyOnChangeProps: () => ['data'],
817+
})
818+
const listener = vi.fn()
819+
820+
const unsubscribe = observer.subscribe(listener)
821+
listener.mockClear()
822+
823+
observer.refetch()
824+
await vi.advanceTimersByTimeAsync(10)
825+
expect(listener).not.toHaveBeenCalled()
826+
827+
unsubscribe()
828+
})
829+
784830
it('should use placeholderData as non-cache data when pending a query with no data', async () => {
785831
const key = queryKey()
786832
const observer = new QueryObserver(queryClient, {

0 commit comments

Comments
 (0)