|
| 1 | +import { waitFor, renderHook } from '@testing-library/react'; |
| 2 | +import nock from 'nock'; |
| 3 | +import { createTestQueryClient } from '@support/queryClient'; |
| 4 | +import { getFileHistory, getFileHistoryError } from '@support/nocks/fileStorage'; |
| 5 | +import useFileHistory from './useFileHistory'; |
| 6 | +import { getFileHistoryData } from '../../test/frontend/support/data/fileStorageData'; |
| 7 | + |
| 8 | +const createWrapper = createTestQueryClient(); |
| 9 | + |
| 10 | +const props = { |
| 11 | + fileStorageServiceId: 123, |
| 12 | + page: 1, |
| 13 | +}; |
| 14 | + |
| 15 | +describe('useFileHistory', () => { |
| 16 | + beforeEach(() => nock.cleanAll()); |
| 17 | + afterAll(() => nock.restore()); |
| 18 | + |
| 19 | + it('should fetch file history successfully', async () => { |
| 20 | + const expectedData = getFileHistoryData(props.page); |
| 21 | + await getFileHistory({ ...props }); |
| 22 | + |
| 23 | + const { result } = renderHook( |
| 24 | + () => useFileHistory(props.fileStorageServiceId, props.page), |
| 25 | + { wrapper: createWrapper() }, |
| 26 | + ); |
| 27 | + |
| 28 | + await waitFor(() => expect(!result.current.isPending).toBe(true)); |
| 29 | + |
| 30 | + expect(result.current.data).toEqual(expectedData.data); |
| 31 | + expect(result.current.currentPage).toEqual(expectedData.currentPage); |
| 32 | + expect(result.current.totalPages).toEqual(expectedData.totalPages); |
| 33 | + expect(result.current.totalItems).toEqual(expectedData.totalItems); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should fetch the second page of results correctly', async () => { |
| 37 | + const pageNumber = 2; |
| 38 | + const expectedData = getFileHistoryData(pageNumber); |
| 39 | + await getFileHistory({ ...props, page: pageNumber }); |
| 40 | + |
| 41 | + const { result } = renderHook( |
| 42 | + () => useFileHistory(props.fileStorageServiceId, pageNumber), |
| 43 | + { wrapper: createWrapper() }, |
| 44 | + ); |
| 45 | + |
| 46 | + await waitFor(() => expect(!result.current.isPending).toBe(true)); |
| 47 | + |
| 48 | + expect(result.current.data).toEqual(expectedData.data); |
| 49 | + expect(result.current.data.length).toEqual(expectedData.data.length); |
| 50 | + expect(result.current.currentPage).toEqual(expectedData.currentPage); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return an error when fetching history fails', async () => { |
| 54 | + await getFileHistoryError({ ...props }); |
| 55 | + |
| 56 | + const { result } = renderHook(() => useFileHistory(props.fileStorageServiceId), { |
| 57 | + wrapper: createWrapper(), |
| 58 | + }); |
| 59 | + |
| 60 | + await waitFor(() => expect(result.current.error).toBeInstanceOf(Error)); |
| 61 | + expect(result.current.error.message).toBe('Failed to fetch storage history'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should not fetch when fileStorageServiceId is not provided', async () => { |
| 65 | + const { result } = renderHook(() => useFileHistory(), { |
| 66 | + wrapper: createWrapper(), |
| 67 | + }); |
| 68 | + expect(result.current.isPending).toBe(true); |
| 69 | + expect(result.current.error).toBe(null); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should refetch when page changes', async () => { |
| 73 | + await getFileHistory({ ...props }); |
| 74 | + const { result, rerender } = renderHook( |
| 75 | + ({ page }) => useFileHistory(props.fileStorageServiceId, page), |
| 76 | + { |
| 77 | + wrapper: createWrapper(), |
| 78 | + initialProps: { page: 1 }, |
| 79 | + }, |
| 80 | + ); |
| 81 | + |
| 82 | + await waitFor(() => expect(!result.current.isPending).toBe(true)); |
| 83 | + |
| 84 | + await getFileHistory({ ...props, page: 2 }); |
| 85 | + rerender({ page: 2 }); |
| 86 | + |
| 87 | + await waitFor(() => expect(result.current.currentPage).toBe(2)); |
| 88 | + }); |
| 89 | +}); |
0 commit comments