|
| 1 | +import { useQuery } from '@apollo/client' |
| 2 | +import { screen, waitFor, fireEvent } from '@testing-library/react' |
| 3 | +import { act } from 'react' |
| 4 | +import { useNavigate } from 'react-router-dom' |
| 5 | +import { render } from 'wrappers/testUtil' |
| 6 | +import { toaster } from 'components/ui/toaster' |
| 7 | +import SnapshotsPage from 'pages/Snapshots' |
| 8 | + |
| 9 | +jest.mock('components/ui/toaster', () => ({ |
| 10 | + toaster: { |
| 11 | + create: jest.fn(), |
| 12 | + }, |
| 13 | +})) |
| 14 | + |
| 15 | +jest.mock('react-router-dom', () => ({ |
| 16 | + ...jest.requireActual('react-router-dom'), |
| 17 | + useNavigate: jest.fn(), |
| 18 | +})) |
| 19 | + |
| 20 | +jest.mock('@apollo/client', () => ({ |
| 21 | + ...jest.requireActual('@apollo/client'), |
| 22 | + useQuery: jest.fn(), |
| 23 | +})) |
| 24 | + |
| 25 | +const mockSnapshots = [ |
| 26 | + { |
| 27 | + key: '2024-12', |
| 28 | + title: 'Snapshot 1', |
| 29 | + startAt: '2023-01-01T00:00:00Z', |
| 30 | + endAt: '2023-01-02T00:00:00Z', |
| 31 | + }, |
| 32 | + { |
| 33 | + key: '2024-11', |
| 34 | + title: 'Snapshot 2', |
| 35 | + startAt: '2022-12-01T00:00:00Z', |
| 36 | + endAt: '2022-12-31T23:59:59Z', |
| 37 | + }, |
| 38 | +] |
| 39 | + |
| 40 | +describe('SnapshotsPage', () => { |
| 41 | + beforeEach(() => { |
| 42 | + ;(useQuery as jest.Mock).mockReturnValue({ |
| 43 | + data: { snapshots: mockSnapshots }, |
| 44 | + error: null, |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + jest.clearAllMocks() |
| 50 | + }) |
| 51 | + |
| 52 | + it('renders loading spinner initially', async () => { |
| 53 | + ;(useQuery as jest.Mock).mockReturnValue({ |
| 54 | + data: null, |
| 55 | + error: null, |
| 56 | + }) |
| 57 | + |
| 58 | + render(<SnapshotsPage />) |
| 59 | + |
| 60 | + await waitFor(() => { |
| 61 | + const loadingSpinners = screen.getAllByAltText('Loading indicator') |
| 62 | + expect(loadingSpinners.length).toBe(2) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('renders snapshots when data is fetched successfully', async () => { |
| 67 | + render(<SnapshotsPage />) |
| 68 | + |
| 69 | + await waitFor(() => { |
| 70 | + expect(screen.getByText('Snapshot 1')).toBeInTheDocument() |
| 71 | + expect(screen.getByText('Snapshot 2')).toBeInTheDocument() |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + it('renders "No Snapshots found" when no snapshots are available', async () => { |
| 76 | + ;(useQuery as jest.Mock).mockReturnValue({ |
| 77 | + data: { snapshots: [] }, |
| 78 | + error: null, |
| 79 | + }) |
| 80 | + |
| 81 | + render(<SnapshotsPage />) |
| 82 | + |
| 83 | + await waitFor(() => { |
| 84 | + expect(screen.getByText('No Snapshots found')).toBeInTheDocument() |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + it('shows an error toaster when GraphQL request fails', async () => { |
| 89 | + ;(useQuery as jest.Mock).mockReturnValue({ |
| 90 | + data: null, |
| 91 | + error: new Error('GraphQL error'), |
| 92 | + }) |
| 93 | + |
| 94 | + render(<SnapshotsPage />) |
| 95 | + |
| 96 | + await waitFor(() => { |
| 97 | + expect(toaster.create).toHaveBeenCalledWith({ |
| 98 | + description: 'Unable to complete the requested operation.', |
| 99 | + title: 'GraphQL Request Failed', |
| 100 | + type: 'error', |
| 101 | + }) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('navigates to the correct URL when "View Snapshot" button is clicked', async () => { |
| 106 | + const navigateMock = jest.fn() |
| 107 | + ;(useNavigate as jest.Mock).mockReturnValue(navigateMock) |
| 108 | + render(<SnapshotsPage />) |
| 109 | + |
| 110 | + // Wait for the "View Snapshot" button to appear |
| 111 | + const viewSnapshotButton = await screen.findAllByRole('button', { name: /view snapshot/i }) |
| 112 | + |
| 113 | + // Click the button |
| 114 | + await act(async () => { |
| 115 | + fireEvent.click(viewSnapshotButton[0]) |
| 116 | + }) |
| 117 | + |
| 118 | + // Check if navigate was called with the correct argument |
| 119 | + await waitFor(() => { |
| 120 | + expect(navigateMock).toHaveBeenCalledTimes(1) |
| 121 | + expect(navigateMock).toHaveBeenCalledWith('/community/snapshots/2024-12') |
| 122 | + }) |
| 123 | + }) |
| 124 | +}) |
0 commit comments