Skip to content

Commit

Permalink
Added the test for the DeleteOrg component
Browse files Browse the repository at this point in the history
  • Loading branch information
kanhaiya04 committed Dec 29, 2023
1 parent 431f317 commit e5758ae
Showing 1 changed file with 138 additions and 37 deletions.
175 changes: 138 additions & 37 deletions src/components/DeleteOrg/DeleteOrg.test.tsx
Original file line number Diff line number Diff line change
@@ -1,88 +1,159 @@
import React from 'react';
import { MockedProvider } from '@apollo/react-testing';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import 'jest-location-mock';
import { I18nextProvider } from 'react-i18next';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';

import { DELETE_ORGANIZATION_MUTATION } from 'GraphQl/Mutations/mutations';
import {
DELETE_ORGANIZATION_MUTATION,
REMOVE_SAMPLE_ORGANIZATION_MUTATION,
} from 'GraphQl/Mutations/mutations';
import { act } from 'react-dom/test-utils';
import { store } from 'state/store';
import { StaticMockLink } from 'utils/StaticMockLink';
import i18nForTest from 'utils/i18nForTest';
import DeleteOrg from './DeleteOrg';
import { ToastContainer, toast } from 'react-toastify';
import { IS_SAMPLE_ORGANIZATION_QUERY } from 'GraphQl/Queries/Queries';

const MOCKS = [
{
request: {
query: IS_SAMPLE_ORGANIZATION_QUERY,
variables: {
isSampleOrganizationId: '123',
},
},
result: {
data: {
isSampleOrganization: true,
},
},
},
{
request: {
query: REMOVE_SAMPLE_ORGANIZATION_MUTATION,
},
result: {
data: {
removeSampleOrganization: true,
},
},
},
{
request: {
query: DELETE_ORGANIZATION_MUTATION,
variables: {
id: 123,
id: '456',
},
},
result: {
data: {
removeOrganization: {
_id: '456',
},
},
},
},
];

const MOCKS_WITH_ERROR = [
{
request: {
query: IS_SAMPLE_ORGANIZATION_QUERY,
variables: {
isSampleOrganizationId: '123',
},
},
result: {
data: {
removeOrganization: [
{
_id: 123,
},
],
isSampleOrganization: true,
},
},
},
{
request: {
query: DELETE_ORGANIZATION_MUTATION,
variables: {
id: '456',
},
},
error: new Error('Failed to delete organization'),
},
{
request: {
query: REMOVE_SAMPLE_ORGANIZATION_MUTATION,
},
error: new Error('Failed to delete sample organization'),
},
];

const link = new StaticMockLink(MOCKS, true);
const link2 = new StaticMockLink(MOCKS_WITH_ERROR, true);

afterEach(() => {
localStorage.clear();
});

