From f7805735018ce819cc589e306d36ef4a8249126d Mon Sep 17 00:00:00 2001 From: NishantSinghhhhh Date: Mon, 6 Jan 2025 17:31:19 +0530 Subject: [PATCH] improving coverage of OrgPost.tsx Signed-off-by: NishantSinghhhhh --- src/screens/OrgPost/OrgPost.test.tsx | 160 +++++++++++++++++++++++++++ src/screens/OrgPost/OrgPost.tsx | 7 +- 2 files changed, 164 insertions(+), 3 deletions(-) diff --git a/src/screens/OrgPost/OrgPost.test.tsx b/src/screens/OrgPost/OrgPost.test.tsx index 9829589350..a4fc74409a 100644 --- a/src/screens/OrgPost/OrgPost.test.tsx +++ b/src/screens/OrgPost/OrgPost.test.tsx @@ -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: { @@ -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); + }); + }); +}); diff --git a/src/screens/OrgPost/OrgPost.tsx b/src/screens/OrgPost/OrgPost.tsx index e9cb4d4ca2..94eece5797 100644 --- a/src/screens/OrgPost/OrgPost.tsx +++ b/src/screens/OrgPost/OrgPost.tsx @@ -247,7 +247,7 @@ function orgPost(): JSX.Element { setFirst(null); setLast(6); }; - // console.log(orgPostListData?.organizations[0].posts); + const sortPosts = ( posts: InterfaceOrgPost[], sortingOption: string, @@ -426,10 +426,11 @@ function orgPost(): JSX.Element {