Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
akanshaaa19 committed Aug 28, 2024
1 parent 9321060 commit d28ae47
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen, waitFor } from '@testing-library/react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MockedProvider } from '@apollo/client/testing';

Expand Down Expand Up @@ -60,3 +60,30 @@ test('Files other than .csv should raise a warning message upon upload', async (
).toBeInTheDocument();
});
});

test('it removes the selected file', async () => {
render(contactManagement);

fireEvent.click(screen.getByTestId('uploadFile'));

const csvContent = `name,phone,collection
John Doe,919876543210,"Optin collection,Optout Collection"
Virat Kohli,919876543220,Cricket`;
const file = new File([csvContent], 'test.csv', { type: 'text/csv' });

await waitFor(() => {
const fileInput = screen.getByTestId('uploadFile');
userEvent.upload(fileInput, file);
});

await waitFor(() => {
// the filename should be visible instead of Select .csv after upload
expect(screen.getByText('test.csv')).toBeInTheDocument();
});

fireEvent.click(screen.getByTestId('cross-icon'));

await waitFor(() => {
expect(screen.queryByText('test.csv')).not.toBeInTheDocument();
});
});
56 changes: 43 additions & 13 deletions src/containers/NotificationList/NotificationList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import {
markAllNotificationAsRead,
getInfoNotificationsQuery,
getStatus,
getStatusWithError,
} from 'mocks/Notifications';
import { setUserSession } from 'services/AuthService';
import { NotificationList } from './NotificationList';
import * as Notification from 'common/notification';

setUserSession(JSON.stringify({ roles: ['Admin'] }));

Expand All @@ -32,16 +34,21 @@ const mocks: any = [
getInfoNotificationsQuery({ severity: 'Critical' }),
getInfoNotificationsQuery({ severity: '' }),
getInfoNotificationsQuery(),
getStatus,
];

const notifications = (
<MockedProvider mocks={mocks} addTypename={false}>
<Router>
<NotificationList />
</Router>
</MockedProvider>
);
const notifications = (mock?: any) => {
let MOCKS = mocks;
if (mock) {
MOCKS = [...MOCKS, mock];
}
return (
<MockedProvider mocks={MOCKS} addTypename={false}>
<Router>
<NotificationList />
</Router>
</MockedProvider>
);
};

const mockedUsedNavigate = vi.fn();
vi.mock('react-router-dom', async () => ({
Expand All @@ -50,7 +57,7 @@ vi.mock('react-router-dom', async () => ({
}));

test('It should load notifications', async () => {
render(notifications);
render(notifications());

await waitFor(() => {
expect(screen.getByTestId('loading')).toBeInTheDocument();
Expand All @@ -75,7 +82,7 @@ test('It should load notifications', async () => {
});

test('click on forward arrrow', async () => {
render(notifications);
render(notifications(getStatus));

await waitFor(() => {
expect(screen.getByTestId('loading')).toBeInTheDocument();
Expand All @@ -97,7 +104,7 @@ test('click on forward arrrow', async () => {
});

test('it should show copy text and view option on clicking entity ', async () => {
const { getByTestId, getByText } = render(notifications);
const { getByTestId, getByText } = render(notifications());
await waitFor(() => {
const entityMenu = screen.getAllByTestId('NotificationRowMenu');
expect(entityMenu[0]).toBeInTheDocument();
Expand Down Expand Up @@ -126,7 +133,7 @@ test('it should show copy text and view option on clicking entity ', async () =>
});

test('it should show filter radio button', async () => {
render(notifications);
render(notifications());

await waitFor(() => {
const checkboxInput = screen.getAllByTestId('radio');
Expand All @@ -135,7 +142,7 @@ test('it should show filter radio button', async () => {
});

test('it should have Info, Warning and critical checkbox', async () => {
render(notifications);
render(notifications());

await waitFor(() => {
const checkboxInput = screen.getAllByTestId('radio');
Expand All @@ -145,3 +152,26 @@ test('it should have Info, Warning and critical checkbox', async () => {
expect(checkboxInput[3]).toHaveTextContent('All');
});
});

test('it should show "Contact import is in progress" message', async () => {
const notificationSpy = vi.spyOn(Notification, 'setNotification');
render(notifications(getStatusWithError));

await waitFor(() => {
expect(screen.getByTestId('loading')).toBeInTheDocument();
});

await waitFor(() => {
expect(screen.getByText('Notifications')).toBeInTheDocument();
});

const arrowButtons = screen.getAllByTestId('ArrowForwardIcon');

arrowButtons.forEach(async (button) => {
fireEvent.click(button);

await waitFor(() => {
expect(notificationSpy).toHaveBeenCalledWith();
});
});
});
30 changes: 28 additions & 2 deletions src/mocks/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,21 @@ export const getNotificationsQuery = {
{
category: 'Contact Upload',
entity: '{"user_job_id":1}',
id: '60',
id: '7',
isRead: true,
message: 'Contact upload completed',
severity: '"Information"',
updatedAt: '2024-08-09T05:50:00Z',
},
{
category: 'unknown category',
entity: '{}',
id: '8',
isRead: true,
message: '',
severity: '',
updatedAt: '2024-08-09T05:50:00Z',
},
],
},
},
Expand Down Expand Up @@ -221,9 +230,26 @@ export const getStatus = {
result: {
data: {
getContactUploadReport: {
csvRows: '',
csvRows: 'Contact import done',
error: null,
},
},
},
};

export const getStatusWithError = {
request: {
query: GET_CONTACT_IMPORT_STATUS,
variables: {
userJobId: 1,
},
},
result: {
data: {
getContactUploadReport: {
csvRows: null,
error: 'Contact upload is in progress',
},
},
},
};

0 comments on commit d28ae47

Please sign in to comment.