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