From 7ab30a9c89ea7189effe06ea90837fc8e08ee379 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Fri, 4 Jul 2025 01:52:46 +0900 Subject: [PATCH] test(react-query/useMutationState): remove 'vi.waitFor' and add 'advanceTimersByTimeAsync' --- .../src/__tests__/useMutationState.test.tsx | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/packages/react-query/src/__tests__/useMutationState.test.tsx b/packages/react-query/src/__tests__/useMutationState.test.tsx index 5797172030..186821de59 100644 --- a/packages/react-query/src/__tests__/useMutationState.test.tsx +++ b/packages/react-query/src/__tests__/useMutationState.test.tsx @@ -62,12 +62,12 @@ describe('useIsMutating', () => { // [ +0, 1, 2, 1, +0 ] // our batching strategy might yield different results - await vi.waitFor(() => expect(isMutatingArray[0]).toEqual(0)) - await vi.waitFor(() => expect(isMutatingArray[1]).toEqual(1)) - await vi.waitFor(() => expect(isMutatingArray[2]).toEqual(2)) - await vi.waitFor(() => - expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0), - ) + await vi.advanceTimersByTimeAsync(40) + expect(isMutatingArray[0]).toEqual(0) + expect(isMutatingArray[1]).toEqual(1) + expect(isMutatingArray[2]).toEqual(2) + await vi.advanceTimersByTimeAsync(1) + expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0) }) it('should filter correctly by mutationKey', async () => { @@ -99,7 +99,8 @@ describe('useIsMutating', () => { } renderWithClient(queryClient, ) - await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0])) + await vi.advanceTimersByTimeAsync(101) + expect(isMutatingArray).toEqual([0, 1, 0]) }) it('should filter correctly by predicate', async () => { @@ -134,7 +135,8 @@ describe('useIsMutating', () => { } renderWithClient(queryClient, ) - await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0])) + await vi.advanceTimersByTimeAsync(101) + expect(isMutatingArray).toEqual([0, 1, 0]) }) it('should use provided custom queryClient', async () => { @@ -163,13 +165,20 @@ describe('useIsMutating', () => { const rendered = render() - await vi.waitFor(() => - expect(rendered.getByText('mutating: 1')).toBeInTheDocument(), - ) + await vi.advanceTimersByTimeAsync(0) + expect(rendered.getByText('mutating: 1')).toBeInTheDocument() }) }) describe('useMutationState', () => { + beforeEach(() => { + vi.useFakeTimers() + }) + + afterEach(() => { + vi.useRealTimers() + }) + it('should return variables after calling mutate', async () => { const queryClient = new QueryClient() const variables: Array> = [] @@ -211,11 +220,12 @@ describe('useMutationState', () => { const rendered = renderWithClient(queryClient, ) - await vi.waitFor(() => rendered.getByText('data: null')) + rendered.getByText('data: null') fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) - await vi.waitFor(() => rendered.getByText('data: data1')) + await vi.advanceTimersByTimeAsync(151) + rendered.getByText('data: data1') expect(variables).toEqual([[], [1], []]) })