-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathuseUpdateBundleCache.test.tsx
197 lines (171 loc) · 4.94 KB
/
useUpdateBundleCache.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {
QueryClientProvider as QueryClientProviderV5,
QueryClient as QueryClientV5,
} from '@tanstack/react-queryV5'
import { act, renderHook, waitFor } from '@testing-library/react'
import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { useUpdateBundleCache } from './useUpdateBundleCache'
const mockSuccessfulResponse = {
data: {
updateBundleCacheConfig: {
results: [{ bundleName: 'bundle-1', isCached: true }],
error: null,
},
},
}
const mockParsingError = {
data: null,
errors: [{ message: 'Parsing error' }],
}
const mockUnauthenticatedError = {
data: {
updateBundleCacheConfig: {
results: null,
error: {
__typename: 'UnauthenticatedError',
message: 'Unauthenticated error',
},
},
},
}
const mockValidationError = {
data: {
updateBundleCacheConfig: {
results: null,
error: { __typename: 'ValidationError', message: 'Validation error' },
},
},
}
const queryClient = new QueryClientV5({
defaultOptions: { mutations: { retry: false } },
})
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<QueryClientProviderV5 client={queryClient}>{children}</QueryClientProviderV5>
)
const server = setupServer()
beforeAll(() => {
server.listen()
})
afterEach(() => {
queryClient.clear()
server.resetHandlers()
})
afterAll(() => {
server.close()
})
interface SetupArgs {
isParsingError?: boolean
isUnauthenticatedError?: boolean
isValidationError?: boolean
}
describe('useUpdateBundleCache', () => {
function setup({
isParsingError = false,
isUnauthenticatedError = false,
isValidationError = false,
}: SetupArgs) {
server.use(
graphql.mutation('UpdateBundleCacheConfig', () => {
if (isParsingError) {
return HttpResponse.json(mockParsingError)
} else if (isUnauthenticatedError) {
return HttpResponse.json(mockUnauthenticatedError)
} else if (isValidationError) {
return HttpResponse.json(mockValidationError)
}
return HttpResponse.json(mockSuccessfulResponse)
})
)
}
describe('when the mutation is successful', () => {
it('returns the updated results', async () => {
setup({})
const { result } = renderHook(
() =>
useUpdateBundleCache({
provider: 'gh',
owner: 'owner',
repo: 'repo',
}),
{ wrapper }
)
act(() =>
result.current.mutate([{ bundleName: 'bundle-1', isCached: true }])
)
await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(result.current.data).toEqual([
{ bundleName: 'bundle-1', isCached: true },
])
})
})
describe('when the mutation fails', () => {
describe('when the mutation fails with a parsing error', () => {
it('returns a parsing error', async () => {
setup({ isParsingError: true })
const { result } = renderHook(
() =>
useUpdateBundleCache({
provider: 'gh',
owner: 'owner',
repo: 'repo',
}),
{ wrapper }
)
act(() =>
result.current.mutate([{ bundleName: 'bundle-1', isCached: true }])
)
await waitFor(() => expect(result.current.isError).toBe(true))
expect(result.current.error).toEqual({
data: {},
dev: 'useUpdateBundleCache - 400 failed to parse data',
status: 400,
})
})
})
describe('when the mutation fails with an unauthenticated error', () => {
it('returns an unauthenticated error', async () => {
setup({ isUnauthenticatedError: true })
const { result } = renderHook(
() =>
useUpdateBundleCache({
provider: 'gh',
owner: 'owner',
repo: 'repo',
}),
{ wrapper }
)
act(() =>
result.current.mutate([{ bundleName: 'bundle-1', isCached: true }])
)
await waitFor(() => expect(result.current.isError).toBe(true))
expect(result.current.error).toEqual({
error: 'UnauthenticatedError',
message: 'Unauthenticated error',
})
})
})
describe('when the mutation fails with a validation error', () => {
it('returns a validation error', async () => {
setup({ isValidationError: true })
const { result } = renderHook(
() =>
useUpdateBundleCache({
provider: 'gh',
owner: 'owner',
repo: 'repo',
}),
{ wrapper }
)
act(() =>
result.current.mutate([{ bundleName: 'bundle-1', isCached: true }])
)
await waitFor(() => expect(result.current.isError).toBe(true))
expect(result.current.error).toEqual({
error: 'ValidationError',
message: 'Validation error',
})
})
})
})
})