Skip to content

Commit

Permalink
Migrated src/screens/SubTags from Jest to Vitest (#2739)
Browse files Browse the repository at this point in the history
* Migrated src/screens/SubTags from Jest to Vitest

* fix: removed import from Jest
  • Loading branch information
PratapRathi authored Dec 23, 2024
1 parent df2dd67 commit cd0b041
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
waitForElementToBeRemoved,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import 'jest-location-mock';
import { I18nextProvider } from 'react-i18next';
import { Provider } from 'react-redux';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
Expand All @@ -22,6 +21,7 @@ import i18n from 'utils/i18nForTest';
import SubTags from './SubTags';
import { MOCKS, MOCKS_ERROR_SUB_TAGS } from './SubTagsMocks';
import { InMemoryCache, type ApolloLink } from '@apollo/client';
import { vi, beforeEach, afterEach, expect, it } from 'vitest';

const translations = {
...JSON.parse(
Expand All @@ -44,10 +44,10 @@ async function wait(ms = 500): Promise<void> {
});
}

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

Expand Down Expand Up @@ -107,19 +107,18 @@ const renderSubTags = (link: ApolloLink): RenderResult => {

describe('Organisation Tags Page', () => {
beforeEach(() => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({ orgId: 'orgId' }),
vi.mock('react-router-dom', async () => ({
...(await vi.importActual('react-router-dom')),
}));
cache.reset();
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
cleanup();
});

test('Component loads correctly', async () => {
it('Component loads correctly', async () => {
const { getByText } = renderSubTags(link);

await wait();
Expand All @@ -129,7 +128,7 @@ describe('Organisation Tags Page', () => {
});
});

test('render error component on unsuccessful subtags query', async () => {
it('render error component on unsuccessful subtags query', async () => {
const { queryByText } = renderSubTags(link2);

await wait();
Expand All @@ -139,7 +138,7 @@ describe('Organisation Tags Page', () => {
});
});

test('opens and closes the create tag modal', async () => {
it('opens and closes the create tag modal', async () => {
renderSubTags(link);

await wait();
Expand All @@ -161,7 +160,7 @@ describe('Organisation Tags Page', () => {
);
});

test('navigates to manage tag screen after clicking manage tag option', async () => {
it('navigates to manage tag screen after clicking manage tag option', async () => {
renderSubTags(link);

await wait();
Expand All @@ -176,7 +175,7 @@ describe('Organisation Tags Page', () => {
});
});

test('navigates to sub tags screen after clicking on a tag', async () => {
it('navigates to sub tags screen after clicking on a tag', async () => {
renderSubTags(link);

await wait();
Expand All @@ -191,7 +190,7 @@ describe('Organisation Tags Page', () => {
});
});

test('navigates to the different sub tag screen screen after clicking a tag in the breadcrumbs', async () => {
it('navigates to the different sub tag screen screen after clicking a tag in the breadcrumbs', async () => {
renderSubTags(link);

await wait();
Expand All @@ -206,7 +205,7 @@ describe('Organisation Tags Page', () => {
});
});

test('navigates to organization tags screen screen after clicking tha all tags option in the breadcrumbs', async () => {
it('navigates to organization tags screen screen after clicking tha all tags option in the breadcrumbs', async () => {
renderSubTags(link);

await wait();
Expand All @@ -221,7 +220,7 @@ describe('Organisation Tags Page', () => {
});
});

test('navigates to manage tags screen for the current tag after clicking tha manageCurrentTag button', async () => {
it('navigates to manage tags screen for the current tag after clicking tha manageCurrentTag button', async () => {
renderSubTags(link);

await wait();
Expand All @@ -236,7 +235,7 @@ describe('Organisation Tags Page', () => {
});
});

test('searchs for tags where the name matches the provided search input', async () => {
it('searchs for tags where the name matches the provided search input', async () => {
renderSubTags(link);

await wait();
Expand All @@ -257,7 +256,7 @@ describe('Organisation Tags Page', () => {
});
});

test('fetches the tags by the sort order, i.e. latest or oldest first', async () => {
it('fetches the tags by the sort order, i.e. latest or oldest first', async () => {
renderSubTags(link);

await wait();
Expand Down Expand Up @@ -314,7 +313,7 @@ describe('Organisation Tags Page', () => {
});
});

test('Fetches more sub tags with infinite scroll', async () => {
it('Fetches more sub tags with infinite scroll', async () => {
const { getByText } = renderSubTags(link);

await wait();
Expand Down Expand Up @@ -343,7 +342,7 @@ describe('Organisation Tags Page', () => {
});
});

test('adds a new sub tag to the current tag', async () => {
it('adds a new sub tag to the current tag', async () => {
renderSubTags(link);

await wait();
Expand Down
32 changes: 17 additions & 15 deletions src/screens/SubTags/SubTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ function SubTags(): JSX.Element {
fetchMoreResult?: { getChildTags: InterfaceQueryUserTagChildTags };
},
) => {
if (!fetchMoreResult) /* istanbul ignore next */ return prevResult;
/* istanbul ignore next -- @preserve */
if (!fetchMoreResult) return prevResult;

return {
getChildTags: {
Expand Down Expand Up @@ -126,15 +127,15 @@ function SubTags(): JSX.Element {
},
});

/* istanbul ignore next */
/* istanbul ignore next -- @preserve */
if (data) {
toast.success(t('tagCreationSuccess') as string);
subTagsRefetch();
setTagName('');
setAddSubTagModalIsOpen(false);
}
} catch (error: unknown) {
/* istanbul ignore next */
/* istanbul ignore next -- @preserve */
if (error instanceof Error) {
toast.error(error.message);
}
Expand All @@ -155,8 +156,8 @@ function SubTags(): JSX.Element {
}

const subTagsList =
subTagsData?.getChildTags.childTags.edges.map((edge) => edge.node) ??
/* istanbul ignore next */ [];
/* istanbul ignore next -- @preserve */
subTagsData?.getChildTags.childTags.edges.map((edge) => edge.node) ?? [];

const parentTagName = subTagsData?.getChildTags.name;

Expand Down Expand Up @@ -394,7 +395,7 @@ function SubTags(): JSX.Element {
next={loadMoreSubTags}
hasMore={
subTagsData?.getChildTags.childTags.pageInfo.hasNextPage ??
/* istanbul ignore next */
/* istanbul ignore next -- @preserve */
false
}
loader={<InfiniteScrollLoader />}
Expand All @@ -406,15 +407,16 @@ function SubTags(): JSX.Element {
hideFooter={true}
getRowId={(row) => row.id}
slots={{
noRowsOverlay: /* istanbul ignore next */ () => (
<Stack
height="100%"
alignItems="center"
justifyContent="center"
>
{t('noTagsFound')}
</Stack>
),
noRowsOverlay:
/* istanbul ignore next -- @preserve */ () => (
<Stack
height="100%"
alignItems="center"
justifyContent="center"
>
{t('noTagsFound')}
</Stack>
),
}}
sx={dataGridStyle}
getRowClassName={() => `${styles.rowBackground}`}
Expand Down

0 comments on commit cd0b041

Please sign in to comment.