From 2662c6ab9998f1729d63b1fd845e759f12f48bf4 Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Sat, 15 Mar 2025 08:25:13 +0900 Subject: [PATCH] test(query-core): use fake timers for mutationObserver.test.tsx --- .../src/__tests__/mutationObserver.test.tsx | 148 ++++++++---------- 1 file changed, 66 insertions(+), 82 deletions(-) diff --git a/packages/query-core/src/__tests__/mutationObserver.test.tsx b/packages/query-core/src/__tests__/mutationObserver.test.tsx index 55498a9b2a..3d0fe61df5 100644 --- a/packages/query-core/src/__tests__/mutationObserver.test.tsx +++ b/packages/query-core/src/__tests__/mutationObserver.test.tsx @@ -1,5 +1,4 @@ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' -import { waitFor } from '@testing-library/dom' import { MutationObserver } from '..' import { createQueryClient, queryKey, sleep } from './utils' import type { QueryClient } from '..' @@ -8,20 +7,19 @@ describe('mutationObserver', () => { let queryClient: QueryClient beforeEach(() => { + vi.useFakeTimers() queryClient = createQueryClient() queryClient.mount() }) afterEach(() => { queryClient.clear() + vi.useRealTimers() }) test('onUnsubscribe should not remove the current mutation observer if there is still a subscription', async () => { const mutation = new MutationObserver(queryClient, { - mutationFn: async (text: string) => { - await sleep(20) - return text - }, + mutationFn: (text: string) => sleep(20).then(() => text), }) const subscription1Handler = vi.fn() @@ -34,7 +32,7 @@ describe('mutationObserver', () => { unsubscribe1() - await waitFor(() => { + await vi.waitFor(() => { // 1 call: loading expect(subscription1Handler).toBeCalledTimes(1) // 2 calls: loading, success @@ -47,10 +45,7 @@ describe('mutationObserver', () => { test('unsubscribe should remove observer to trigger GC', async () => { const mutation = new MutationObserver(queryClient, { - mutationFn: async (text: string) => { - await sleep(5) - return text - }, + mutationFn: (text: string) => sleep(5).then(() => text), gcTime: 10, }) @@ -58,23 +53,21 @@ describe('mutationObserver', () => { const unsubscribe = mutation.subscribe(subscriptionHandler) - await mutation.mutate('input') - - expect(queryClient.getMutationCache().findAll()).toHaveLength(1) + mutation.mutate('input') + await vi.waitFor(() => + expect(queryClient.getMutationCache().findAll()).toHaveLength(1), + ) unsubscribe() - await waitFor(() => + await vi.waitFor(() => expect(queryClient.getMutationCache().findAll()).toHaveLength(0), ) }) test('reset should remove observer to trigger GC', async () => { const mutation = new MutationObserver(queryClient, { - mutationFn: async (text: string) => { - await sleep(5) - return text - }, + mutationFn: (text: string) => sleep(5).then(() => text), gcTime: 10, }) @@ -82,13 +75,14 @@ describe('mutationObserver', () => { const unsubscribe = mutation.subscribe(subscriptionHandler) - await mutation.mutate('input') - - expect(queryClient.getMutationCache().findAll()).toHaveLength(1) + mutation.mutate('input') + await vi.waitFor(() => + expect(queryClient.getMutationCache().findAll()).toHaveLength(1), + ) mutation.reset() - await waitFor(() => + await vi.waitFor(() => expect(queryClient.getMutationCache().findAll()).toHaveLength(0), ) @@ -99,22 +93,21 @@ describe('mutationObserver', () => { const key = queryKey() const mutation = new MutationObserver(queryClient, { mutationKey: [...key, '1'], - mutationFn: async (text: string) => { - await sleep(5) - return text - }, + mutationFn: (text: string) => sleep(5).then(() => text), }) const subscriptionHandler = vi.fn() const unsubscribe = mutation.subscribe(subscriptionHandler) - await mutation.mutate('input') + mutation.mutate('input') - expect(mutation.getCurrentResult()).toMatchObject({ - status: 'success', - data: 'input', - }) + await vi.waitFor(() => + expect(mutation.getCurrentResult()).toMatchObject({ + status: 'success', + data: 'input', + }), + ) mutation.setOptions({ mutationKey: [...key, '2'], @@ -131,27 +124,26 @@ describe('mutationObserver', () => { const key = queryKey() const mutationObserver = new MutationObserver(queryClient, { mutationKey: [...key, '1'], - mutationFn: async (text: string) => { - await sleep(5) - return text - }, + mutationFn: (text: string) => sleep(5).then(() => text), }) const subscriptionHandler = vi.fn() const unsubscribe = mutationObserver.subscribe(subscriptionHandler) - await mutationObserver.mutate('input') + mutationObserver.mutate('input') - expect( - queryClient.getMutationCache().find({ mutationKey: [...key, '1'] }), - ).toMatchObject({ - options: { mutationKey: [...key, '1'] }, - state: { - status: 'success', - data: 'input', - }, - }) + await vi.waitFor(() => + expect( + queryClient.getMutationCache().find({ mutationKey: [...key, '1'] }), + ).toMatchObject({ + options: { mutationKey: [...key, '1'] }, + state: { + status: 'success', + data: 'input', + }, + }), + ) mutationObserver.setOptions({ mutationKey: [...key, '2'], @@ -173,25 +165,24 @@ describe('mutationObserver', () => { test('changing mutation meta should not affect successful mutations', async () => { const mutationObserver = new MutationObserver(queryClient, { meta: { a: 1 }, - mutationFn: async (text: string) => { - await sleep(5) - return text - }, + mutationFn: (text: string) => sleep(5).then(() => text), }) const subscriptionHandler = vi.fn() const unsubscribe = mutationObserver.subscribe(subscriptionHandler) - await mutationObserver.mutate('input') + mutationObserver.mutate('input') - expect(queryClient.getMutationCache().find({})).toMatchObject({ - options: { meta: { a: 1 } }, - state: { - status: 'success', - data: 'input', - }, - }) + await vi.waitFor(() => + expect(queryClient.getMutationCache().find({})).toMatchObject({ + options: { meta: { a: 1 } }, + state: { + status: 'success', + data: 'input', + }, + }), + ) mutationObserver.setOptions({ meta: { a: 2 }, @@ -209,10 +200,7 @@ describe('mutationObserver', () => { }) test('mutation cache should have different meta when updated between mutations', async () => { - const mutationFn = async (text: string) => { - await sleep(5) - return text - } + const mutationFn = (text: string) => sleep(5).then(() => text) const mutationObserver = new MutationObserver(queryClient, { meta: { a: 1 }, mutationFn, @@ -222,14 +210,16 @@ describe('mutationObserver', () => { const unsubscribe = mutationObserver.subscribe(subscriptionHandler) - await mutationObserver.mutate('input') + mutationObserver.mutate('input') + await vi.advanceTimersByTimeAsync(5) mutationObserver.setOptions({ meta: { a: 2 }, mutationFn, }) - await mutationObserver.mutate('input') + mutationObserver.mutate('input') + await vi.advanceTimersByTimeAsync(5) const mutations = queryClient.getMutationCache().findAll() expect(mutations[0]).toMatchObject({ @@ -253,24 +243,23 @@ describe('mutationObserver', () => { test('changing mutation meta should not affect rejected mutations', async () => { const mutationObserver = new MutationObserver(queryClient, { meta: { a: 1 }, - mutationFn: async (_: string) => { - await sleep(5) - return Promise.reject(new Error('err')) - }, + mutationFn: (_: string) => + sleep(5).then(() => Promise.reject(new Error('err'))), }) const subscriptionHandler = vi.fn() const unsubscribe = mutationObserver.subscribe(subscriptionHandler) - await mutationObserver.mutate('input').catch(() => undefined) - - expect(queryClient.getMutationCache().find({})).toMatchObject({ - options: { meta: { a: 1 } }, - state: { - status: 'error', - }, - }) + mutationObserver.mutate('input').catch(() => undefined) + await vi.waitFor(() => + expect(queryClient.getMutationCache().find({})).toMatchObject({ + options: { meta: { a: 1 } }, + state: { + status: 'error', + }, + }), + ) mutationObserver.setOptions({ meta: { a: 2 }, @@ -289,10 +278,7 @@ describe('mutationObserver', () => { test('changing mutation meta should affect pending mutations', async () => { const mutationObserver = new MutationObserver(queryClient, { meta: { a: 1 }, - mutationFn: async (text: string) => { - await sleep(20) - return text - }, + mutationFn: (text: string) => sleep(20).then(() => text), }) const subscriptionHandler = vi.fn() @@ -300,9 +286,7 @@ describe('mutationObserver', () => { const unsubscribe = mutationObserver.subscribe(subscriptionHandler) mutationObserver.mutate('input') - - await sleep(0) - + await vi.advanceTimersByTimeAsync(5) expect(queryClient.getMutationCache().find({})).toMatchObject({ options: { meta: { a: 1 } }, state: {