From 73df92c4c6d1f2658080b97007442c56af21ba5e Mon Sep 17 00:00:00 2001 From: Meetul Rathore <55585268+meetulr@users.noreply.github.com> Date: Sun, 10 Dec 2023 22:36:50 +0530 Subject: [PATCH] fix: improve code coverage for src/screens/OrgPost/OrgPost.test.tsx (#1165) * fix: improve code coverage for src/screens/OrgPost/OrgPost.test.tsx * fix: improved code coverage for src/components/OrgPostCard/OrgPostCard.tsx --- .../OrgPostCard/OrgPostCard.test.tsx | 166 ++++++++++++++++-- src/components/OrgPostCard/OrgPostCard.tsx | 1 + src/screens/OrgPost/OrgPost.test.tsx | 42 ++++- 3 files changed, 194 insertions(+), 15 deletions(-) diff --git a/src/components/OrgPostCard/OrgPostCard.test.tsx b/src/components/OrgPostCard/OrgPostCard.test.tsx index e2a209c00f..5b22718db1 100644 --- a/src/components/OrgPostCard/OrgPostCard.test.tsx +++ b/src/components/OrgPostCard/OrgPostCard.test.tsx @@ -20,16 +20,17 @@ import i18nForTest from 'utils/i18nForTest'; import { StaticMockLink } from 'utils/StaticMockLink'; import convertToBase64 from 'utils/convertToBase64'; import { BrowserRouter } from 'react-router-dom'; + const MOCKS = [ { request: { query: DELETE_POST_MUTATION, - variable: { id: '123' }, + variables: { id: '12' }, }, result: { data: { removePost: { - _id: '123', + _id: '12', }, }, }, @@ -37,8 +38,8 @@ const MOCKS = [ { request: { query: UPDATE_POST_MUTATION, - variable: { - id: '123', + variables: { + id: '12', title: 'updated title', text: 'This is a updated text', }, @@ -46,7 +47,7 @@ const MOCKS = [ result: { data: { updatePost: { - _id: '32', + _id: '12', }, }, }, @@ -55,13 +56,13 @@ const MOCKS = [ request: { query: TOGGLE_PINNED_POST, variables: { - id: '32', + id: '12', }, }, result: { data: { togglePostPin: { - _id: '32', + _id: '12', }, }, }, @@ -83,6 +84,24 @@ async function wait(ms = 100): Promise { }); } describe('Testing Organization Post Card', () => { + const originalLocation = window.location; + + beforeAll(() => { + Object.defineProperty(window, 'location', { + configurable: true, + value: { + reload: jest.fn(), + }, + }); + }); + + afterAll(() => { + Object.defineProperty(window, 'location', { + configurable: true, + value: originalLocation, + }); + }); + const props = { key: '123', id: '12', @@ -93,6 +112,7 @@ describe('Testing Organization Post Card', () => { postVideo: 'test.mp4', pinned: false, }; + jest.mock('react-toastify', () => ({ toast: { success: jest.fn(), @@ -168,7 +188,7 @@ describe('Testing Organization Post Card', () => { expect(screen.getByAltText('Post Image')).toBeInTheDocument(); }); test('Testing post updating after post is updated', async () => { - render( + const { getByTestId } = render( @@ -182,9 +202,20 @@ describe('Testing Organization Post Card', () => { userEvent.click(screen.getByTestId('moreiconbtn')); userEvent.click(screen.getByTestId('editPostModalBtn')); - userEvent.type(screen.getByTestId('updateTitle'), 'updated title'); - userEvent.type(screen.getByTestId('updateText'), 'This is a updated text'); + fireEvent.change(getByTestId('updateTitle'), { + target: { value: 'updated title' }, + }); + fireEvent.change(getByTestId('updateText'), { + target: { value: 'This is a updated text' }, + }); userEvent.click(screen.getByTestId('updatePostBtn')); + + await waitFor( + () => { + expect(window.location.reload).toHaveBeenCalled(); + }, + { timeout: 2500 } + ); }); test('Testing pin post functionality', async () => { render( @@ -199,7 +230,38 @@ describe('Testing Organization Post Card', () => { userEvent.click(screen.getByAltText('image')); userEvent.click(screen.getByTestId('moreiconbtn')); + userEvent.click(screen.getByTestId('pinpostBtn')); + await waitFor( + () => { + expect(window.location.reload).toHaveBeenCalled(); + }, + { timeout: 3000 } + ); + }); + test('Testing pin post functionality fail case', async () => { + const props2 = { + key: '123', + id: '', + postTitle: 'Event Info', + postInfo: 'Time change', + postAuthor: 'John Doe', + postPhoto: 'test.png', + postVideo: 'test.mp4', + pinned: true, + }; + render( + + + + + + ); + + await wait(); + + userEvent.click(screen.getByAltText('image')); + userEvent.click(screen.getByTestId('moreiconbtn')); userEvent.click(screen.getByTestId('pinpostBtn')); }); test('Testing post delete functionality', async () => { @@ -220,6 +282,42 @@ describe('Testing Organization Post Card', () => { userEvent.click(screen.getByTestId('deletePostModalBtn')); fireEvent.click(screen.getByTestId('deletePostBtn')); + + await waitFor( + () => { + expect(window.location.reload).toHaveBeenCalled(); + }, + { timeout: 3000 } + ); + }); + test('Testing post delete functionality fail case', async () => { + const props2 = { + key: '123', + id: '', + postTitle: 'Event Info', + postInfo: 'Time change', + postAuthor: 'John Doe', + postPhoto: 'test.png', + postVideo: 'test.mp4', + pinned: true, + }; + render( + + + + + + + + ); + + await wait(); + + userEvent.click(screen.getByAltText('image')); + userEvent.click(screen.getByTestId('moreiconbtn')); + + userEvent.click(screen.getByTestId('deletePostModalBtn')); + fireEvent.click(screen.getByTestId('deletePostBtn')); }); test('Testing close functionality of primary modal', async () => { render( @@ -251,7 +349,7 @@ describe('Testing Organization Post Card', () => { userEvent.click(screen.getByTestId('closebtn')); }); test('renders without "Read more" button when postInfo length is less than or equal to 43', () => { - const props = { + const props2 = { key: '123', id: '12', postTitle: 'Event Info', @@ -264,7 +362,7 @@ describe('Testing Organization Post Card', () => { render( - + ); @@ -396,8 +494,50 @@ describe('Testing Organization Post Card', () => { fireEvent.click(pinButton); await waitFor(() => { expect(MOCKS[2].request.variables).toEqual({ - id: '32', + id: '12', }); }); }); + test('testing video play and pause on mouse enter and leave events', async () => { + const { getByTestId } = render( + + + + + + ); + + const card = getByTestId('cardVid'); + + HTMLVideoElement.prototype.play = jest.fn(); + HTMLVideoElement.prototype.pause = jest.fn(); + + fireEvent.mouseEnter(card); + expect(HTMLVideoElement.prototype.play).toHaveBeenCalled(); + + fireEvent.mouseLeave(card); + expect(HTMLVideoElement.prototype.pause).toHaveBeenCalled(); + }); + test('for rendering when no image and no video is available', async () => { + const props2 = { + key: '123', + id: '', + postTitle: 'Event Info', + postInfo: 'Time change', + postAuthor: 'John Doe', + postPhoto: '', + postVideo: '', + pinned: true, + }; + + const { getByAltText } = render( + + + + + + ); + + expect(getByAltText('image not found')).toBeInTheDocument(); + }); }); diff --git a/src/components/OrgPostCard/OrgPostCard.tsx b/src/components/OrgPostCard/OrgPostCard.tsx index cfa221714d..cf7da948d1 100644 --- a/src/components/OrgPostCard/OrgPostCard.tsx +++ b/src/components/OrgPostCard/OrgPostCard.tsx @@ -182,6 +182,7 @@ export default function OrgPostCard( if (data) { toast.success(t('postDeleted')); + toggleShowDeleteModal(); setTimeout(() => { window.location.reload(); }); diff --git a/src/screens/OrgPost/OrgPost.test.tsx b/src/screens/OrgPost/OrgPost.test.tsx index 85dfc03edc..992e53212a 100644 --- a/src/screens/OrgPost/OrgPost.test.tsx +++ b/src/screens/OrgPost/OrgPost.test.tsx @@ -46,7 +46,7 @@ const MOCKS = [ likeCount: 0, commentCount: 0, comments: [], - pinned: false, + pinned: true, likedBy: [], }, { @@ -68,6 +68,44 @@ const MOCKS = [ likedBy: [], comments: [], }, + { + _id: '6411e54835d7ba2344a78e30', + title: 'posttwo', + text: 'Tis is the post two', + imageUrl: null, + videoUrl: null, + createdAt: '2023-08-24T09:26:56.524+00:00', + creator: { + _id: '640d98d9eb6a743d75341067', + firstName: 'Aditya', + lastName: 'Shelke', + email: 'adidacreator1@gmail.com', + }, + likeCount: 0, + commentCount: 0, + pinned: true, + likedBy: [], + comments: [], + }, + { + _id: '6411e54835d7ba2344a78e31', + title: 'posttwo', + text: 'Tis is the post two', + imageUrl: null, + videoUrl: null, + createdAt: '2023-08-24T09:26:56.524+00:00', + creator: { + _id: '640d98d9eb6a743d75341067', + firstName: 'Aditya', + lastName: 'Shelke', + email: 'adidacreator1@gmail.com', + }, + likeCount: 0, + commentCount: 0, + pinned: false, + likedBy: [], + comments: [], + }, ], }, }, @@ -145,7 +183,7 @@ describe('Organisation Post Page', () => { }, likeCount: 0, commentCount: 0, - pinned: false, + pinned: true, likedBy: [], comments: [], });