Skip to content

Commit 88d74ed

Browse files
fix(Notification test): rewrite notifications test file when user role is admin.
1 parent ac1e528 commit 88d74ed

File tree

2 files changed

+11
-135
lines changed

2 files changed

+11
-135
lines changed

src/pages/SingleProduct.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//import { FaCartPlus, FaPlus, FaMinus } from 'react-icons/fa';
12
import { useAppDispatch, useAppSelector } from '../redux/hooks/hooks';
23
import { useEffect, useState } from 'react';
34
import { IoMdArrowDropdown } from 'react-icons/io';

test/components/notifications/Notifications.test.tsx

+10-135
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,13 @@
1-
// /* eslint-disable @typescript-eslint/no-var-requires */
2-
// import { render, screen, waitFor } from '@testing-library/react';
3-
// import { it, expect, describe, vi } from 'vitest';
4-
// import Notification from '../../../src/components/notification/Notification';
5-
// import AllProvider from '../../Utils/AllProvider';
6-
// import userEvent from '@testing-library/user-event';
7-
// import { addNotification } from '../../../src/redux/features/notificationSlice';
8-
// import { store } from '../../../src/redux/store';
9-
// import { toast } from 'sonner';
10-
// import { DynamicData } from '../../../src/@types/DynamicData';
11-
12-
// vi.spyOn(window.HTMLMediaElement.prototype, 'play').mockImplementation(() =>
13-
// Promise.resolve(),
14-
// );
15-
16-
// vi.mock('sonner', () => ({
17-
// toast: {
18-
// error: vi.fn(),
19-
// success: vi.fn(),
20-
// },
21-
// }));
22-
23-
// describe('Notification component', () => {
24-
// const renderComponent = () => {
25-
// render(<Notification />, { wrapper: AllProvider });
26-
// return {
27-
// user: userEvent.setup(),
28-
// bell: screen.getByRole('img'),
29-
// };
30-
// };
31-
32-
// it('should render a notification component with a bell icon', () => {
33-
// const { bell } = renderComponent();
34-
// expect(bell).toBeInTheDocument();
35-
// });
36-
37-
// it('should show notification tab when a user clicks on the bell', async () => {
38-
// const notification = {
39-
// id: '1',
40-
// message: 'Test Notification',
41-
// unread: true,
42-
// createdAt: new Date().toISOString(),
43-
// };
44-
// store.dispatch(addNotification(notification));
45-
46-
// const { bell, user } = renderComponent();
47-
// await user.click(bell);
48-
// expect(screen.getByLabelText('notification-tab')).toBeInTheDocument();
49-
// expect(screen.getByText('Test Notification')).toBeInTheDocument();
50-
// });
51-
52-
// it('should show a toast when a new message is received', async () => {
53-
// const { bell, user } = renderComponent();
54-
// const socket = require('socket.io-client')(
55-
// import.meta.env.VITE_API_APP_ROOT_URL,
56-
// );
57-
58-
// socket.emit('notification-user1', {
59-
// id: '2',
60-
// message: 'New Notification',
61-
// unread: true,
62-
// createdAt: new Date().toISOString(),
63-
// });
64-
65-
// const notificatioNber = screen.getByLabelText('notification-number');
66-
// expect(notificatioNber).toHaveTextContent('1');
67-
68-
// await user.click(bell);
69-
70-
// const notificationCard = screen.getByText(/new/i);
71-
// await user.click(notificationCard);
72-
// });
73-
74-
// it('should handle errors in readAllNotification function', async () => {
75-
// const { bell, user } = renderComponent();
76-
// await user.click(bell);
77-
// const markButton = screen.getByLabelText('mark-button');
78-
// vi.spyOn(store, 'dispatch').mockImplementationOnce(() => {
79-
// throw new Error('Error');
80-
// });
81-
// await user.click(markButton);
82-
// await waitFor(() => {
83-
// expect(toast.error).toHaveBeenCalledWith(
84-
// 'Unknown error occurred! Please try again!',
85-
// );
86-
// });
87-
// });
88-
// it('should handle errors in readAllNotification function', async () => {
89-
// const { bell, user } = renderComponent();
90-
// await user.click(bell);
91-
// const markButton = screen.getByLabelText('mark-button');
92-
// const error = { data: { message: 'Test error' } };
93-
// (vi.spyOn(store, 'dispatch') as DynamicData).mockImplementation(() => {
94-
// return {
95-
// unwrap: () => Promise.reject(error),
96-
// };
97-
// });
98-
// await user.click(markButton);
99-
// await waitFor(() => {
100-
// expect(toast.error).toHaveBeenCalledWith(
101-
// 'Unknown error occurred! Please try again!',
102-
// );
103-
// });
104-
// });
105-
// });
1+
/* eslint-disable @typescript-eslint/no-var-requires */
1062
import { render, screen, waitFor } from '@testing-library/react';
107-
import { it, expect, describe, vi, beforeEach } from 'vitest';
3+
import { it, expect, describe, vi } from 'vitest';
1084
import Notification from '../../../src/components/notification/Notification';
1095
import AllProvider from '../../Utils/AllProvider';
1106
import userEvent from '@testing-library/user-event';
111-
import {
112-
addNotification,
113-
markAllRead,
114-
} from '../../../src/redux/features/notificationSlice';
7+
import { addNotification } from '../../../src/redux/features/notificationSlice';
1158
import { store } from '../../../src/redux/store';
1169
import { toast } from 'sonner';
11710
import { DynamicData } from '../../../src/@types/DynamicData';
118-
import { io } from 'socket.io-client';
11911

