|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import { MemoryRouter } from 'react-router-dom'; |
| 4 | +import { ThemeProvider, createTheme } from '@mui/material/styles'; |
| 5 | +import { AppDataProvider, Layout } from './Layout'; |
| 6 | +import { AppDataContext } from '../hooks/useLayoutContext'; |
| 7 | +import { useContext } from 'react'; |
| 8 | + |
| 9 | +vi.mock('react-helmet-async', () => ({ |
| 10 | + Helmet: ({ children }: { children: React.ReactNode }) => <>{children}</>, |
| 11 | +})); |
| 12 | + |
| 13 | +// jsdom does not have requestIdleCallback / cancelIdleCallback |
| 14 | +vi.stubGlobal( |
| 15 | + 'requestIdleCallback', |
| 16 | + vi.fn((cb: IdleRequestCallback) => { |
| 17 | + const id = setTimeout(() => cb({} as IdleDeadline), 0); |
| 18 | + return id as unknown as number; |
| 19 | + }), |
| 20 | +); |
| 21 | +vi.stubGlobal('cancelIdleCallback', vi.fn((id: number) => clearTimeout(id))); |
| 22 | + |
| 23 | +const theme = createTheme(); |
| 24 | + |
| 25 | +function wrap(ui: React.ReactElement) { |
| 26 | + return render(ui, { |
| 27 | + wrapper: ({ children }) => ( |
| 28 | + <ThemeProvider theme={theme}> |
| 29 | + <MemoryRouter>{children}</MemoryRouter> |
| 30 | + </ThemeProvider> |
| 31 | + ), |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +describe('Layout', () => { |
| 36 | + it('renders children via Outlet', () => { |
| 37 | + // Layout uses <Outlet />, which renders nothing without route context, |
| 38 | + // but the wrapper itself renders without errors. |
| 39 | + wrap(<Layout />); |
| 40 | + |
| 41 | + // The main Box should be present |
| 42 | + const main = document.querySelector('main'); |
| 43 | + expect(main).toBeInTheDocument(); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('AppDataProvider', () => { |
| 48 | + beforeEach(() => { |
| 49 | + vi.restoreAllMocks(); |
| 50 | + // Re-stub after restoreAllMocks clears them |
| 51 | + vi.stubGlobal( |
| 52 | + 'requestIdleCallback', |
| 53 | + vi.fn((cb: IdleRequestCallback) => { |
| 54 | + const id = setTimeout(() => cb({} as IdleDeadline), 0); |
| 55 | + return id as unknown as number; |
| 56 | + }), |
| 57 | + ); |
| 58 | + vi.stubGlobal('cancelIdleCallback', vi.fn((id: number) => clearTimeout(id))); |
| 59 | + }); |
| 60 | + |
| 61 | + it('provides context to children', async () => { |
| 62 | + vi.stubGlobal( |
| 63 | + 'fetch', |
| 64 | + vi.fn().mockResolvedValue({ |
| 65 | + ok: true, |
| 66 | + json: () => Promise.resolve({ specs: [], libraries: [], specs_count: 0, plots_count: 0, libraries_count: 0 }), |
| 67 | + }), |
| 68 | + ); |
| 69 | + |
| 70 | + function Consumer() { |
| 71 | + const ctx = useContext(AppDataContext); |
| 72 | + return <div data-testid="ctx">{ctx ? 'has-context' : 'no-context'}</div>; |
| 73 | + } |
| 74 | + |
| 75 | + wrap( |
| 76 | + <AppDataProvider> |
| 77 | + <Consumer /> |
| 78 | + </AppDataProvider>, |
| 79 | + ); |
| 80 | + |
| 81 | + expect(screen.getByTestId('ctx')).toHaveTextContent('has-context'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('calls fetch for /specs, /libraries, and /stats', async () => { |
| 85 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 86 | + ok: true, |
| 87 | + json: () => Promise.resolve({}), |
| 88 | + }); |
| 89 | + vi.stubGlobal('fetch', fetchMock); |
| 90 | + |
| 91 | + wrap( |
| 92 | + <AppDataProvider> |
| 93 | + <div>child</div> |
| 94 | + </AppDataProvider>, |
| 95 | + ); |
| 96 | + |
| 97 | + await waitFor(() => { |
| 98 | + expect(fetchMock).toHaveBeenCalledTimes(3); |
| 99 | + }); |
| 100 | + |
| 101 | + const urls = fetchMock.mock.calls.map((c: unknown[]) => c[0] as string); |
| 102 | + expect(urls.some((u: string) => u.includes('/specs'))).toBe(true); |
| 103 | + expect(urls.some((u: string) => u.includes('/libraries'))).toBe(true); |
| 104 | + expect(urls.some((u: string) => u.includes('/stats'))).toBe(true); |
| 105 | + }); |
| 106 | + |
| 107 | + it('handles fetch failure gracefully', async () => { |
| 108 | + const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 109 | + vi.stubGlobal( |
| 110 | + 'fetch', |
| 111 | + vi.fn().mockRejectedValue(new Error('Network error')), |
| 112 | + ); |
| 113 | + |
| 114 | + wrap( |
| 115 | + <AppDataProvider> |
| 116 | + <div data-testid="child">still renders</div> |
| 117 | + </AppDataProvider>, |
| 118 | + ); |
| 119 | + |
| 120 | + await waitFor(() => { |
| 121 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 122 | + 'Initial data load incomplete:', |
| 123 | + 'Network error', |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + expect(screen.getByTestId('child')).toHaveTextContent('still renders'); |
| 128 | + consoleSpy.mockRestore(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments