From 971cf30cfbc899166198792b9d3586dd8068b55c Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Sun, 12 Jul 2026 12:42:03 +0900 Subject: [PATCH] test(query-core): assert exact cache event arguments in subscribe callbacks --- .../query-core/src/__tests__/mutationCache.test.tsx | 4 +--- packages/query-core/src/__tests__/query.test.tsx | 7 ++----- packages/query-core/src/__tests__/queryCache.test.tsx | 10 ++++++---- packages/query-core/src/__tests__/queryClient.test.tsx | 10 +++++++++- .../query-core/src/__tests__/queryObserver.test.tsx | 9 ++++++--- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/packages/query-core/src/__tests__/mutationCache.test.tsx b/packages/query-core/src/__tests__/mutationCache.test.tsx index 05d4e581dfa..be46802a8e2 100644 --- a/packages/query-core/src/__tests__/mutationCache.test.tsx +++ b/packages/query-core/src/__tests__/mutationCache.test.tsx @@ -503,9 +503,7 @@ describe('mutationCache', () => { expect(testCache.getAll()).toHaveLength(0) expect(callback).toHaveBeenCalledTimes(1) - expect(callback).toHaveBeenCalledWith( - expect.objectContaining({ type: 'removed', mutation }), - ) + expect(callback).toHaveBeenCalledWith({ type: 'removed', mutation }) unsubscribe() }) diff --git a/packages/query-core/src/__tests__/query.test.tsx b/packages/query-core/src/__tests__/query.test.tsx index 00caf5704ee..ac4c10ea5b0 100644 --- a/packages/query-core/src/__tests__/query.test.tsx +++ b/packages/query-core/src/__tests__/query.test.tsx @@ -966,13 +966,10 @@ describe('query', () => { const unsubscribe = queryClient.getQueryCache().subscribe(fn) queryClient.setQueryData(key, 'data') + const query = queryClient.getQueryCache().find({ queryKey: key }) await vi.advanceTimersByTimeAsync(10) - expect(fn).toHaveBeenCalledWith( - expect.objectContaining({ - type: 'removed', - }), - ) + expect(fn).toHaveBeenLastCalledWith({ type: 'removed', query }) expect(queryClient.getQueryCache().findAll()).toHaveLength(0) diff --git a/packages/query-core/src/__tests__/queryCache.test.tsx b/packages/query-core/src/__tests__/queryCache.test.tsx index c404c1f942b..f381bcbccf1 100644 --- a/packages/query-core/src/__tests__/queryCache.test.tsx +++ b/packages/query-core/src/__tests__/queryCache.test.tsx @@ -24,7 +24,7 @@ describe('queryCache', () => { const unsubscribe = queryCache.subscribe(subscriber) queryClient.setQueryData(key, 'foo') const query = queryCache.find({ queryKey: key }) - expect(subscriber).toHaveBeenCalledWith({ query, type: 'added' }) + expect(subscriber).toHaveBeenNthCalledWith(1, { query, type: 'added' }) unsubscribe() }) @@ -37,7 +37,8 @@ describe('queryCache', () => { queryFn: () => sleep(100).then(() => 'data'), }) await vi.advanceTimersByTimeAsync(100) - expect(callback).toHaveBeenCalled() + const query = queryCache.find({ queryKey: key }) + expect(callback).toHaveBeenNthCalledWith(1, { query, type: 'added' }) }) it('should notify query cache when a query becomes stale', async () => { @@ -89,7 +90,7 @@ describe('queryCache', () => { }) await vi.advanceTimersByTimeAsync(100) const query = queryCache.find({ queryKey: key }) - expect(callback).toHaveBeenCalledWith({ query, type: 'added' }) + expect(callback).toHaveBeenNthCalledWith(1, { query, type: 'added' }) }) it('should notify subscribers when new query with initialData is added', async () => { @@ -102,7 +103,8 @@ describe('queryCache', () => { initialData: 'initial', }) await vi.advanceTimersByTimeAsync(100) - expect(callback).toHaveBeenCalled() + const query = queryCache.find({ queryKey: key }) + expect(callback).toHaveBeenNthCalledWith(1, { query, type: 'added' }) }) it('should be able to limit cache size', async () => { diff --git a/packages/query-core/src/__tests__/queryClient.test.tsx b/packages/query-core/src/__tests__/queryClient.test.tsx index a8db8d10c76..2ed4c4c193a 100644 --- a/packages/query-core/src/__tests__/queryClient.test.tsx +++ b/packages/query-core/src/__tests__/queryClient.test.tsx @@ -1614,7 +1614,15 @@ describe('queryClient', () => { queryClient.resetQueries({ queryKey: key }) - expect(callback).toHaveBeenCalled() + const query = queryCache.find({ queryKey: key }) + expect(callback).toHaveBeenNthCalledWith( + 1, + expect.objectContaining({ + type: 'updated', + query, + action: expect.objectContaining({ type: 'setState' }), + }), + ) }) it('should reset query', async () => { diff --git a/packages/query-core/src/__tests__/queryObserver.test.tsx b/packages/query-core/src/__tests__/queryObserver.test.tsx index 79dc3a8e588..9ffcb3ba51b 100644 --- a/packages/query-core/src/__tests__/queryObserver.test.tsx +++ b/packages/query-core/src/__tests__/queryObserver.test.tsx @@ -1240,10 +1240,13 @@ describe('queryObserver', () => { const unsubscribe = queryClient.getQueryCache().subscribe(spy) observer.setOptions({ queryKey: key, enabled: false, refetchInterval: 10 }) + const query = queryClient.getQueryCache().find({ queryKey: key }) expect(spy).toHaveBeenCalledTimes(1) - expect(spy).toHaveBeenCalledWith( - expect.objectContaining({ type: 'observerOptionsUpdated' }), - ) + expect(spy).toHaveBeenCalledWith({ + type: 'observerOptionsUpdated', + query, + observer, + }) unsubscribe() })