12012
vi.spyOn(window.HTMLMediaElement.prototype, 'play').mockImplementation(() =>
12113
Promise.resolve(),
@@ -127,33 +19,16 @@ vi.mock('sonner', () => ({
12719
success: vi.fn(),
12820
},
12921
}));
130-
vi.mock('socket.io-client', () => {
131-
//@ts-ignore
132-
const originalModule = vi.requireActual('socket.io-client');
133-
return {
134-
...originalModule,
135-
__esModule: true,
136-
default: vi.fn(() => ({
137-
on: vi.fn(),
138-
emit: vi.fn(),
139-
disconnect: vi.fn(),
140-
})),
141-
};
142-
});
14322

14423
describe('Notification component', () => {
14524
const renderComponent = () => {
14625
render(<Notification />, { wrapper: AllProvider });
14726
return {
14827
user: userEvent.setup(),
149-
bell: screen.getByRole('img', { name: /bell-image/i }),
28+
bell: screen.getByRole('img'),
15029
};
15130
};
15231

153-
beforeEach(() => {
154-
vi.clearAllMocks();
155-
});
156-
15732
it('should render a notification component with a bell icon', () => {
15833
const { bell } = renderComponent();
15934
expect(bell).toBeInTheDocument();
@@ -176,7 +51,9 @@ describe('Notification component', () => {
17651

17752
it('should show a toast when a new message is received', async () => {
17853
const { bell, user } = renderComponent();
179-
const socket = io(import.meta.env.VITE_API_APP_ROOT_URL);
54+
const socket = require('socket.io-client')(
55+
import.meta.env.VITE_API_APP_ROOT_URL,
56+
);
18057

18158
socket.emit('notification-user1', {
18259
id: '2',
@@ -185,13 +62,12 @@ describe('Notification component', () => {
18562
createdAt: new Date().toISOString(),
18663
});
18764

188-
const notificationNumber = screen.getByLabelText('notification-number');
189-
expect(notificationNumber).toHaveTextContent('1');
65+
const notificatioNber = screen.getByLabelText('notification-number');
66+
expect(notificatioNber).toHaveTextContent('1');
19067

19168
await user.click(bell);
19269

193-
const notificationCard = screen.getByText(/new notification/i);
194-
expect(notificationCard).toBeInTheDocument();
70+
const notificationCard = screen.getByText(/new/i);
19571
await user.click(notificationCard);
19672
});
19773

@@ -209,7 +85,6 @@ describe('Notification component', () => {
20985
);
21086
});
21187
});
212-
21388
it('should handle errors in readAllNotification function', async () => {
21489
const { bell, user } = renderComponent();
21590
await user.click(bell);

0 commit comments

Comments
 (0)