From e5758aefbac116377d5ea5c36d794b5f1ab4e56b Mon Sep 17 00:00:00 2001 From: kanhaiya04 Date: Fri, 29 Dec 2023 21:21:32 +0530 Subject: [PATCH] Added the test for the DeleteOrg component --- src/components/DeleteOrg/DeleteOrg.test.tsx | 175 +++++++++++++++----- 1 file changed, 138 insertions(+), 37 deletions(-) diff --git a/src/components/DeleteOrg/DeleteOrg.test.tsx b/src/components/DeleteOrg/DeleteOrg.test.tsx index e0a58351f7..6d442bba84 100644 --- a/src/components/DeleteOrg/DeleteOrg.test.tsx +++ b/src/components/DeleteOrg/DeleteOrg.test.tsx @@ -1,39 +1,97 @@ 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(); @@ -41,29 +99,33 @@ afterEach(() => { 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( + ); + 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( @@ -71,18 +133,27 @@ describe('Delete Organization Component', () => { + ); + 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( @@ -95,12 +166,18 @@ describe('Delete Organization Component', () => { ); - - 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( @@ -114,19 +191,26 @@ describe('Delete Organization Component', () => { ); - 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( - + @@ -136,15 +220,27 @@ describe('Delete Organization Component', () => { ); - 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( - + @@ -154,8 +250,13 @@ describe('Delete Organization Component', () => { ); - 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)); + }); }); });