Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(query-core): use fake timers for mutations.test.tsx #8806

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions packages/query-core/src/__tests__/mutations.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { waitFor } from '@testing-library/dom'
import { MutationObserver } from '../mutationObserver'
import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
import type { QueryClient } from '..'
Expand All @@ -9,12 +8,14 @@ describe('mutations', () => {
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryClient = createQueryClient()
queryClient.mount()
})

afterEach(() => {
queryClient.clear()
vi.useRealTimers()
})

test('mutate should accept null values', async () => {
Expand Down Expand Up @@ -54,10 +55,7 @@ describe('mutations', () => {

test('mutation should set correct success states', async () => {
const mutation = new MutationObserver(queryClient, {
mutationFn: async (text: string) => {
await sleep(10)
return text
},
mutationFn: (text: string) => sleep(10).then(() => text),
onMutate: (text) => text,
})

Expand Down Expand Up @@ -87,7 +85,7 @@ describe('mutations', () => {

mutation.mutate('todo')

await sleep(0)
await vi.advanceTimersByTimeAsync(0)

expect(states[0]).toEqual({
context: undefined,
Expand All @@ -107,7 +105,7 @@ describe('mutations', () => {
submittedAt: expect.any(Number),
})

await sleep(5)
await vi.advanceTimersByTimeAsync(5)

expect(states[1]).toEqual({
context: 'todo',
Expand All @@ -127,7 +125,7 @@ describe('mutations', () => {
submittedAt: expect.any(Number),
})

await sleep(20)
await vi.advanceTimersByTimeAsync(20)

expect(states[2]).toEqual({
context: 'todo',
Expand All @@ -150,10 +148,8 @@ describe('mutations', () => {

test('mutation should set correct error states', async () => {
const mutation = new MutationObserver(queryClient, {
mutationFn: async (_: string) => {
await sleep(20)
return Promise.reject(new Error('err'))
},
mutationFn: (_: string) =>
sleep(20).then(() => Promise.reject(new Error('err'))),
onMutate: (text) => text,
retry: 1,
retryDelay: 1,
Expand All @@ -167,7 +163,7 @@ describe('mutations', () => {

mutation.mutate('todo').catch(() => undefined)

await sleep(0)
await vi.advanceTimersByTimeAsync(0)

expect(states[0]).toEqual({
context: undefined,
Expand All @@ -187,7 +183,7 @@ describe('mutations', () => {
submittedAt: expect.any(Number),
})

await sleep(10)
await vi.advanceTimersByTimeAsync(10)

expect(states[1]).toEqual({
context: 'todo',
Expand All @@ -207,7 +203,7 @@ describe('mutations', () => {
submittedAt: expect.any(Number),
})

await sleep(20)
await vi.advanceTimersByTimeAsync(20)

expect(states[2]).toEqual({
context: 'todo',
Expand All @@ -227,7 +223,7 @@ describe('mutations', () => {
submittedAt: expect.any(Number),
})

await sleep(30)
await vi.advanceTimersByTimeAsync(30)

expect(states[3]).toEqual({
context: 'todo',
Expand Down Expand Up @@ -405,7 +401,7 @@ describe('mutations', () => {
},
})

await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))

expect(onSuccess).toHaveBeenCalledWith(2)
})
Expand All @@ -417,7 +413,7 @@ describe('mutations', () => {

const results: Array<string> = []

const execute1 = executeMutation(
executeMutation(
queryClient,
{
mutationKey: key1,
Expand All @@ -441,7 +437,7 @@ describe('mutations', () => {
isPaused: false,
})

const execute2 = executeMutation(
executeMutation(
queryClient,
{
mutationKey: key2,
Expand All @@ -465,7 +461,7 @@ describe('mutations', () => {
isPaused: true,
})

await Promise.all([execute1, execute2])
await vi.runAllTimersAsync()

expect(results).toStrictEqual([
'start-A',
Expand All @@ -482,7 +478,7 @@ describe('mutations', () => {

const results: Array<string> = []

const execute1 = executeMutation(
executeMutation(
queryClient,
{
mutationKey: key1,
Expand All @@ -496,7 +492,7 @@ describe('mutations', () => {
'vars1',
)

const execute2 = executeMutation(
executeMutation(
queryClient,
{
mutationKey: key2,
Expand All @@ -510,7 +506,7 @@ describe('mutations', () => {
'vars2',
)

await Promise.all([execute1, execute2])
await vi.runAllTimersAsync()

expect(results).toStrictEqual([
'start-A',
Expand All @@ -523,7 +519,7 @@ describe('mutations', () => {
test('each scope should run should run in parallel, serial within scope', async () => {
const results: Array<string> = []

const execute1 = executeMutation(
executeMutation(
queryClient,
{
scope: {
Expand All @@ -539,7 +535,7 @@ describe('mutations', () => {
'vars1',
)

const execute2 = executeMutation(
executeMutation(
queryClient,
{
scope: {
Expand All @@ -555,7 +551,7 @@ describe('mutations', () => {
'vars2',
)

const execute3 = executeMutation(
executeMutation(
queryClient,
{
scope: {
Expand All @@ -571,7 +567,7 @@ describe('mutations', () => {
'vars1',
)

const execute4 = executeMutation(
executeMutation(
queryClient,
{
scope: {
Expand All @@ -587,7 +583,7 @@ describe('mutations', () => {
'vars2',
)

await Promise.all([execute1, execute2, execute3, execute4])
await vi.runAllTimersAsync()

expect(results).toStrictEqual([
'start-A1',
Expand Down
Loading