The Node.js® Website
1import { render } from '@testing-library/react';
2
3import { NotificationProvider } from '@/providers/notificationProvider';
4
5import useNotification from '../useNotification';
6
7describe('useNotification', () => {
8 it('should return the notification dispatch function', () => {
9 // Arrange
10 const TestComponent = () => {
11 const notificationDispatch = useNotification();
12 return (
13 <div>
14 {notificationDispatch ? 'Dispatch available' : 'Dispatch unavailable'}
15 </div>
16 );
17 };
18
19 // Act
20 const { getByText } = render(
21 <NotificationProvider>
22 <TestComponent />
23 </NotificationProvider>
24 );
25
26 // Assert
27 const result = getByText('Dispatch available');
28 expect(result).toBeInTheDocument();
29 });
30
31 it('should return null outside NotificationProvider', () => {
32 // Arrange
33 const TestComponent = () => {
34 const notificationDispatch = useNotification();
35 return (
36 <div>
37 {notificationDispatch ? 'Dispatch available' : 'Dispatch unavailable'}
38 </div>
39 );
40 };
41
42 // Act
43 const { queryByText } = render(<TestComponent />);
44
45 // Assert
46 const result = queryByText((content, element) => {
47 return element.textContent === 'Dispatch unavailable';
48 });
49
50 expect(result).toBeNull();
51 });
52});