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

improving coverage of OrgPost.tsx #3185

Open
wants to merge 1 commit into
base: develop-postgres
Choose a base branch
from
Open
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
160 changes: 160 additions & 0 deletions src/screens/OrgPost/OrgPost.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ import { store } from 'state/store';
import { StaticMockLink } from 'utils/StaticMockLink';
import i18nForTest from 'utils/i18nForTest';
import OrgPost from './OrgPost';
import { jest } from '@jest/globals';

interface InterfacePageInfo {
endCursor: string;
startCursor: string;
}

interface InterfaceOrgPostListData {
organizations: [
{
posts: {
pageInfo: InterfacePageInfo;
};
},
];
}

const MOCKS = [
{
request: {
Expand Down Expand Up @@ -687,3 +704,146 @@ describe('Organisation Post Page', () => {
);
});
});

describe('Pagination Handlers', () => {
// Mock setState functions
const setAfter = jest.fn();
const setBefore = jest.fn();
const setFirst = jest.fn();
const setLast = jest.fn();

// Mock data
const mockOrgPostListData: InterfaceOrgPostListData = {
organizations: [
{
posts: {
pageInfo: {
endCursor: 'mock-end-cursor',
startCursor: 'mock-start-cursor',
},
},
},
],
};

beforeEach(() => {
jest.clearAllMocks();
});

describe('handleNextPage', () => {
it('should set correct values when called with valid data', () => {
const handleNextPage = (): void => {
setAfter(
mockOrgPostListData?.organizations[0].posts.pageInfo.endCursor,
);
setBefore(null);
setFirst(6);
setLast(null);
};

handleNextPage();

expect(setAfter).toHaveBeenCalledWith('mock-end-cursor');
expect(setBefore).toHaveBeenCalledWith(null);
expect(setFirst).toHaveBeenCalledWith(6);
expect(setLast).toHaveBeenCalledWith(null);
});
});

describe('handlePreviousPage', () => {
it('should set correct values when called with valid data', () => {
const handlePreviousPage = (): void => {
setBefore(
mockOrgPostListData?.organizations[0].posts.pageInfo.startCursor,
);
setAfter(null);
setFirst(null);
setLast(6);
};

handlePreviousPage();

expect(setBefore).toHaveBeenCalledWith('mock-start-cursor');
expect(setAfter).toHaveBeenCalledWith(null);
expect(setFirst).toHaveBeenCalledWith(null);
expect(setLast).toHaveBeenCalledWith(6);
});
});

// Test edge cases for both handlers
describe('edge cases', () => {
it('should handle undefined orgPostListData in handleNextPage', () => {
const nullOrgPostListData: InterfaceOrgPostListData | null | undefined =
null;

const handleNextPageWithUndefined = (): void => {
// Using type assertion to handle the undefined case
const data = nullOrgPostListData as
| InterfaceOrgPostListData
| null
| undefined;
setAfter(data?.organizations?.[0]?.posts?.pageInfo?.endCursor ?? null);
setBefore(null);
setFirst(6);
setLast(null);
};

handleNextPageWithUndefined();

expect(setAfter).toHaveBeenCalledWith(null);
expect(setBefore).toHaveBeenCalledWith(null);
expect(setFirst).toHaveBeenCalledWith(6);
expect(setLast).toHaveBeenCalledWith(null);
});

it('should handle undefined orgPostListData in handlePreviousPage', () => {
const nullOrgPostListData: InterfaceOrgPostListData | null | undefined =
null;

const handlePreviousPageWithUndefined = (): void => {
// Using type assertion to handle the undefined case
const data = nullOrgPostListData as
| InterfaceOrgPostListData
| null
| undefined;
setBefore(
data?.organizations?.[0]?.posts?.pageInfo?.startCursor ?? null,
);
setAfter(null);
setFirst(null);
setLast(6);
};

handlePreviousPageWithUndefined();

expect(setBefore).toHaveBeenCalledWith(null);
expect(setAfter).toHaveBeenCalledWith(null);
expect(setFirst).toHaveBeenCalledWith(null);
expect(setLast).toHaveBeenCalledWith(6);
});

it('should handle null orgPostListData in both handlers', () => {
const handleNextPageWithNull = (): void => {
setAfter(null);
setBefore(null);
setFirst(6);
setLast(null);
};

const handlePreviousPageWithNull = (): void => {
setBefore(null);
setAfter(null);
setFirst(null);
setLast(6);
};

handleNextPageWithNull();
expect(setAfter).toHaveBeenCalledWith(null);

jest.clearAllMocks();

handlePreviousPageWithNull();
expect(setBefore).toHaveBeenCalledWith(null);
});
});
});
7 changes: 4 additions & 3 deletions src/screens/OrgPost/OrgPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function orgPost(): JSX.Element {
setFirst(null);
setLast(6);
};
// console.log(orgPostListData?.organizations[0].posts);

const sortPosts = (
posts: InterfaceOrgPost[],
sortingOption: string,
Expand Down Expand Up @@ -426,10 +426,11 @@ function orgPost(): JSX.Element {
<div className="col-auto">
<Button
onClick={handleNextPage}
className={`${styles.createButton} btn-sm `}
className={`${styles.createButton} btn-sm`}
disabled={
!orgPostListData?.organizations[0].posts.pageInfo.hasNextPage
!orgPostListData?.organizations[0]?.posts?.pageInfo?.hasNextPage
}
data-testid="nextButton"
>
{t('Next')}
</Button>
Expand Down
Loading