-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-query): add support for global onSuccess for mutation (#5668)
- Loading branch information
1 parent
7f7c36e
commit 3bb7111
Showing
4 changed files
with
176 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
packages/tests/server/react/queryClientDefaults.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { routerToServerAndClientNew } from '../___testHelpers'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { render, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { createTRPCReact, getUntypedClient } from '@trpc/react-query'; | ||
import { initTRPC } from '@trpc/server'; | ||
import { konn } from 'konn'; | ||
import type { ReactNode } from 'react'; | ||
import React from 'react'; | ||
import { z } from 'zod'; | ||
|
||
describe('query client defaults', () => { | ||
const ctx = konn() | ||
.beforeEach(() => { | ||
const t = initTRPC.create(); | ||
interface Post { | ||
title: string; | ||
} | ||
const onSuccessSpy = vi.fn(); | ||
|
||
const posts: Post[] = []; | ||
|
||
const appRouter = t.router({ | ||
list: t.procedure.query(() => posts), | ||
add: t.procedure.input(z.string()).mutation(({ input }) => { | ||
posts.push({ | ||
title: input, | ||
}); | ||
}), | ||
}); | ||
const opts = routerToServerAndClientNew(appRouter); | ||
const trpc = createTRPCReact<typeof appRouter>(); | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
mutations: { | ||
onSuccess: async (opts) => { | ||
await queryClient.invalidateQueries(); | ||
onSuccessSpy(opts); | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
function App(props: { children: ReactNode }) { | ||
return ( | ||
<trpc.Provider | ||
{...{ queryClient, client: getUntypedClient(opts.client) }} | ||
> | ||
<QueryClientProvider client={queryClient}> | ||
{props.children} | ||
</QueryClientProvider> | ||
</trpc.Provider> | ||
); | ||
} | ||
return { | ||
...opts, | ||
App, | ||
trpc, | ||
onSuccessSpy, | ||
}; | ||
}) | ||
.afterEach(async (opts) => { | ||
await opts?.close?.(); | ||
}) | ||
.done(); | ||
|
||
test('clear cache on every mutation', async () => { | ||
const { trpc } = ctx; | ||
const nonce = `nonce-${Math.random()}`; | ||
function MyComp() { | ||
const listQuery = trpc.list.useQuery(); | ||
const mutation = trpc.add.useMutation(); | ||
|
||
return ( | ||
<> | ||
<button | ||
onClick={() => { | ||
mutation.mutate(nonce); | ||
}} | ||
data-testid="add" | ||
> | ||
add | ||
</button> | ||
<pre>{JSON.stringify(listQuery.data ?? null, null, 4)}</pre> | ||
</> | ||
); | ||
} | ||
|
||
const $ = render( | ||
<ctx.App> | ||
<MyComp /> | ||
</ctx.App>, | ||
); | ||
|
||
await userEvent.click($.getByTestId('add')); | ||
|
||
await waitFor(() => { | ||
expect($.container).toHaveTextContent(nonce); | ||
}); | ||
}); | ||
|
||
test('skip invalidate', async () => { | ||
const { trpc } = ctx; | ||
const nonce = `nonce-${Math.random()}`; | ||
const onSuccessLocalSpy = vi.fn().mockReturnValue('something'); | ||
|
||
function MyComp() { | ||
const listQuery = trpc.list.useQuery(); | ||
const mutation = trpc.add.useMutation({ | ||
onSuccess: () => onSuccessLocalSpy(), | ||
}); | ||
|
||
return ( | ||
<> | ||
<button | ||
onClick={() => { | ||
mutation.mutate(nonce); | ||
}} | ||
data-testid="add" | ||
> | ||
add | ||
</button> | ||
<pre>{JSON.stringify(listQuery.data ?? null, null, 4)}</pre> | ||
</> | ||
); | ||
} | ||
|
||
const $ = render( | ||
<ctx.App> | ||
<MyComp /> | ||
</ctx.App>, | ||
); | ||
|
||
await userEvent.click($.getByTestId('add')); | ||
|
||
await waitFor(() => { | ||
expect(ctx.onSuccessSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(onSuccessLocalSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect($.container).not.toHaveTextContent(nonce); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.