Skip to content

Commit 608706e

Browse files
authored
test(query-core/queriesObserver): add test for returning cached combined result when nothing has changed (#10513)
1 parent cc97e9a commit 608706e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,32 @@ describe('queriesObserver', () => {
633633
trackResultSpy.mockRestore()
634634
})
635635

636+
test('should return cached combined result when nothing has changed', () => {
637+
const combine = vi.fn((results: Array<QueryObserverResult>) => ({
638+
count: results.length,
639+
}))
640+
641+
const key = queryKey()
642+
const queryFn = vi.fn().mockReturnValue(1)
643+
644+
const queries = [{ queryKey: key, queryFn }]
645+
646+
const observer = new QueriesObserver<{ count: number }>(
647+
queryClient,
648+
queries,
649+
{ combine },
650+
)
651+
652+
const [raw1, getCombined1] = observer.getOptimisticResult(queries, combine)
653+
const combined1 = getCombined1(raw1)
654+
655+
const [raw2, getCombined2] = observer.getOptimisticResult(queries, combine)
656+
const combined2 = getCombined2(raw2)
657+
658+
// Same combine, same queries → cached result returned
659+
expect(combined1).toBe(combined2)
660+
})
661+
636662
test('should track properties on all observers when trackResult is called', () => {
637663
const key1 = queryKey()
638664
const key2 = queryKey()

0 commit comments

Comments
 (0)