Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Prevent unnecessary page reload after deleting a user #2724

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 121 additions & 59 deletions src/components/OrgPeopleListCard/OrgPeopleListCard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,81 +1,143 @@
import React, { act } from 'react';
import { render, screen } from '@testing-library/react';
import { MockedProvider } from '@apollo/react-testing';
import userEvent from '@testing-library/user-event';
import { I18nextProvider } from 'react-i18next';

import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { MockedProvider } from '@apollo/client/testing';
import OrgPeopleListCard from './OrgPeopleListCard';
import { REMOVE_MEMBER_MUTATION } from 'GraphQl/Mutations/mutations';
import i18nForTest from 'utils/i18nForTest';
import { BrowserRouter } from 'react-router-dom';
import { StaticMockLink } from 'utils/StaticMockLink';

const MOCKS = [
{
request: {
query: REMOVE_MEMBER_MUTATION,
variable: { userid: '123', orgid: '456' },
},
result: {
data: {
organizations: [
{
_id: '1',
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { toast } from 'react-toastify';
import { errorHandler } from 'utils/errorHandler';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
orgId: '12345',
}),
}));

jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string): string => key,
}),
}));

jest.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
},
}));

jest.mock('utils/errorHandler', () => ({
errorHandler: jest.fn(),
}));

describe('orgPeopleListCard Component', () => {
const toggleRemoveModalMock = jest.fn();

const mocks = [
{
request: {
query: REMOVE_MEMBER_MUTATION,
variables: {
userid: '1',
orgid: '12345',
},
},
result: {
data: {
removeMember: {
_id: 12,
},
],
},
},
},
},
];
const link = new StaticMockLink(MOCKS, true);
async function wait(ms = 100): Promise<void> {
await act(() => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
});
}
];

describe('Testing Organization People List Card', () => {
const props = {
toggleRemoveModal: () => true,
id: '1',
toggleRemoveModal: toggleRemoveModalMock,
};
global.alert = jest.fn();

test('should render props and text elements test for the page component', async () => {
global.confirm = (): boolean => true;

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrgPeopleListCard {...props} />
</I18nextProvider>
</BrowserRouter>

it('should render the modal and successfully remove the member', async () => {
const { getByText, getByTestId } = render(
<MockedProvider mocks={mocks} addTypename={false}>
<MemoryRouter initialEntries={['/org/12345']}>
<Routes>
<Route
path="/org/:orgId"
element={<OrgPeopleListCard {...props} />}
/>
</Routes>
</MemoryRouter>
</MockedProvider>,
);

await wait();
expect(getByText('removeMember')).toBeInTheDocument();
expect(getByText('removeMemberMsg')).toBeInTheDocument();

fireEvent.click(getByTestId('removeMemberBtn'));

userEvent.click(screen.getByTestId(/removeMemberBtn/i));
await waitFor(() => {
expect(toast.success).toHaveBeenCalledWith('memberRemoved');
expect(toggleRemoveModalMock).toHaveBeenCalled();
});
});

test('Should not render modal when id is undefined', async () => {
global.confirm = (): boolean => false;
it('should handle errors correctly when mutation fails', async () => {
const errorMocks = [
{
request: {
query: REMOVE_MEMBER_MUTATION,
variables: {
userid: '1',
orgid: '12345',
},
},
error: new Error('An error occurred'),
},
];

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrgPeopleListCard id={undefined} toggleRemoveModal={() => true} />
</I18nextProvider>
</BrowserRouter>
const { getByTestId } = render(
<MockedProvider mocks={errorMocks} addTypename={false}>
<MemoryRouter initialEntries={['/org/12345']}>
<Routes>
<Route
path="/org/:orgId"
element={<OrgPeopleListCard {...props} />}
/>
</Routes>
</MemoryRouter>
</MockedProvider>,
);

await wait();
fireEvent.click(getByTestId('removeMemberBtn'));

await waitFor(() => {
expect(errorHandler).toHaveBeenCalled();
});
});

it('should not render modal when id is undefined', () => {
const propsWithUndefinedId = {
id: undefined,
toggleRemoveModal: toggleRemoveModalMock,
};

const { queryByText } = render(
<MockedProvider mocks={mocks} addTypename={false}>
<MemoryRouter initialEntries={['/orglist']}>
{' '}
{/* Change this to '/orglist' */}
<Routes>
<Route
path="/orglist"
element={<OrgPeopleListCard {...propsWithUndefinedId} />}
/>
</Routes>
</MemoryRouter>
</MockedProvider>,
);

expect(window.location.pathname).toEqual('/orglist');
expect(queryByText('removeMember')).not.toBeInTheDocument();
expect(queryByText('removeMemberMsg')).not.toBeInTheDocument();
});
});
4 changes: 1 addition & 3 deletions src/components/OrgPeopleListCard/OrgPeopleListCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ function orgPeopleListCard(
/* istanbul ignore next */
if (data) {
toast.success(t('memberRemoved') as string);
setTimeout(() => {
window.location.reload();
}, 2000);
props.toggleRemoveModal();
}
} catch (error: unknown) {
/* istanbul ignore next */
Expand Down
Loading