Skip to content

Commit

Permalink
Test for src/screens/OrgList/OrgList.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
kanhaiya04 committed Dec 29, 2023
1 parent 208338d commit 0daf4fa
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 38 deletions.
187 changes: 154 additions & 33 deletions src/screens/OrgList/OrgList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
screen,
fireEvent,
cleanup,
waitFor,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import 'jest-localstorage-mock';
Expand All @@ -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<void> {
await act(() => {
Expand All @@ -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',
Expand Down Expand Up @@ -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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<OrgList />
</Provider>
</BrowserRouter>
</MockedProvider>
);
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<OrgList />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

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
Expand All @@ -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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<OrgList />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
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(
<MockedProvider addTypename={false} link={link3}>
<BrowserRouter>
<Provider store={store}>
<ToastContainer />
<OrgList />
</Provider>
</BrowserRouter>
</MockedProvider>
);
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', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/screens/OrgList/OrgList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,8 @@ function orgList(): JSX.Element {
</a>
<Button
variant="secondary"
onClick={(): void => toggleModal()}
data-testid="closeOrganizationModal"
onClick={toggleModal}
data-testid="cancelOrganizationModal"
>
{t('cancel')}
</Button>
Expand All @@ -665,7 +665,7 @@ function orgList(): JSX.Element {
<div className={styles.pluginStoreBtnContainer}>
<Link
className={styles.secondbtn}
data-testid="submitOrganizationForm"
data-testid="goToStore"
to={`orgstore/id=${dialogRedirectOrgId}`}
>
{t('goToStore')}
Expand All @@ -676,7 +676,7 @@ function orgList(): JSX.Element {
className={styles.greenregbtn}
onClick={closeDialogModal}
value="invite"
data-testid="submitOrganizationForm"
data-testid="enableEverythingForm"
>
{t('enableEverything')}
</button>
Expand Down
73 changes: 72 additions & 1 deletion src/screens/OrgList/OrgListMocks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
CREATE_ORGANIZATION_MUTATION,
CREATE_SAMPLE_ORGANIZATION_MUTATION,
} from 'GraphQl/Mutations/mutations';
import {
ORGANIZATION_CONNECTION_LIST,
USER_ORGANIZATION_LIST,
Expand Down Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -163,4 +234,4 @@ const MOCKS_ADMIN = [
},
];

export { MOCKS, MOCKS_ADMIN, MOCKS_EMPTY };
export { MOCKS, MOCKS_ADMIN, MOCKS_EMPTY, MOCKS_WITH_ERROR };

0 comments on commit 0daf4fa

Please sign in to comment.