Skip to content

Commit

Permalink
#3046 Improve Code Coverage in AddPeopleToTag.tsx (#3411)
Browse files Browse the repository at this point in the history
* Improve Code Coverage for AddPeopleToTag.tsx

* Improve Code Coverage for AddPeopleToTag.tsx
  • Loading branch information
pranavnathe authored Jan 25, 2025
1 parent 00fe16b commit d19190a
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[Admin Docs](/)

***

# Variable: MOCK\_EMPTY

> `const` **MOCK\_EMPTY**: `object`[]
Defined in: [src/components/AddPeopleToTag/AddPeopleToTagsMocks.ts:294](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/AddPeopleToTag/AddPeopleToTagsMocks.ts#L294)

## Type declaration

### request

> **request**: `object`
#### request.query

> **query**: `DocumentNode` = `USER_TAGS_MEMBERS_TO_ASSIGN_TO`
#### request.variables

> **variables**: `object`
#### request.variables.first

> **first**: `number` = `TAGS_QUERY_DATA_CHUNK_SIZE`
#### request.variables.id

> **id**: `string` = `'1'`
#### request.variables.where

> **where**: `object`
#### request.variables.where.firstName

> **firstName**: `object`
#### request.variables.where.firstName.starts\_with

> **starts\_with**: `string` = `''`
#### request.variables.where.lastName

> **lastName**: `object`
#### request.variables.where.lastName.starts\_with

> **starts\_with**: `string` = `''`
### result

> **result**: `object`
#### result.data

> **data**: `object`
#### result.data.getUsersToAssignTo

> **getUsersToAssignTo**: `object`
#### result.data.getUsersToAssignTo.usersToAssignTo

> **usersToAssignTo**: `object`
#### result.data.getUsersToAssignTo.usersToAssignTo.edges

> **edges**: `any`[] = `[]`
#### result.data.getUsersToAssignTo.usersToAssignTo.pageInfo

> **pageInfo**: `object`
#### result.data.getUsersToAssignTo.usersToAssignTo.pageInfo.hasNextPage

> **hasNextPage**: `boolean` = `false`
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Admin Docs](/)

***

# Variable: MOCK\_NON\_ERROR

