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

fix: improve code coverage for src/screens/OrgPost/OrgPost.test.tsx #1165

Merged
merged 2 commits into from
Dec 10, 2023
Merged
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
166 changes: 153 additions & 13 deletions src/components/OrgPostCard/OrgPostCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,34 @@ 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',
},
},
},
},
{
request: {
query: UPDATE_POST_MUTATION,
variable: {
id: '123',
variables: {
id: '12',
title: 'updated title',
text: 'This is a updated text',
},
},
result: {
data: {
updatePost: {
_id: '32',
_id: '12',
},
},
},
Expand All @@ -55,13 +56,13 @@ const MOCKS = [
request: {
query: TOGGLE_PINNED_POST,
variables: {
id: '32',
id: '12',
},
},
result: {
data: {
togglePostPin: {
_id: '32',
_id: '12',
},
},
},
Expand All @@ -83,6 +84,24 @@ async function wait(ms = 100): Promise<void> {
});
}
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',
Expand All @@ -93,6 +112,7 @@ describe('Testing Organization Post Card', () => {
postVideo: 'test.mp4',
pinned: false,
};

jest.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
Expand Down Expand Up @@ -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(
<MockedProvider addTypename={false} link={link}>
<I18nextProvider i18n={i18nForTest}>
<OrgPostCard {...props} />
Expand All @@ -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(
Expand All @@ -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(
<MockedProvider addTypename={false} link={link}>
<I18nextProvider i18n={i18nForTest}>
<OrgPostCard {...props2} />
</I18nextProvider>
</MockedProvider>
);

await wait();

userEvent.click(screen.getByAltText('image'));
userEvent.click(screen.getByTestId('moreiconbtn'));
userEvent.click(screen.getByTestId('pinpostBtn'));
});
test('Testing post delete functionality', async () => {
Expand All @@ -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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrgPostCard {...props2} />
</I18nextProvider>
</BrowserRouter>
</MockedProvider>
);

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(
Expand Down Expand Up @@ -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',
Expand All @@ -264,7 +362,7 @@ describe('Testing Organization Post Card', () => {
render(
<MockedProvider addTypename={false} link={link}>
<I18nextProvider i18n={i18nForTest}>
<OrgPostCard {...props} />
<OrgPostCard {...props2} />
</I18nextProvider>
</MockedProvider>
);
Expand Down Expand Up @@ -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(
<MockedProvider addTypename={false} link={link}>
<I18nextProvider i18n={i18nForTest}>
<OrgPostCard {...props} />
</I18nextProvider>
</MockedProvider>
);

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(
<MockedProvider addTypename={false} link={link}>
<I18nextProvider i18n={i18nForTest}>
<OrgPostCard {...props2} />
</I18nextProvider>
</MockedProvider>
);

expect(getByAltText('image not found')).toBeInTheDocument();
});
});
1 change: 1 addition & 0 deletions src/components/OrgPostCard/OrgPostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export default function OrgPostCard(

if (data) {
toast.success(t('postDeleted'));
toggleShowDeleteModal();
setTimeout(() => {
window.location.reload();
});
Expand Down
42 changes: 40 additions & 2 deletions src/screens/OrgPost/OrgPost.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const MOCKS = [
likeCount: 0,
commentCount: 0,
comments: [],
pinned: false,
pinned: true,
likedBy: [],
},
{
Expand All @@ -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: '[email protected]',
},
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: '[email protected]',
},
likeCount: 0,
commentCount: 0,
pinned: false,
likedBy: [],
comments: [],
},
],
},
},
Expand Down Expand Up @@ -145,7 +183,7 @@ describe('Organisation Post Page', () => {
},
likeCount: 0,
commentCount: 0,
pinned: false,
pinned: true,
likedBy: [],
comments: [],
});
Expand Down