describe('Delete Organization Component', () => {
test('should be able to Toggle Delete Organization Modal', async () => {
window.location.assign('/orgsetting/id=123');
window.location.assign('/orgsetting/id=456');
localStorage.setItem('UserType', 'SUPERADMIN');
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<DeleteOrg />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
screen.getByTestId(/openDeleteModalBtn/i).click();
expect(screen.getByTestId(/orgDeleteModal/i)).toBeInTheDocument();
screen.getByTestId(/closeDelOrgModalBtn/i).click();
await act(async () => {
expect(screen.queryByTestId(/orgDeleteModal/i)).not.toHaveFocus();
});
expect(window.location).toBeAt('/orgsetting/id=123');
expect(window.location).toBeAt('/orgsetting/id=456');
});

test('Delete organization functionality should work properly', async () => {
test('should be able to Toggle Delete Organization Modal When Organization is Sample Organization', async () => {
window.location.assign('/orgsetting/id=123');
localStorage.setItem('UserType', 'SUPERADMIN');
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<DeleteOrg />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
screen.getByTestId(/openDeleteModalBtn/i).click();
screen.getByTestId(/deleteOrganizationBtn/i).click();
expect(window.location).not.toBeNull();
expect(screen.getByTestId(/orgDeleteModal/i)).toBeInTheDocument();
screen.getByTestId(/closeDelOrgModalBtn/i).click();
await act(async () => {
expect(screen.queryByTestId(/orgDeleteModal/i)).not.toHaveFocus();
});
expect(window.location).toBeAt('/orgsetting/id=123');
});
test('should handle deletion failure gracefully', async () => {
window.location.assign('/orgsetting/id=456'); // Using an ID that triggers a failure

test('Delete organization functionality should work properly', async () => {
window.location.assign('/orgsetting/id=456');
localStorage.setItem('UserType', 'SUPERADMIN');
render(
<MockedProvider addTypename={false} link={link}>
Expand All @@ -95,12 +166,18 @@ describe('Delete Organization Component', () => {
</BrowserRouter>
</MockedProvider>
);

fireEvent.click(screen.getByTestId('openDeleteModalBtn'));
fireEvent.click(screen.getByTestId('deleteOrganizationBtn'));
expect(screen.queryByText(/Deletion failed!/i)).toBeNull();
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
screen.getByTestId(/openDeleteModalBtn/i).click();
screen.getByTestId(/deleteOrganizationBtn/i).click();
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
expect(window.location.replace).toHaveBeenCalledWith('/orglist');
});
test('should close the Delete Organization Modal when "Cancel" button is clicked', async () => {

test('Delete organization functionality should work properly for sample org', async () => {
window.location.assign('/orgsetting/id=123');
localStorage.setItem('UserType', 'SUPERADMIN');
render(
Expand All @@ -114,19 +191,26 @@ describe('Delete Organization Component', () => {
</BrowserRouter>
</MockedProvider>
);
fireEvent.click(screen.getByTestId('openDeleteModalBtn'));
expect(screen.getByTestId('orgDeleteModal')).toBeInTheDocument();
fireEvent.click(screen.getByTestId('closeDelOrgModalBtn'));
await waitFor(() => {
expect(screen.queryByTestId('orgDeleteModal')).toBeNull();
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
expect(window.location).toBeAt('/orgsetting/id=123');
screen.getByTestId(/openDeleteModalBtn/i).click();
screen.getByTestId(/deleteOrganizationBtn/i).click();
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
expect(window.location.replace).toHaveBeenCalledWith('/orglist');
});
test('should open the Delete Organization Modal when "Delete" button is clicked', async () => {

test('Error handling for IS_SAMPLE_ORGANIZATION_QUERY mock', async () => {
window.location.assign('/orgsetting/id=123');
localStorage.setItem('UserType', 'SUPERADMIN');
jest.spyOn(toast, 'error');
render(
<MockedProvider addTypename={false} link={link}>
<MockedProvider addTypename={false} link={link2}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
Expand All @@ -136,15 +220,27 @@ describe('Delete Organization Component', () => {
</BrowserRouter>
</MockedProvider>
);
fireEvent.click(screen.getByTestId('openDeleteModalBtn'));
expect(screen.getByTestId('orgDeleteModal')).toBeInTheDocument();
expect(window.location).toBeAt('/orgsetting/id=123');
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
screen.getByTestId(/openDeleteModalBtn/i).click();
screen.getByTestId(/deleteOrganizationBtn/i).click();
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
expect(toast.error).toHaveBeenCalledWith(
'Failed to delete sample organization'
);
});
test('render Delete Organization Modal when "Delete" button is clicked', async () => {
window.location.assign('/orgsetting/id=123');

test('Error handling for DELETE_ORGANIZATION_MUTATION mock', async () => {
window.location.assign('/orgsetting/id=456');
localStorage.setItem('UserType', 'SUPERADMIN');
render(
<MockedProvider addTypename={false} link={link}>
<MockedProvider addTypename={false} link={link2}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
Expand All @@ -154,8 +250,13 @@ describe('Delete Organization Component', () => {
</BrowserRouter>
</MockedProvider>
);
fireEvent.click(screen.getByTestId('openDeleteModalBtn'));
expect(screen.getByTestId('orgDeleteModal')).toBeInTheDocument();
expect(window.location).toBeAt('/orgsetting/id=123');
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
screen.getByTestId(/openDeleteModalBtn/i).click();
screen.getByTestId(/deleteOrganizationBtn/i).click();
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});
});
});

0 comments on commit e5758ae

Please sign in to comment.