Skip to content

Commit

Permalink
Reduced the no of ignore comments by adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arpit-chakraborty committed Jan 14, 2025
1 parent 6d60b5b commit 475aa4f
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 21 deletions.
60 changes: 59 additions & 1 deletion src/screens/OrganizationTags/OrganizationTags.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
MOCKS_ERROR,
MOCKS_ERROR_ERROR_TAG,
MOCKS_EMPTY,
MOCKS_UNDEFINED_USER_TAGS,
MOCKS_NULL_END_CURSOR,
MOCKS_NO_MORE_PAGES,
} from './OrganizationTagsMocks';
import type { ApolloLink } from '@apollo/client';

Expand All @@ -42,6 +45,9 @@ const link = new StaticMockLink(MOCKS, true);
const link2 = new StaticMockLink(MOCKS_ERROR, true);
const link3 = new StaticMockLink([...MOCKS, ...MOCKS_ERROR_ERROR_TAG], true);
const link4 = new StaticMockLink(MOCKS_EMPTY, true);
const link5 = new StaticMockLink(MOCKS_UNDEFINED_USER_TAGS, true);
const link6 = new StaticMockLink(MOCKS_NULL_END_CURSOR, true);
const link7 = new StaticMockLink(MOCKS_NO_MORE_PAGES, true);

