From 0daf4fa89b21653d13db700db47810f47ecc6cc0 Mon Sep 17 00:00:00 2001 From: kanhaiya04 Date: Sat, 30 Dec 2023 01:28:06 +0530 Subject: [PATCH] Test for src/screens/OrgList/OrgList.tsx --- src/screens/OrgList/OrgList.test.tsx | 187 ++++++++++++++++++++++----- src/screens/OrgList/OrgList.tsx | 8 +- src/screens/OrgList/OrgListMocks.ts | 73 ++++++++++- 3 files changed, 230 insertions(+), 38 deletions(-) diff --git a/src/screens/OrgList/OrgList.test.tsx b/src/screens/OrgList/OrgList.test.tsx index 5d716dd065..35acc85c45 100644 --- a/src/screens/OrgList/OrgList.test.tsx +++ b/src/screens/OrgList/OrgList.test.tsx @@ -6,6 +6,7 @@ import { screen, fireEvent, cleanup, + waitFor, } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import 'jest-localstorage-mock'; @@ -18,7 +19,13 @@ import { StaticMockLink } from 'utils/StaticMockLink'; import i18nForTest from 'utils/i18nForTest'; import OrgList from './OrgList'; -import { MOCKS, MOCKS_ADMIN, MOCKS_EMPTY } from './OrgListMocks'; +import { + MOCKS, + MOCKS_ADMIN, + MOCKS_EMPTY, + MOCKS_WITH_ERROR, +} from './OrgListMocks'; +import { ToastContainer, toast } from 'react-toastify'; async function wait(ms = 100): Promise { await act(() => { @@ -38,6 +45,7 @@ describe('Organisations Page testing as SuperAdmin', () => { const link = new StaticMockLink(MOCKS, true); const link2 = new StaticMockLink(MOCKS_EMPTY, true); + const link3 = new StaticMockLink(MOCKS_WITH_ERROR, true); const formData = { name: 'Dummy Organization', @@ -130,44 +138,99 @@ describe('Organisations Page testing as SuperAdmin', () => { test('Create organization model should work properly', async () => { localStorage.setItem('id', '123'); localStorage.setItem('UserType', 'SUPERADMIN'); - await act(async () => { - render( - - - + + render( + + + + + - - - - ); + + + + + ); - await wait(500); + await wait(500); - expect(localStorage.setItem).toHaveBeenLastCalledWith( - 'UserType', - 'SUPERADMIN' - ); + expect(localStorage.setItem).toHaveBeenLastCalledWith( + 'UserType', + 'SUPERADMIN' + ); - userEvent.click(screen.getByTestId(/createOrganizationBtn/i)); + userEvent.click(screen.getByTestId(/createOrganizationBtn/i)); - userEvent.type( - screen.getByTestId(/modalOrganizationName/i), - formData.name - ); - userEvent.type( - screen.getByPlaceholderText(/Description/i), - formData.description - ); - userEvent.type( - screen.getByPlaceholderText(/Location/i), - formData.location - ); - userEvent.click(screen.getByTestId(/isPublic/i)); - userEvent.click(screen.getByTestId(/visibleInSearch/i)); - userEvent.upload(screen.getByLabelText(/Display Image/i), formData.image); + userEvent.type(screen.getByTestId(/modalOrganizationName/i), formData.name); + userEvent.type( + screen.getByPlaceholderText(/Description/i), + formData.description + ); + userEvent.type(screen.getByPlaceholderText(/Location/i), formData.location); + userEvent.click(screen.getByTestId(/isPublic/i)); + userEvent.click(screen.getByTestId(/visibleInSearch/i)); + userEvent.upload(screen.getByLabelText(/Display Image/i), formData.image); - await wait(500); + expect(screen.getByTestId(/modalOrganizationName/i)).toHaveValue( + formData.name + ); + expect(screen.getByPlaceholderText(/Description/i)).toHaveValue( + formData.description + ); + expect(screen.getByPlaceholderText(/Location/i)).toHaveValue( + formData.location + ); + expect(screen.getByTestId(/isPublic/i)).not.toBeChecked(); + expect(screen.getByTestId(/visibleInSearch/i)).toBeChecked(); + expect(screen.getByLabelText(/Display Image/i)).toBeTruthy(); + + userEvent.click(screen.getByTestId(/submitOrganizationForm/i)); + await act(async () => { + await new Promise((resolve) => setTimeout(resolve, 1000)); }); + await waitFor(() => + expect( + screen.queryByText(/Congratulation the Organization is created/i) + ).toBeInTheDocument() + ); + userEvent.click(screen.getByTestId(/closeOrganizationModal/i)); + }); + + test('Plugin Notification model should work properly', async () => { + localStorage.setItem('id', '123'); + localStorage.setItem('UserType', 'SUPERADMIN'); + + render( + + + + + + + + + + + ); + + await wait(500); + + expect(localStorage.setItem).toHaveBeenLastCalledWith( + 'UserType', + 'SUPERADMIN' + ); + + userEvent.click(screen.getByTestId(/createOrganizationBtn/i)); + + userEvent.type(screen.getByTestId(/modalOrganizationName/i), formData.name); + userEvent.type( + screen.getByPlaceholderText(/Description/i), + formData.description + ); + userEvent.type(screen.getByPlaceholderText(/Location/i), formData.location); + userEvent.click(screen.getByTestId(/isPublic/i)); + userEvent.click(screen.getByTestId(/visibleInSearch/i)); + userEvent.upload(screen.getByLabelText(/Display Image/i), formData.image); expect(screen.getByTestId(/modalOrganizationName/i)).toHaveValue( formData.name @@ -183,7 +246,65 @@ describe('Organisations Page testing as SuperAdmin', () => { expect(screen.getByLabelText(/Display Image/i)).toBeTruthy(); userEvent.click(screen.getByTestId(/submitOrganizationForm/i)); - }, 10000); + await act(async () => { + await new Promise((resolve) => setTimeout(resolve, 1000)); + }); + await waitFor(() => + expect( + screen.queryByText(/Congratulation the Organization is created/i) + ).toBeInTheDocument() + ); + userEvent.click(screen.getByTestId(/enableEverythingForm/i)); + }); + + test('Testing create sample organization working properly', async () => { + localStorage.setItem('id', '123'); + localStorage.setItem('UserType', 'SUPERADMIN'); + + render( + + + + + + + + + + + ); + await wait(); + userEvent.click(screen.getByTestId(/createOrganizationBtn/i)); + userEvent.click(screen.getByTestId(/createSampleOrganizationBtn/i)); + await waitFor(() => + expect( + screen.queryByText(/Sample Organization Successfully created/i) + ).toBeInTheDocument() + ); + }); + test('Testing error handling for CreateSampleOrg', async () => { + localStorage.setItem('id', '123'); + localStorage.setItem('UserType', 'SUPERADMIN'); + jest.spyOn(toast, 'error'); + render( + + + + + + + + + ); + await wait(); + userEvent.click(screen.getByTestId(/createOrganizationBtn/i)); + userEvent.click(screen.getByTestId(/createSampleOrganizationBtn/i)); + await waitFor(() => + expect( + screen.queryByText(/Only one sample organization allowed/i) + ).toBeInTheDocument() + ); + }); }); describe('Organisations Page testing as Admin', () => { diff --git a/src/screens/OrgList/OrgList.tsx b/src/screens/OrgList/OrgList.tsx index 9104d2c282..0a1286405e 100644 --- a/src/screens/OrgList/OrgList.tsx +++ b/src/screens/OrgList/OrgList.tsx @@ -652,8 +652,8 @@ function orgList(): JSX.Element { @@ -665,7 +665,7 @@ function orgList(): JSX.Element {
{t('goToStore')} @@ -676,7 +676,7 @@ function orgList(): JSX.Element { className={styles.greenregbtn} onClick={closeDialogModal} value="invite" - data-testid="submitOrganizationForm" + data-testid="enableEverythingForm" > {t('enableEverything')} diff --git a/src/screens/OrgList/OrgListMocks.ts b/src/screens/OrgList/OrgListMocks.ts index 6591c00e31..833535f4aa 100644 --- a/src/screens/OrgList/OrgListMocks.ts +++ b/src/screens/OrgList/OrgListMocks.ts @@ -1,3 +1,7 @@ +import { + CREATE_ORGANIZATION_MUTATION, + CREATE_SAMPLE_ORGANIZATION_MUTATION, +} from 'GraphQl/Mutations/mutations'; import { ORGANIZATION_CONNECTION_LIST, USER_ORGANIZATION_LIST, @@ -103,6 +107,39 @@ const MOCKS = [ data: superAdminUser, }, }, + { + request: { + query: CREATE_SAMPLE_ORGANIZATION_MUTATION, + }, + result: { + data: { + createSampleOrganization: { + id: '1', + name: 'Sample Organization', + }, + }, + }, + }, + { + request: { + query: CREATE_ORGANIZATION_MUTATION, + variables: { + description: 'This is a dummy organization', + location: 'Delhi, India', + name: 'Dummy Organization', + visibleInSearch: true, + isPublic: false, + image: '', + }, + }, + result: { + data: { + createOrganization: { + _id: '1', + }, + }, + }, + }, ]; const MOCKS_EMPTY = [ { @@ -132,6 +169,40 @@ const MOCKS_EMPTY = [ }, }, ]; +const MOCKS_WITH_ERROR = [ + { + request: { + query: ORGANIZATION_CONNECTION_LIST, + variables: { + first: 8, + skip: 0, + filter: '', + orderBy: 'createdAt_ASC', + }, + notifyOnNetworkStatusChange: true, + }, + result: { + data: { + organizationsConnection: organizations, + }, + }, + }, + { + request: { + query: USER_ORGANIZATION_LIST, + variables: { id: '123' }, + }, + result: { + data: superAdminUser, + }, + }, + { + request: { + query: CREATE_SAMPLE_ORGANIZATION_MUTATION, + }, + error: new Error('Failed to create sample organization'), + }, +]; // MOCKS FOR ADMIN const MOCKS_ADMIN = [ @@ -163,4 +234,4 @@ const MOCKS_ADMIN = [ }, ]; -export { MOCKS, MOCKS_ADMIN, MOCKS_EMPTY }; +export { MOCKS, MOCKS_ADMIN, MOCKS_EMPTY, MOCKS_WITH_ERROR };