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 describe('MutationCacheConfig.onMutate', () => {
57 test('should be called before a mutation executes', async () => {
58 const consoleMock = mockConsoleError()
59 const key = queryKey()
60 const onMutate = jest.fn()
61 const testCache = new MutationCache({ onMutate })
62 const testClient = new QueryClient({ mutationCache: testCache })
63β
64 try {
65 await testClient.executeMutation({
66 mutationKey: key,
67 variables: 'vars',
68 mutationFn: () => Promise.resolve({ data: 5 }),
69 onMutate: () => 'context',
70 })
71 } catch {
72 consoleMock.mockRestore()
73 }
74β
75 const mutation = testCache.getAll()[0]
76 expect(onMutate).toHaveBeenCalledWith('vars', mutation)
77 })
78 })
79β
80 describe('find', () => {
81 test('should filter correctly', async () => {
82 const testCache = new MutationCache()
83 const testClient = new QueryClient({ mutationCache: testCache })
84 const key = ['mutation', 'vars']
85 await testClient.executeMutation({
86 mutationKey: key,
87 variables: 'vars',
88 mutationFn: () => Promise.resolve(),
89 })
90 const [mutation] = testCache.getAll()
91 expect(testCache.find({ mutationKey: key })).toEqual(mutation)
92 expect(testCache.find({ mutationKey: 'mutation', exact: false })).toEqual(
93 mutation
94 )
95 expect(testCache.find({ mutationKey: 'unknown' })).toEqual(undefined)
96 expect(
97 testCache.find({ predicate: m => m.options.variables === 'vars' })
98 ).toEqual(mutation)
99 })
100 })
101β
102 describe('findAll', () => {
103 test('should filter correctly', async () => {
104 const testCache = new MutationCache()
105 const testClient = new QueryClient({ mutationCache: testCache })
106 await testClient.executeMutation({
107 mutationKey: ['a', 1],
108 variables: 1,
109 mutationFn: () => Promise.resolve(),
110 })
111 await testClient.executeMutation({
112 mutationKey: ['a', 2],
113 variables: 2,
114 mutationFn: () => Promise.resolve(),
115 })
116 await testClient.executeMutation({
117 mutationKey: 'b',
118 mutationFn: () => Promise.resolve(),
119 })
120β
121 const [mutation1, mutation2] = testCache.getAll()
122 expect(
123 testCache.findAll({ mutationKey: 'a', exact: false })
124 ).toHaveLength(2)
125 expect(testCache.find({ mutationKey: ['a', 1] })).toEqual(mutation1)
126 expect(testCache.findAll({ mutationKey: 'unknown' })).toEqual([])
127 expect(
128 testCache.findAll({ predicate: m => m.options.variables === 2 })
129 ).toEqual([mutation2])
130 })
131 })
132})