forked from Cylo-Traders/Agrocylo-PIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToastContext.test.tsx
More file actions
62 lines (55 loc) · 1.8 KB
/
Copy pathToastContext.test.tsx
File metadata and controls
62 lines (55 loc) · 1.8 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
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ToastProvider, useToast } from './ToastContext';
function Probe() {
const toast = useToast();
return (
<div>
<button type="button" onClick={() => toast.success('Saved', 'All good')}>
Success
</button>
<button
type="button"
onClick={() => toast.error('Failed', 'Something broke')}
>
Failure
</button>
</div>
);
}
describe('ToastProvider', () => {
it('renders a success toast and allows dismissal', async () => {
const user = userEvent.setup();
render(
<ToastProvider>
<Probe />
</ToastProvider>,
);
await user.click(screen.getByRole('button', { name: 'Success' }));
expect(screen.getByRole('status')).toHaveTextContent('Saved');
expect(screen.getByRole('status')).toHaveTextContent('All good');
await user.click(
screen.getByRole('button', { name: /dismiss notification/i }),
);
expect(screen.queryByRole('status')).not.toBeInTheDocument();
});
it('renders an error toast with role="alert"', async () => {
const user = userEvent.setup();
render(
<ToastProvider>
<Probe />
</ToastProvider>,
);
await user.click(screen.getByRole('button', { name: 'Failure' }));
expect(screen.getByRole('alert')).toHaveTextContent('Failed');
expect(screen.getByRole('alert')).toHaveTextContent('Something broke');
});
it('throws when useToast is used outside the provider', () => {
const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
expect(() => render(<Probe />)).toThrow(
/useToast must be used within a ToastProvider/,
);
spy.mockRestore();
});
});