diff --git a/packages/query-core/src/__tests__/mutationCache.test.tsx b/packages/query-core/src/__tests__/mutationCache.test.tsx index d2c16987bc..a3f41d99b7 100644 --- a/packages/query-core/src/__tests__/mutationCache.test.tsx +++ b/packages/query-core/src/__tests__/mutationCache.test.tsx @@ -483,5 +483,31 @@ describe('mutationCache', () => { expect(testCache.getAll()).toHaveLength(0) }) + + test('should still notify removal when removing a mutation that does not exist in the cache', () => { + const testCache = new MutationCache() + const testClient = new QueryClient({ mutationCache: testCache }) + + const mutation = testCache.build(testClient, { + mutationFn: () => Promise.resolve('data'), + }) + + expect(testCache.getAll()).toHaveLength(1) + testCache.remove(mutation) + expect(testCache.getAll()).toHaveLength(0) + + // Remove again — mutation is already gone from the cache + const callback = vi.fn() + const unsubscribe = testCache.subscribe(callback) + testCache.remove(mutation) + + expect(testCache.getAll()).toHaveLength(0) + expect(callback).toHaveBeenCalledTimes(1) + expect(callback).toHaveBeenCalledWith( + expect.objectContaining({ type: 'removed', mutation }), + ) + + unsubscribe() + }) }) })