> `const` **MOCK\_NON\_ERROR**: (\{ `error`: `undefined`; `request`: \{ `query`: `DocumentNode`; `variables`: \{ `first`: `number`; `id`: `string`; `tagId`: `undefined`; `userIds`: `undefined`; `where`: \{ `firstName`: \{ `starts_with`: `string`; \}; `lastName`: \{ `starts_with`: `string`; \}; \}; \}; \}; `result`: \{ `data`: \{ `getUsersToAssignTo`: \{ `usersToAssignTo`: \{ `edges`: `object`[]; `pageInfo`: \{ `endCursor`: `string`; `hasNextPage`: `boolean`; \}; \}; \}; \}; \}; \} \| \{ `error`: `Error`; `request`: \{ `query`: `DocumentNode`; `variables`: \{ `first`: `undefined`; `id`: `undefined`; `tagId`: `string`; `userIds`: `string`[]; `where`: `undefined`; \}; \}; `result`: `undefined`; \})[]
Defined in: [src/components/AddPeopleToTag/AddPeopleToTagsMocks.ts:322](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/AddPeopleToTag/AddPeopleToTagsMocks.ts#L322)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Admin Docs](/)

***

# Variable: MOCK\_NO\_DATA

> `const` **MOCK\_NO\_DATA**: (\{ `request`: \{ `query`: `DocumentNode`; `variables`: \{ `first`: `number`; `id`: `string`; `tagId`: `undefined`; `userIds`: `undefined`; `where`: \{ `firstName`: \{ `starts_with`: `string`; \}; `lastName`: \{ `starts_with`: `string`; \}; \}; \}; \}; `result`: \{ `data`: \{ `getUsersToAssignTo`: \{ `usersToAssignTo`: \{ `edges`: `object`[]; `pageInfo`: \{ `endCursor`: `string`; `hasNextPage`: `boolean`; \}; \}; \}; \}; \}; \} \| \{ `request`: \{ `query`: `DocumentNode`; `variables`: \{ `first`: `undefined`; `id`: `undefined`; `tagId`: `string`; `userIds`: `string`[]; `where`: `undefined`; \}; \}; `result`: \{ `data`: `any`; \}; \})[]
Defined in: [src/components/AddPeopleToTag/AddPeopleToTagsMocks.ts:413](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/AddPeopleToTag/AddPeopleToTagsMocks.ts#L413)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Admin Docs](/)

***

# Variable: MOCK\_NULL\_FETCH\_MORE

> `const` **MOCK\_NULL\_FETCH\_MORE**: (\{ `request`: \{ `query`: `DocumentNode`; `variables`: \{ `after`: `undefined`; `first`: `number`; `id`: `string`; `where`: \{ `firstName`: \{ `starts_with`: `string`; \}; `lastName`: \{ `starts_with`: `string`; \}; \}; \}; \}; `result`: \{ `data`: \{ `getUsersToAssignTo`: \{ `usersToAssignTo`: \{ `edges`: `object`[]; `pageInfo`: \{ `endCursor`: `string`; `hasNextPage`: `boolean`; \}; \}; \}; \}; \}; \} \| \{ `request`: \{ `query`: `DocumentNode`; `variables`: \{ `after`: `string`; `first`: `number`; `id`: `string`; `where`: \{ `firstName`: \{ `starts_with`: `string`; \}; `lastName`: \{ `starts_with`: `string`; \}; \}; \}; \}; `result`: \{ `data`: `any`; \}; \})[]
Defined in: [src/components/AddPeopleToTag/AddPeopleToTagsMocks.ts:362](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/AddPeopleToTag/AddPeopleToTagsMocks.ts#L362)
148 changes: 147 additions & 1 deletion src/components/AddPeopleToTag/AddPeopleToTag.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
fireEvent,
cleanup,
waitFor,
act,
} from '@testing-library/react';
import { Provider } from 'react-redux';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
Expand All @@ -22,7 +23,14 @@ import { InMemoryCache, type ApolloLink } from '@apollo/client';
import type { InterfaceAddPeopleToTagProps } from './AddPeopleToTag';
import AddPeopleToTag from './AddPeopleToTag';
import i18n from 'utils/i18nForTest';
import { MOCKS, MOCKS_ERROR } from './AddPeopleToTagsMocks';
import {
MOCK_EMPTY,
MOCK_NULL_FETCH_MORE,
MOCK_NO_DATA,
MOCK_NON_ERROR,
MOCKS,
MOCKS_ERROR,
} from './AddPeopleToTagsMocks';
import type { TFunction } from 'i18next';

const link = new StaticMockLink(MOCKS, true);
Expand Down Expand Up @@ -50,6 +58,14 @@ const translations = {
...JSON.parse(JSON.stringify(i18n.getDataByLanguage('en')?.errors ?? {})),
};

const defaultProps: InterfaceAddPeopleToTagProps = {
addPeopleToTagModalIsOpen: false,
hideAddPeopleToTagModal: vi.fn(),
refetchAssignedMembersData: vi.fn(),
t: ((key: string) => key) as TFunction<'translation', 'manageTag'>,
tCommon: ((key: string) => key) as TFunction<'common', undefined>,
};

const props: InterfaceAddPeopleToTagProps = {
addPeopleToTagModalIsOpen: true,
hideAddPeopleToTagModal: () => {},
Expand Down Expand Up @@ -113,6 +129,28 @@ const renderAddPeopleToTagModal = (
);
};

const renderComponent = (
customProps?: Partial<InterfaceAddPeopleToTagProps>,
): RenderResult =>
render(
<MockedProvider cache={cache} link={new StaticMockLink(MOCKS, true)}>
<MemoryRouter initialEntries={['/orgtags/1/manageTag/1']}>
<Provider store={store}>
<I18nextProvider i18n={i18n}>
<Routes>
<Route
path="/orgtags/:orgId/manageTag/:tagId"
element={
<AddPeopleToTag {...defaultProps} {...(customProps ?? {})} />
}
/>
</Routes>
</I18nextProvider>
</Provider>
</MemoryRouter>
</MockedProvider>,
);

describe('Organisation Tags Page', () => {
beforeEach(() => {
// Mocking `react-router-dom` to return the actual module and override `useParams`
Expand Down Expand Up @@ -320,4 +358,112 @@ describe('Organisation Tags Page', () => {
);
});
});

it('Displays "no more members found" overlay when data is empty', async () => {
const link = new StaticMockLink(MOCK_EMPTY, true);
renderAddPeopleToTagModal(props, link);

await waitFor(() => {
expect(
screen.queryByTestId('infiniteScrollLoader'),
).not.toBeInTheDocument();
});

expect(
screen.getByText(translations.noMoreMembersFound),
).toBeInTheDocument();
});

it('Resets the search state and refetches when the modal transitions from closed to open', async () => {
const { rerender } = renderComponent({ addPeopleToTagModalIsOpen: false });

act(() => {
rerender(
<MockedProvider cache={cache} link={new StaticMockLink(MOCKS, true)}>
<MemoryRouter initialEntries={['/orgtags/1/manageTag/1']}>
<Provider store={store}>
<I18nextProvider i18n={i18n}>
<Routes>
<Route
path="/orgtags/:orgId/manageTag/:tagId"
element={
<AddPeopleToTag
{...defaultProps}
addPeopleToTagModalIsOpen={true}
/>
}
/>
</Routes>
</I18nextProvider>
</Provider>
</MemoryRouter>
</MockedProvider>,
);
});

await waitFor(() => {
expect(screen.getByPlaceholderText('firstName')).toHaveValue('');
expect(screen.getByPlaceholderText('lastName')).toHaveValue('');
});
});

it('displays the unknownError toast if a non-Error is thrown', async () => {
const linkWithNonError = new StaticMockLink(MOCK_NON_ERROR, true);

const customProps = {
...props,
addPeopleToTagModalIsOpen: true,
};

renderAddPeopleToTagModal(customProps, linkWithNonError);

await waitFor(() => {
expect(screen.getAllByTestId('selectMemberBtn')).toHaveLength(1);
});

userEvent.click(screen.getAllByTestId('selectMemberBtn')[0]);
userEvent.click(screen.getByTestId('assignPeopleBtn'));

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

it('returns prevResult if fetchMoreResult is null', async () => {
const linkWithNullFetchMore = new StaticMockLink(
MOCK_NULL_FETCH_MORE,
true,
);

renderAddPeopleToTagModal(props, linkWithNullFetchMore);

await waitFor(() => {
expect(screen.getByText('member 1')).toBeInTheDocument();
});

const scrollableDiv = screen.getByTestId('addPeopleToTagScrollableDiv');
fireEvent.scroll(scrollableDiv, {
target: { scrollY: 99999 },
});

await waitFor(() => {
expect(screen.getAllByTestId('memberName')).toHaveLength(1);
});
});

it('skips the if(data) block when the mutation returns data = null', async () => {
const linkNoData = new StaticMockLink(MOCK_NO_DATA, true);
renderAddPeopleToTagModal(props, linkNoData);

await waitFor(() => {
expect(screen.getAllByTestId('selectMemberBtn')).toHaveLength(1);
});

userEvent.click(screen.getAllByTestId('selectMemberBtn')[0]);
userEvent.click(screen.getByTestId('assignPeopleBtn'));

await waitFor(() => {
expect(toast.success).not.toHaveBeenCalled();
});
});
});
Loading

0 comments on commit d19190a

Please sign in to comment.