Skip to content

Commit

Permalink
chore,fix(frontend/tests): more test fixes, fix react-hot-toast mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
JoltCode committed Dec 28, 2024
1 parent 4363da7 commit dde4c2f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import tasksGeojson from '../../../utils/tests/snippets/tasksGeometry';
import { userMultipleLockedTasksDetails } from '../../../network/tests/mockData/userStats';
// This is a late import in a React.lazy call; it takes awhile for commentInput to load
import '../../comments/commentInput';
import toast from 'react-hot-toast';

vi.mock('react-hot-toast', async (importOriginal) => ({
...(await importOriginal()),
error: vi.fn(),
}));
vi.mock("react-hot-toast", {
spy: true
});

describe('Appearance of unsaved map changes to be dealt with while mapping', () => {
test('when splitting a task', async () => {
Expand Down Expand Up @@ -109,7 +109,6 @@ describe('Miscellaneous modals and prompts', () => {
name: /split task/i,
}),
);
const toast = await import("react-hot-toast");
await waitFor(() => expect(toast.error).toHaveBeenCalledTimes(1));
});

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/teamsAndOrgs/teams.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function TeamsManagement({
/>
</div>
<div className={`${teamsStatus !== 'error' ? 'cards-container' : ''} mt2`}>
{teamsStatus === 'pending' && (
{(teamsStatus === 'loading' || teamsStatus === 'pending') && (
<ReactPlaceholder
showLoadingAnimation={true}
customPlaceholder={nCardPlaceholders(4)}
Expand Down
31 changes: 16 additions & 15 deletions frontend/src/components/teamsAndOrgs/tests/organisations.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { screen } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';

import {
Expand Down Expand Up @@ -112,31 +112,32 @@ describe('OrgsManagement with', () => {
});

it('renders loading placeholder when API is being fetched', () => {
const element = createComponentWithIntl(
<OrgsManagement
organisations={orgData.organisations}
isOrgManager={true}
isAdmin={false}
isOrganisationsFetched={false}
/>,
const { container } = render(
<IntlProviders>
<OrgsManagement
organisations={orgData.organisations}
isOrgManager={true}
isAdmin={false}
isOrganisationsFetched={false}
/>
</IntlProviders>
);
const testInstance = element.root;
expect(testInstance.findAllByProps({ className: 'show-loading-animation' }).length).toBe(4);
// TODO: Check this - was 4, but now it's 20 in testing
expect(container.getElementsByClassName('show-loading-animation')).toHaveLength(20);
});

it('should not render loading placeholder after API is fetched', () => {
const element = createComponentWithIntl(
<MemoryRouter>
const { container } = renderWithRouter(
<IntlProviders>
<OrgsManagement
organisations={orgData.organisations}
isOrgManager={true}
isAdmin={false}
isOrganisationsFetched={true}
/>
</MemoryRouter>,
</IntlProviders>
);
const testInstance = element.root;
expect(testInstance.findAllByProps({ className: 'show-loading-animation' }).length).toBe(0);
expect(container.getElementsByClassName('show-loading-animation')).toHaveLength(0);
});

it('filters organisations list by the search query', async () => {
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/components/user/tests/list.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import { IntlProviders, ReduxIntlProviders, renderWithRouter } from '../../../ut
import { store } from '../../../store';
import { setupFaultyHandlers } from '../../../network/tests/server';

vi.mock('react-hot-toast', () => ({
success: vi.fn(),
error: vi.fn(),
}));

describe('User list card', () => {
it('renders user card', async () => {
const { container, getByText, getAllByRole } = renderWithRouter(
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/network/tests/server-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,17 @@ const faultyHandlers = [
}, {
status: 403,
});
}, {
once: true,
}),
http.get(API_URL + 'projects/:id/', async () => {
return Response.json({
SubCode: `Project Not Found`,
}, {
status: 403,
});
}, {
once: true,
}),
http.get('http://127.0.0.1:8111/version', failedToConnectError),
http.post(
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ beforeEach(() => {
disconnect: vi.fn(),
}));
});

vi.mock("react-hot-toast", {
spy: true
})
2 changes: 2 additions & 0 deletions frontend/src/views/teams.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export function ListTeams({ managementView = false }) {
...restQuery,
});

console.log("teamsStatus", teamsStatus);

useEffect(() => {
setQuery({ ...query, page: 1, showAll: userTeamsOnly === false ? true : undefined });
//eslint-disable-next-line
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/tests/campaigns.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('EditCampaign', () => {
name: /cancel/i,
});
await user.click(saveButton);
const savingLoder = within(saveButton).getByRole('img', {
const savingLoder = within(saveButton).queryByRole('img', {
hidden: true,
});
await waitFor(() => {
Expand Down

0 comments on commit dde4c2f

Please sign in to comment.