Skip to content

Commit

Permalink
chore: try to stabilize a flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo committed Oct 11, 2024
1 parent 018b1da commit 9812699
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/react-query/src/__tests__/useMutationState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
createQueryClient,
doNotExecute,
renderWithClient,
setActTimeout,
sleep,
} from './utils'
import type { MutationState, MutationStatus } from '@tanstack/query-core'
Expand All @@ -27,26 +26,24 @@ describe('useIsMutating', () => {
const { mutate: mutate1 } = useMutation({
mutationKey: ['mutation1'],
mutationFn: async () => {
await sleep(150)
await sleep(50)
return 'data'
},
})
const { mutate: mutate2 } = useMutation({
mutationKey: ['mutation2'],
mutationFn: async () => {
await sleep(50)
await sleep(10)
return 'data'
},
})

React.useEffect(() => {
mutate1()
setActTimeout(() => {
mutate2()
}, 50)
}, [mutate1, mutate2])

return null
return (
<div>
<button onClick={() => mutate1()}>mutate1</button>
<button onClick={() => mutate2()}>mutate2</button>
</div>
)
}

function Page() {
Expand All @@ -58,7 +55,10 @@ describe('useIsMutating', () => {
)
}

renderWithClient(queryClient, <Page />)
const rendered = renderWithClient(queryClient, <Page />)
fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
await sleep(10)
fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
await waitFor(() => expect(isMutatingArray).toEqual([0, 1, 2, 1, 0]))
})

Expand Down

0 comments on commit 9812699

Please sign in to comment.