-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatchdog-panel.component.test.tsx
More file actions
83 lines (64 loc) · 2.82 KB
/
watchdog-panel.component.test.tsx
File metadata and controls
83 lines (64 loc) · 2.82 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { WatchdogPanel } from './watchdog-panel.component.js';
const apiMock = vi.hoisted(() => ({
config: vi.fn(),
saveConfig: vi.fn(),
}));
vi.mock('../api.service.js', () => ({ api: apiMock }));
const toastMock = vi.hoisted(() =>
Object.assign(vi.fn(), { success: vi.fn(), error: vi.fn() }),
);
vi.mock('sonner', () => ({ toast: toastMock }));
/** Default config returned by the mocked `api.config()`. */
const DEFAULT_CONFIG = {
watchdog_interval_minutes: 15,
watchdog_idle_checks: 4,
watchdog_min_packets: 100,
};
describe('WatchdogPanel', () => {
beforeEach(() => {
apiMock.config.mockResolvedValue(DEFAULT_CONFIG);
apiMock.saveConfig.mockResolvedValue(undefined);
toastMock.success.mockClear();
toastMock.error.mockClear();
});
it('should render the Watchdog Settings heading', () => {
render(<WatchdogPanel />);
expect(screen.getByText('Watchdog Settings')).toBeInTheDocument();
});
it('should render the Save button', () => {
render(<WatchdogPanel />);
expect(screen.getByRole('button', { name: /save/i })).toBeInTheDocument();
});
it('should render an accessible help button for each watchdog field', () => {
render(<WatchdogPanel />);
expect(screen.getByRole('button', { name: 'Check interval (min) help' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Idle checks before shutdown help' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Min packets (activity threshold) help' })).toBeInTheDocument();
});
it('should show a success toast after saving settings', async () => {
render(<WatchdogPanel />);
await userEvent.click(screen.getByRole('button', { name: /save/i }));
expect(toastMock.success).toHaveBeenCalledWith('Watchdog settings saved');
});
it('should show an error toast with the error message when saving fails', async () => {
apiMock.saveConfig.mockRejectedValueOnce(new Error('network timeout'));
render(<WatchdogPanel />);
await userEvent.click(screen.getByRole('button', { name: /save/i }));
expect(toastMock.error).toHaveBeenCalledWith(
'Failed to save watchdog settings',
expect.objectContaining({ description: 'network timeout' }),
);
});
it('should show the fallback description when saving fails with an unknown error', async () => {
apiMock.saveConfig.mockRejectedValueOnce('not an Error object');
render(<WatchdogPanel />);
await userEvent.click(screen.getByRole('button', { name: /save/i }));
expect(toastMock.error).toHaveBeenCalledWith(
'Failed to save watchdog settings',
expect.objectContaining({ description: 'An unknown error occurred' }),
);
});
});