|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | + |
| 3 | +import snackbar from '@/services/SnackbarUtils'; |
| 4 | +import { makeApiError } from '@/test/apiErrorHelpers'; |
| 5 | +import useApiError, { getStatus } from '../useApiError'; |
| 6 | + |
| 7 | +jest.mock('@/services/SnackbarUtils'); |
| 8 | + |
| 9 | +describe('useApiError', () => { |
| 10 | + beforeEach(() => { |
| 11 | + jest.clearAllMocks(); |
| 12 | + jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + (console.error as jest.Mock).mockRestore(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('getStatus', () => { |
| 20 | + test('reads the status attached by errorHandler', () => { |
| 21 | + expect(getStatus(makeApiError(404))).toBe(404); |
| 22 | + expect(getStatus(makeApiError(500, 'Server Error'))).toBe(500); |
| 23 | + }); |
| 24 | + |
| 25 | + test('is undefined for errors without a response', () => { |
| 26 | + expect(getStatus(new Error('network down'))).toBeUndefined(); |
| 27 | + expect(getStatus(undefined)).toBeUndefined(); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('in the browser view', () => { |
| 32 | + test('reportError shows a labelled snackbar', () => { |
| 33 | + const { result } = renderHook(() => useApiError(false)); |
| 34 | + |
| 35 | + result.current.reportError('Failed to load comparators', makeApiError(404)); |
| 36 | + |
| 37 | + expect(snackbar.error).toHaveBeenCalledTimes(1); |
| 38 | + expect(snackbar.error).toHaveBeenCalledWith('Failed to load comparators: Not Found'); |
| 39 | + }); |
| 40 | + |
| 41 | + test('reportErrorSkip404 logs but shows no snackbar on 404', () => { |
| 42 | + const { result } = renderHook(() => useApiError(false)); |
| 43 | + |
| 44 | + result.current.reportErrorSkip404('Failed to load TMB', makeApiError(404)); |
| 45 | + |
| 46 | + expect(snackbar.error).not.toHaveBeenCalled(); |
| 47 | + expect(console.error).toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + |
| 50 | + test('reportErrorSkip404 still reports other statuses', () => { |
| 51 | + const { result } = renderHook(() => useApiError(false)); |
| 52 | + |
| 53 | + result.current.reportErrorSkip404('Failed to load TMB', makeApiError(500, 'Server Error')); |
| 54 | + |
| 55 | + expect(snackbar.error).toHaveBeenCalledWith('Failed to load TMB: Server Error'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('queryOnError returns a handler usable as a react-query onError', () => { |
| 59 | + const { result } = renderHook(() => useApiError(false)); |
| 60 | + |
| 61 | + result.current.queryOnError('Failed to load MSI')(makeApiError(404)); |
| 62 | + |
| 63 | + expect(snackbar.error).toHaveBeenCalledWith('Failed to load MSI: Not Found'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('in the print view', () => { |
| 68 | + test('reportError logs but shows no snackbar', () => { |
| 69 | + const { result } = renderHook(() => useApiError(true)); |
| 70 | + |
| 71 | + result.current.reportError('Failed to load comparators', makeApiError(404)); |
| 72 | + |
| 73 | + expect(snackbar.error).not.toHaveBeenCalled(); |
| 74 | + expect(console.error).toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + test('queryOnError shows no snackbar', () => { |
| 78 | + const { result } = renderHook(() => useApiError(true)); |
| 79 | + |
| 80 | + result.current.queryOnError('Failed to load MSI')(makeApiError(500, 'Server Error')); |
| 81 | + |
| 82 | + expect(snackbar.error).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + test('handlers are stable across re-renders with the same isPrint', () => { |
| 87 | + const { result, rerender } = renderHook(({ isPrint }) => useApiError(isPrint), { |
| 88 | + initialProps: { isPrint: false }, |
| 89 | + }); |
| 90 | + const first = result.current; |
| 91 | + |
| 92 | + rerender({ isPrint: false }); |
| 93 | + |
| 94 | + expect(result.current.reportError).toBe(first.reportError); |
| 95 | + expect(result.current.queryOnError).toBe(first.queryOnError); |
| 96 | + }); |
| 97 | +}); |
0 commit comments