Skip to content

Commit 6e15fe6

Browse files
test({react,preact}-query/useMutation): add chained 'mutateAsync' tests for sequential mutation calls (#10490)
* test({react,preact}-query/useMutation): add chained 'mutateAsync' tests for sequential mutation calls * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 792d3a5 commit 6e15fe6

2 files changed

Lines changed: 172 additions & 0 deletions

File tree

packages/preact-query/src/__tests__/useMutation.test.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,4 +1893,90 @@ describe('useMutation', () => {
18931893
rendered.getByText('data: custom client, status: success'),
18941894
).toBeInTheDocument()
18951895
})
1896+
1897+
it('should be able to chain mutateAsync calls sequentially', async () => {
1898+
function Page() {
1899+
const [result, setResult] = useState<string>('idle')
1900+
1901+
const createUserMutation = useMutation({
1902+
mutationFn: (name: string) => sleep(10).then(() => ({ id: '1', name })),
1903+
})
1904+
1905+
const updateProfileMutation = useMutation({
1906+
mutationFn: (userId: string) =>
1907+
sleep(10).then(() => `profile updated for ${userId}`),
1908+
})
1909+
1910+
return (
1911+
<div>
1912+
<button
1913+
onClick={async () => {
1914+
const user = await createUserMutation.mutateAsync('John')
1915+
const profile = await updateProfileMutation.mutateAsync(user.id)
1916+
setResult(profile)
1917+
}}
1918+
>
1919+
chain
1920+
</button>
1921+
<div>result: {result}</div>
1922+
</div>
1923+
)
1924+
}
1925+
1926+
const rendered = renderWithClient(queryClient, <Page />)
1927+
1928+
fireEvent.click(rendered.getByRole('button', { name: /chain/i }))
1929+
await vi.advanceTimersByTimeAsync(10)
1930+
await vi.advanceTimersByTimeAsync(11)
1931+
1932+
expect(
1933+
rendered.getByText('result: profile updated for 1'),
1934+
).toBeInTheDocument()
1935+
})
1936+
1937+
it('should handle error in chained mutateAsync calls', async () => {
1938+
function Page() {
1939+
const [result, setResult] = useState<string>('idle')
1940+
1941+
const createUserMutation = useMutation({
1942+
mutationFn: (_name: string) =>
1943+
sleep(10).then<{ id: string }>(() => {
1944+
throw new Error('create failed')
1945+
}),
1946+
})
1947+
1948+
const updateProfileMutation = useMutation({
1949+
mutationFn: (userId: string) =>
1950+
sleep(10).then(() => `profile updated for ${userId}`),
1951+
})
1952+
1953+
return (
1954+
<div>
1955+
<button
1956+
onClick={async () => {
1957+
try {
1958+
const user = await createUserMutation.mutateAsync('John')
1959+
const profile = await updateProfileMutation.mutateAsync(user.id)
1960+
setResult(profile)
1961+
} catch (error) {
1962+
setResult(`error: ${(error as Error).message}`)
1963+
}
1964+
}}
1965+
>
1966+
chain
1967+
</button>
1968+
<div>result: {result}</div>
1969+
</div>
1970+
)
1971+
}
1972+
1973+
const rendered = renderWithClient(queryClient, <Page />)
1974+
1975+
fireEvent.click(rendered.getByRole('button', { name: /chain/i }))
1976+
await vi.advanceTimersByTimeAsync(11)
1977+
1978+
expect(
1979+
rendered.getByText('result: error: create failed'),
1980+
).toBeInTheDocument()
1981+
})
18961982
})

packages/react-query/src/__tests__/useMutation.test.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,4 +1892,90 @@ describe('useMutation', () => {
18921892
rendered.getByText('data: custom client, status: success'),
18931893
).toBeInTheDocument()
18941894
})
1895+
1896+
it('should be able to chain mutateAsync calls sequentially', async () => {
1897+
function Page() {
1898+
const [result, setResult] = React.useState<string>('idle')
1899+
1900+
const createUserMutation = useMutation({
1901+
mutationFn: (name: string) => sleep(10).then(() => ({ id: '1', name })),
1902+
})
1903+
1904+
const updateProfileMutation = useMutation({
1905+
mutationFn: (userId: string) =>
1906+
sleep(10).then(() => `profile updated for ${userId}`),
1907+
})
1908+
1909+
return (
1910+
<div>
1911+
<button
1912+
onClick={async () => {
1913+
const user = await createUserMutation.mutateAsync('John')
1914+
const profile = await updateProfileMutation.mutateAsync(user.id)
1915+
setResult(profile)
1916+
}}
1917+
>
1918+
chain
1919+
</button>
1920+
<div>result: {result}</div>
1921+
</div>
1922+
)
1923+
}
1924+
1925+
const rendered = renderWithClient(queryClient, <Page />)
1926+
1927+
fireEvent.click(rendered.getByRole('button', { name: /chain/i }))
1928+
await vi.advanceTimersByTimeAsync(10)
1929+
await vi.advanceTimersByTimeAsync(11)
1930+
1931+
expect(
1932+
rendered.getByText('result: profile updated for 1'),
1933+
).toBeInTheDocument()
1934+
})
1935+
1936+
it('should handle error in chained mutateAsync calls', async () => {
1937+
function Page() {
1938+
const [result, setResult] = React.useState<string>('idle')
1939+
1940+
const createUserMutation = useMutation({
1941+
mutationFn: (_name: string) =>
1942+
sleep(10).then<{ id: string }>(() => {
1943+
throw new Error('create failed')
1944+
}),
1945+
})
1946+
1947+
const updateProfileMutation = useMutation({
1948+
mutationFn: (userId: string) =>
1949+
sleep(10).then(() => `profile updated for ${userId}`),
1950+
})
1951+
1952+
return (
1953+
<div>
1954+
<button
1955+
onClick={async () => {
1956+
try {
1957+
const user = await createUserMutation.mutateAsync('John')
1958+
const profile = await updateProfileMutation.mutateAsync(user.id)
1959+
setResult(profile)
1960+
} catch (error) {
1961+
setResult(`error: ${(error as Error).message}`)
1962+
}
1963+
}}
1964+
>
1965+
chain
1966+
</button>
1967+
<div>result: {result}</div>
1968+
</div>
1969+
)
1970+
}
1971+
1972+
const rendered = renderWithClient(queryClient, <Page />)
1973+
1974+
fireEvent.click(rendered.getByRole('button', { name: /chain/i }))
1975+
await vi.advanceTimersByTimeAsync(11)
1976+
1977+
expect(
1978+
rendered.getByText('result: error: create failed'),
1979+
).toBeInTheDocument()
1980+
})
18951981
})

0 commit comments

Comments
 (0)