Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

persist.test.tsx

49 lines1.1 KB

1import { createQueryClient } from '../../reactjs/tests/utils'
2import { sleep } from '../../core/utils'
3import {
4 PersistedClient,
5 Persister,
6 persistQueryClientSubscribe,
7} from '../persist'
8​
9const createMockPersister = (): Persister => {
10 let storedState: PersistedClient | undefined
11​
12 return {
13 async persistClient(persistClient: PersistedClient) {
14 storedState = persistClient
15 },
16 async restoreClient() {
17 await sleep(10)
18 return storedState
19 },
20 removeClient() {
21 storedState = undefined
22 },
23 }
25​
26describe('persistQueryClientSubscribe', () => {
27 test('should persist mutations', async () => {
28 const queryClient = createQueryClient()
29​
30 const persister = createMockPersister()
31​
32 const unsubscribe = persistQueryClientSubscribe({
33 queryClient,
34 persister,
35 dehydrateOptions: { shouldDehydrateMutation: () => true },
36 })
37​
38 queryClient.getMutationCache().build(queryClient, {
39 mutationFn: async (text: string) => text,
40 variables: 'todo',
41 })
42​
43 const result = await persister.restoreClient()
44​
45 expect(result?.clientState.mutations).toHaveLength(1)
46​
47 unsubscribe()
48 })
49})