@tanstack/query-persist-client-core 4.10.1 → 4.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
import type { QueryClientConfig } from '@tanstack/query-core';
|
|
3
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
4
|
+
export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
|
|
5
|
+
export declare const mockLogger: {
|
|
6
|
+
log: jest.Mock<any, any>;
|
|
7
|
+
warn: jest.Mock<any, any>;
|
|
8
|
+
error: jest.Mock<any, any>;
|
|
9
|
+
};
|
|
10
|
+
export declare function sleep(timeout: number): Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-persist-client-core",
|
|
3
|
-
"version": "4.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "4.12.0",
|
|
4
|
+
"description": "Set of utilities for interacting with persisters, which can save your queryClient for later use",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "tanstack/query",
|
|
@@ -28,13 +28,15 @@
|
|
|
28
28
|
"src"
|
|
29
29
|
],
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@tanstack/query-core": "4.
|
|
31
|
+
"@tanstack/query-core": "4.12.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@tanstack/query-core": "4.
|
|
34
|
+
"@tanstack/query-core": "4.12.0"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"clean": "rm -rf ./build",
|
|
38
|
-
"test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src"
|
|
38
|
+
"test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src",
|
|
39
|
+
"test:jest": "../../node_modules/.bin/jest --config ./jest.config.ts",
|
|
40
|
+
"test:dev": "pnpm run test:jest --watch"
|
|
39
41
|
}
|
|
40
42
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createQueryClient, sleep } from './utils'
|
|
2
|
+
import type { PersistedClient, Persister } from '../persist'
|
|
3
|
+
import { persistQueryClientSubscribe } from '../persist'
|
|
4
|
+
|
|
5
|
+
const createMockPersister = (): Persister => {
|
|
6
|
+
let storedState: PersistedClient | undefined
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
async persistClient(persistClient: PersistedClient) {
|
|
10
|
+
storedState = persistClient
|
|
11
|
+
},
|
|
12
|
+
async restoreClient() {
|
|
13
|
+
await sleep(10)
|
|
14
|
+
return storedState
|
|
15
|
+
},
|
|
16
|
+
removeClient() {
|
|
17
|
+
storedState = undefined
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe('persistQueryClientSubscribe', () => {
|
|
23
|
+
test('should persist mutations', async () => {
|
|
24
|
+
const queryClient = createQueryClient()
|
|
25
|
+
|
|
26
|
+
const persister = createMockPersister()
|
|
27
|
+
|
|
28
|
+
const unsubscribe = persistQueryClientSubscribe({
|
|
29
|
+
queryClient,
|
|
30
|
+
persister,
|
|
31
|
+
dehydrateOptions: { shouldDehydrateMutation: () => true },
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
queryClient.getMutationCache().build(queryClient, {
|
|
35
|
+
mutationFn: async (text: string) => text,
|
|
36
|
+
variables: 'todo',
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const result = await persister.restoreClient()
|
|
40
|
+
|
|
41
|
+
expect(result?.clientState.mutations).toHaveLength(1)
|
|
42
|
+
|
|
43
|
+
unsubscribe()
|
|
44
|
+
})
|
|
45
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { QueryClientConfig } from '@tanstack/query-core'
|
|
2
|
+
import { QueryClient } from '@tanstack/query-core'
|
|
3
|
+
|
|
4
|
+
export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
5
|
+
jest.spyOn(console, 'error').mockImplementation(() => undefined)
|
|
6
|
+
return new QueryClient({ logger: mockLogger, ...config })
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const mockLogger = {
|
|
10
|
+
log: jest.fn(),
|
|
11
|
+
warn: jest.fn(),
|
|
12
|
+
error: jest.fn(),
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function sleep(timeout: number): Promise<void> {
|
|
16
|
+
return new Promise((resolve, _reject) => {
|
|
17
|
+
setTimeout(resolve, timeout)
|
|
18
|
+
})
|
|
19
|
+
}
|