Open History to see the last commit
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 }
24}
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})