Skip to content

Commit 9750ebb

Browse files
committed
test(query-core/queriesObserver): add test for recalculating combined result when 'combine' function changes
1 parent c47c671 commit 9750ebb

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,40 @@ describe('queriesObserver', () => {
588588
trackPropSpy.mockRestore()
589589
})
590590

591+
test('should recalculate combined result when combine function changes', () => {
592+
const combine1 = vi.fn((results: Array<QueryObserverResult>) => ({
593+
total: results.length,
594+
}))
595+
const combine2 = vi.fn((results: Array<QueryObserverResult>) => ({
596+
total: results.length * 4,
597+
}))
598+
599+
const key1 = queryKey()
600+
const key2 = queryKey()
601+
const queryFn1 = vi.fn().mockReturnValue(1)
602+
const queryFn2 = vi.fn().mockReturnValue(2)
603+
604+
const queries = [
605+
{ queryKey: key1, queryFn: queryFn1 },
606+
{ queryKey: key2, queryFn: queryFn2 },
607+
]
608+
609+
const observer = new QueriesObserver<{ total: number }>(
610+
queryClient,
611+
queries,
612+
{ combine: combine1 },
613+
)
614+
615+
const [raw1, getCombined1] = observer.getOptimisticResult(queries, combine1)
616+
const combined1 = getCombined1(raw1)
617+
618+
const [raw2, getCombined2] = observer.getOptimisticResult(queries, combine2)
619+
const combined2 = getCombined2(raw2)
620+
621+
expect(combined1.total).toBe(2)
622+
expect(combined2.total).toBe(8)
623+
})
624+
591625
test('should subscribe to new observers when a query is added while subscribed', async () => {
592626
const key1 = queryKey()
593627
const key2 = queryKey()

0 commit comments

Comments
 (0)