Open History to see the last commit
1import { queryKey, mockConsoleError } from '../../react/tests/utils'
2import { MutationCache, QueryClient } from '../..'
3β
4describe('mutationCache', () => {
5 describe('MutationCacheConfig.onError', () => {
6 test('should be called when a mutation errors', async () => {
7 const consoleMock = mockConsoleError()
8 const key = queryKey()
9 const onError = jest.fn()
10 const testCache = new MutationCache({ onError })
11 const testClient = new QueryClient({ mutationCache: testCache })
12β
13 try {
14 await testClient.executeMutation({
15 mutationKey: key,
16 variables: 'vars',
17 mutationFn: () => Promise.reject('error'),
18 onMutate: () => 'context',
19 })
20 } catch {
21 consoleMock.mockRestore()
22 }
23β
24 const mutation = testCache.getAll()[0]
25 expect(onError).toHaveBeenCalledWith('error', 'vars', 'context', mutation)
26 })
27 })
28 describe('MutationCacheConfig.onSuccess', () => {
29 test('should be called when a mutation is successful', async () => {
30 const consoleMock = mockConsoleError()
31 const key = queryKey()
32 const onSuccess = jest.fn()
33 const testCache = new MutationCache({ onSuccess })
34 const testClient = new QueryClient({ mutationCache: testCache })
35β
36 try {
37 await testClient.executeMutation({
38 mutationKey: key,
39 variables: 'vars',
40 mutationFn: () => Promise.resolve({ data: 5 }),
41 onMutate: () => 'context',
42 })
43 } catch {
44 consoleMock.mockRestore()
45 }
46β
47 const mutation = testCache.getAll()[0]
48 expect(onSuccess).toHaveBeenCalledWith(
49 { data: 5 },
50 'vars',
51 'context',
52 mutation
53 )
54 })
55 })
56β
57 describe('find', () => {
58 test('should filter correctly', async () => {
59 const testCache = new MutationCache()
60 const testClient = new QueryClient({ mutationCache: testCache })
61 const key = ['mutation', 'vars']
62 await testClient.executeMutation({
63 mutationKey: key,
64 variables: 'vars',
65 mutationFn: () => Promise.resolve(),
66 })
67 const [mutation] = testCache.getAll()
68 expect(testCache.find({ mutationKey: key })).toEqual(mutation)
69 expect(testCache.find({ mutationKey: 'mutation', exact: false })).toEqual(
70 mutation
71 )
72 expect(testCache.find({ mutationKey: 'unknown' })).toEqual(undefined)
73 expect(
74 testCache.find({ predicate: m => m.options.variables === 'vars' })
75 ).toEqual(mutation)
76 })
77 })
78β
79 describe('findAll', () => {
80 test('should filter correctly', async () => {
81 const testCache = new MutationCache()
82 const testClient = new QueryClient({ mutationCache: testCache })
83 await testClient.executeMutation({
84 mutationKey: ['a', 1],
85 variables: 1,
86 mutationFn: () => Promise.resolve(),
87 })
88 await testClient.executeMutation({
89 mutationKey: ['a', 2],
90 variables: 2,
91 mutationFn: () => Promise.resolve(),
92 })
93 await testClient.executeMutation({
94 mutationKey: 'b',
95 mutationFn: () => Promise.resolve(),
96 })
97β
98 const [mutation1, mutation2] = testCache.getAll()
99 expect(
100 testCache.findAll({ mutationKey: 'a', exact: false })
101 ).toHaveLength(2)
102 expect(testCache.find({ mutationKey: ['a', 1] })).toEqual(mutation1)
103 expect(testCache.findAll({ mutationKey: 'unknown' })).toEqual([])
104 expect(
105 testCache.findAll({ predicate: m => m.options.variables === 2 })
106 ).toEqual([mutation2])
107 })
108 })
109})