async function wait(ms = 500): Promise<void> {
await act(() => {
Expand Down Expand Up @@ -325,11 +331,63 @@ describe('Organisation Tags Page', () => {
});
});

test('No tags are available', async () => {
test('renders the no tags found message when there are no tags', async () => {
renderOrganizationTags(link4);

await wait();

await waitFor(() => {
expect(screen.getByText(translations.noTagsFound)).toBeInTheDocument();
});
});

test('sets dataLength to 0 when userTagsList is undefined', async () => {
renderOrganizationTags(link5);

await wait();

// Wait for the component to render
await waitFor(() => {
const userTags = screen.queryAllByTestId('user-tag');
expect(userTags).toHaveLength(0);
});
});

test('Null endCursor', async () => {
renderOrganizationTags(link6);

await wait();

const orgUserTagsScrollableDiv = screen.getByTestId(
'orgUserTagsScrollableDiv',
);

// Set scroll position to the bottom
fireEvent.scroll(orgUserTagsScrollableDiv, {
target: { scrollY: orgUserTagsScrollableDiv.scrollHeight },
});

await waitFor(() => {
expect(screen.getByTestId('createTagBtn')).toBeInTheDocument();
});
});

test('Null Page available', async () => {
renderOrganizationTags(link7);

await wait();

const orgUserTagsScrollableDiv = screen.getByTestId(
'orgUserTagsScrollableDiv',
);

// Set scroll position to the bottom
fireEvent.scroll(orgUserTagsScrollableDiv, {
target: { scrollY: orgUserTagsScrollableDiv.scrollHeight },
});

await waitFor(() => {
expect(screen.getByTestId('createTagBtn')).toBeInTheDocument();
});
});
});
35 changes: 15 additions & 20 deletions src/screens/OrganizationTags/OrganizationTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ function OrganizationTags(): JSX.Element {
first: TAGS_QUERY_DATA_CHUNK_SIZE,
after:
orgUserTagsData?.organizations?.[0]?.userTags?.pageInfo?.endCursor ??
/* istanbul ignore next -- @preserve */
null,
},
updateQuery: (
Expand All @@ -97,8 +96,9 @@ function OrganizationTags(): JSX.Element {
};
},
) => {
/* istanbul ignore next -- @preserve */
if (!fetchMoreResult) return prevResult;
if (!fetchMoreResult) {
return prevResult;
}

return {
organizations: [
Expand Down Expand Up @@ -128,7 +128,7 @@ function OrganizationTags(): JSX.Element {

const createTag = async (e: ChangeEvent<HTMLFormElement>): Promise<void> => {
e.preventDefault();
/* istanbul ignore else -- @preserve */

if (!tagName.trim()) {
toast.error(t('enterTagName'));
return;
Expand All @@ -149,14 +149,12 @@ function OrganizationTags(): JSX.Element {
setCreateTagModalIsOpen(false);
}
} catch (error: unknown) {
toast.error(
error instanceof Error
? error.message
: /* istanbul ignore next -- @preserve */ 'Something went wrong',
);
/* istanbul ignore else -- @preserve */
if (error instanceof Error) {
toast.error(error.message);
}
}
};
/* istanbul ignore next -- @preserve */
if (orgUserTagsError) {
return (
<div className={`${styles.errorContainer} bg-white rounded-4 my-3`}>
Expand All @@ -172,10 +170,10 @@ function OrganizationTags(): JSX.Element {
);
}

const userTagsList = orgUserTagsData?.organizations[0].userTags.edges.map(
(edge) => edge.node,
);

const userTagsList =
orgUserTagsData?.organizations?.[0]?.userTags?.edges?.map(
(edge) => edge.node,
) || [];
const redirectToManageTag = (tagId: string): void => {
navigate(`/orgtags/${orgId}/manageTag/${tagId}`);
};
Expand Down Expand Up @@ -378,18 +376,15 @@ function OrganizationTags(): JSX.Element {
className={styles.orgUserTagsScrollableDiv}
>
<InfiniteScroll
dataLength={
userTagsList?.length ??
/* istanbul ignore next -- @preserve */ 0
}
dataLength={userTagsList?.length}
next={loadMoreUserTags}
hasMore={
orgUserTagsData?.organizations?.[0]?.userTags?.pageInfo
?.hasNextPage ??
/* istanbul ignore next -- @preserve */ false
?.hasNextPage ?? false
}
loader={<InfiniteScrollLoader />}
scrollableTarget="orgUserTagsScrollableDiv"
data-testid="infinite-scroll"
>
<DataGrid
disableColumnMenu
Expand Down
212 changes: 212 additions & 0 deletions src/screens/OrganizationTags/OrganizationTagsMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,215 @@ export const MOCKS_EMPTY = [
},
},
];

export const MOCKS_EMPTY_USER_TAG = [
{
request: {
query: ORGANIZATION_USER_TAGS_LIST,
variables: {
id: 'orgId',
first: TAGS_QUERY_DATA_CHUNK_SIZE,
where: { name: { starts_with: '' } },
sortedBy: { id: 'DESCENDING' },
},
},
result: {
data: {
organizations: [
{
userTags: {
edges: [],
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
startCursor: null,
endCursor: null,
},
totalCount: 0,
},
},
],
},
},
},
];

export const MOCKS_UNDEFINED_USER_TAGS = [
{
request: {
query: ORGANIZATION_USER_TAGS_LIST,
variables: {
id: 'orgId',
first: TAGS_QUERY_DATA_CHUNK_SIZE,
where: { name: { starts_with: '' } },
sortedBy: { id: 'DESCENDING' },
},
},
result: {
data: {
organizations: [
{
userTags: undefined, // Simulate undefined userTags
},
],
},
},
},
];

export const MOCKS_NULL_END_CURSOR = [
{
request: {
query: ORGANIZATION_USER_TAGS_LIST,
variables: {
id: 'orgId',
first: TAGS_QUERY_DATA_CHUNK_SIZE,
where: { name: { starts_with: '' } },
sortedBy: { id: 'DESCENDING' },
},
},
result: {
data: {
organizations: [
{
userTags: {
edges: [
{
node: {
_id: '1',
name: 'userTag 1',
parentTag: null,
usersAssignedTo: {
totalCount: 5,
},
childTags: {
totalCount: 11,
},
ancestorTags: [],
},
cursor: '1',
},
],
pageInfo: {
startCursor: '1',
endCursor: null, // Simulate null endCursor
hasNextPage: true,
hasPreviousPage: false,
},
totalCount: 2,
},
},
],
},
},
},
{
request: {
query: ORGANIZATION_USER_TAGS_LIST,
variables: {
id: 'orgId',
first: TAGS_QUERY_DATA_CHUNK_SIZE,
where: { name: { starts_with: '' } },
after: null,
sortedBy: { id: 'DESCENDING' },
},
},
result: {
data: {
organizations: [
{
userTags: {
edges: [
{
node: {
_id: '1',
name: 'userTag 1',
parentTag: null,
usersAssignedTo: {
totalCount: 5,
},
childTags: {
totalCount: 11,
},
ancestorTags: [],
},
cursor: '2',
},
],
pageInfo: {
startCursor: '2',
endCursor: null, // Simulate null endCursor
hasNextPage: true,
hasPreviousPage: false,
},
totalCount: 2,
},
},
],
},
},
},
];

export const MOCKS_NO_MORE_PAGES = [
{
request: {
query: ORGANIZATION_USER_TAGS_LIST,
variables: {
id: 'orgId',
first: TAGS_QUERY_DATA_CHUNK_SIZE,
where: { name: { starts_with: '' } },
sortedBy: { id: 'DESCENDING' },
},
},
result: {
data: {
organizations: [
{
userTags: {
edges: [
{
node: {
_id: '1',
name: 'userTag 1',
parentTag: null,
usersAssignedTo: {
totalCount: 5,
},
childTags: {
totalCount: 11,
},
ancestorTags: [],
},
cursor: '1', // Cursor for pagination
},
],
pageInfo: {
startCursor: '1',
endCursor: '1', // End cursor for the first page
hasNextPage: true, // No more pages available
hasPreviousPage: false,
},
totalCount: 2, // Total number of items
},
},
],
},
},
},
{
request: {
query: ORGANIZATION_USER_TAGS_LIST,
variables: {
id: 'orgId',
first: TAGS_QUERY_DATA_CHUNK_SIZE,
where: { name: { starts_with: '' } },
after: '1', // Cursor for pagination
sortedBy: { id: 'DESCENDING' },
},
},
result: {
data: undefined, // No more pages available
},
},
];

0 comments on commit 475aa4f

Please sign in to comment.