Skip to content

Commit

Permalink
fix(Notification test): rewrite notifications test file when user rol…
Browse files Browse the repository at this point in the history
…e is admin.
  • Loading branch information
Hakizimana-Clement committed Aug 1, 2024
1 parent ac1e528 commit 88d74ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 135 deletions.
1 change: 1 addition & 0 deletions src/pages/SingleProduct.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//import { FaCartPlus, FaPlus, FaMinus } from 'react-icons/fa';
import { useAppDispatch, useAppSelector } from '../redux/hooks/hooks';
import { useEffect, useState } from 'react';
import { IoMdArrowDropdown } from 'react-icons/io';
Expand Down
145 changes: 10 additions & 135 deletions test/components/notifications/Notifications.test.tsx
Original file line number Diff line number Diff line change
@@ -1,121 +1,13 @@
// /* eslint-disable @typescript-eslint/no-var-requires */
// import { render, screen, waitFor } from '@testing-library/react';
// import { it, expect, describe, vi } from 'vitest';
// import Notification from '../../../src/components/notification/Notification';
// import AllProvider from '../../Utils/AllProvider';
// import userEvent from '@testing-library/user-event';
// import { addNotification } from '../../../src/redux/features/notificationSlice';
// import { store } from '../../../src/redux/store';
// import { toast } from 'sonner';
// import { DynamicData } from '../../../src/@types/DynamicData';

// vi.spyOn(window.HTMLMediaElement.prototype, 'play').mockImplementation(() =>
// Promise.resolve(),
// );

// vi.mock('sonner', () => ({
// toast: {
// error: vi.fn(),
// success: vi.fn(),
// },
// }));

// describe('Notification component', () => {
// const renderComponent = () => {
// render(<Notification />, { wrapper: AllProvider });
// return {
// user: userEvent.setup(),
// bell: screen.getByRole('img'),
// };
// };

// it('should render a notification component with a bell icon', () => {
// const { bell } = renderComponent();
// expect(bell).toBeInTheDocument();
// });

// it('should show notification tab when a user clicks on the bell', async () => {
// const notification = {
// id: '1',
// message: 'Test Notification',
// unread: true,
// createdAt: new Date().toISOString(),
// };
// store.dispatch(addNotification(notification));

// const { bell, user } = renderComponent();
// await user.click(bell);
// expect(screen.getByLabelText('notification-tab')).toBeInTheDocument();
// expect(screen.getByText('Test Notification')).toBeInTheDocument();
// });

// it('should show a toast when a new message is received', async () => {
// const { bell, user } = renderComponent();
// const socket = require('socket.io-client')(
// import.meta.env.VITE_API_APP_ROOT_URL,
// );

// socket.emit('notification-user1', {
// id: '2',
// message: 'New Notification',
// unread: true,
// createdAt: new Date().toISOString(),
// });

// const notificatioNber = screen.getByLabelText('notification-number');
// expect(notificatioNber).toHaveTextContent('1');

// await user.click(bell);

// const notificationCard = screen.getByText(/new/i);
// await user.click(notificationCard);
// });

// it('should handle errors in readAllNotification function', async () => {
// const { bell, user } = renderComponent();
// await user.click(bell);
// const markButton = screen.getByLabelText('mark-button');
// vi.spyOn(store, 'dispatch').mockImplementationOnce(() => {
// throw new Error('Error');
// });
// await user.click(markButton);
// await waitFor(() => {
// expect(toast.error).toHaveBeenCalledWith(
// 'Unknown error occurred! Please try again!',
// );
// });
// });
// it('should handle errors in readAllNotification function', async () => {
// const { bell, user } = renderComponent();
// await user.click(bell);
// const markButton = screen.getByLabelText('mark-button');
// const error = { data: { message: 'Test error' } };
// (vi.spyOn(store, 'dispatch') as DynamicData).mockImplementation(() => {
// return {
// unwrap: () => Promise.reject(error),
// };
// });
// await user.click(markButton);
// await waitFor(() => {
// expect(toast.error).toHaveBeenCalledWith(
// 'Unknown error occurred! Please try again!',
// );
// });
// });
// });
/* eslint-disable @typescript-eslint/no-var-requires */
import { render, screen, waitFor } from '@testing-library/react';
import { it, expect, describe, vi, beforeEach } from 'vitest';
import { it, expect, describe, vi } from 'vitest';
import Notification from '../../../src/components/notification/Notification';
import AllProvider from '../../Utils/AllProvider';
import userEvent from '@testing-library/user-event';
import {
addNotification,
markAllRead,
} from '../../../src/redux/features/notificationSlice';
import { addNotification } from '../../../src/redux/features/notificationSlice';
import { store } from '../../../src/redux/store';
import { toast } from 'sonner';
import { DynamicData } from '../../../src/@types/DynamicData';
import { io } from 'socket.io-client';

vi.spyOn(window.HTMLMediaElement.prototype, 'play').mockImplementation(() =>
Promise.resolve(),
Expand All @@ -127,33 +19,16 @@ vi.mock('sonner', () => ({
success: vi.fn(),
},
}));
vi.mock('socket.io-client', () => {
//@ts-ignore
const originalModule = vi.requireActual('socket.io-client');
return {
...originalModule,
__esModule: true,
default: vi.fn(() => ({
on: vi.fn(),
emit: vi.fn(),
disconnect: vi.fn(),
})),
};
});

describe('Notification component', () => {
const renderComponent = () => {
render(<Notification />, { wrapper: AllProvider });
return {
user: userEvent.setup(),
bell: screen.getByRole('img', { name: /bell-image/i }),
bell: screen.getByRole('img'),
};
};

beforeEach(() => {
vi.clearAllMocks();
});

it('should render a notification component with a bell icon', () => {
const { bell } = renderComponent();
expect(bell).toBeInTheDocument();
Expand All @@ -176,7 +51,9 @@ describe('Notification component', () => {

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

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

const notificationNumber = screen.getByLabelText('notification-number');
expect(notificationNumber).toHaveTextContent('1');
const notificatioNber = screen.getByLabelText('notification-number');
expect(notificatioNber).toHaveTextContent('1');

await user.click(bell);

const notificationCard = screen.getByText(/new notification/i);
expect(notificationCard).toBeInTheDocument();
const notificationCard = screen.getByText(/new/i);
await user.click(notificationCard);
});

Expand All @@ -209,7 +85,6 @@ describe('Notification component', () => {
);
});
});

it('should handle errors in readAllNotification function', async () => {
const { bell, user } = renderComponent();
await user.click(bell);
Expand Down

0 comments on commit 88d74ed

Please sign in to comment.