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 */
106
2
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' ;
108
4
import Notification from '../../../src/components/notification/Notification' ;
109
5
import AllProvider from '../../Utils/AllProvider' ;
110
6
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' ;
115
8
import { store } from '../../../src/redux/store' ;
116
9
import { toast } from 'sonner' ;
117
10
import { DynamicData } from '../../../src/@types/DynamicData' ;
118
- import { io } from 'socket.io-client' ;
119
11
120
12
vi . spyOn ( window . HTMLMediaElement . prototype , 'play' ) . mockImplementation ( ( ) =>
121
13
Promise . resolve ( ) ,
@@ -127,33 +19,16 @@ vi.mock('sonner', () => ({
127
19
success : vi . fn ( ) ,
128
20
} ,
129
21
} ) ) ;
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
- } ) ;
143
22
144
23
describe ( 'Notification component' , ( ) => {
145
24
const renderComponent = ( ) => {
146
25
render ( < Notification /> , { wrapper : AllProvider } ) ;
147
26
return {
148
27
user : userEvent . setup ( ) ,
149
- bell : screen . getByRole ( 'img' , { name : / b e l l - i m a g e / i } ) ,
28
+ bell : screen . getByRole ( 'img' ) ,
150
29
} ;
151
30
} ;
152
31
153
- beforeEach ( ( ) => {
154
- vi . clearAllMocks ( ) ;
155
- } ) ;
156
-
157
32
it ( 'should render a notification component with a bell icon' , ( ) => {
158
33
const { bell } = renderComponent ( ) ;
159
34
expect ( bell ) . toBeInTheDocument ( ) ;
@@ -176,7 +51,9 @@ describe('Notification component', () => {
176
51
177
52
it ( 'should show a toast when a new message is received' , async ( ) => {
178
53
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
+ ) ;
180
57
181
58
socket . emit ( 'notification-user1' , {
182
59
id : '2' ,
@@ -185,13 +62,12 @@ describe('Notification component', () => {
185
62
createdAt : new Date ( ) . toISOString ( ) ,
186
63
} ) ;
187
64
188
- const notificationNumber = screen . getByLabelText ( 'notification-number' ) ;
189
- expect ( notificationNumber ) . toHaveTextContent ( '1' ) ;
65
+ const notificatioNber = screen . getByLabelText ( 'notification-number' ) ;
66
+ expect ( notificatioNber ) . toHaveTextContent ( '1' ) ;
190
67
191
68
await user . click ( bell ) ;
192
69
193
- const notificationCard = screen . getByText ( / n e w n o t i f i c a t i o n / i) ;
194
- expect ( notificationCard ) . toBeInTheDocument ( ) ;
70
+ const notificationCard = screen . getByText ( / n e w / i) ;
195
71
await user . click ( notificationCard ) ;
196
72
} ) ;
197
73
@@ -209,7 +85,6 @@ describe('Notification component', () => {
209
85
) ;
210
86
} ) ;
211
87
} ) ;
212
-
213
88
it ( 'should handle errors in readAllNotification function' , async ( ) => {
214
89
const { bell, user } = renderComponent ( ) ;
215
90
await user . click ( bell ) ;
0 commit comments