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