Skip to content

Commit

Permalink
fix: Fixed likes and comments not working with User Posts (Palisadoe…
Browse files Browse the repository at this point in the history
…sFoundation#1882)

* fix: fixed likes and comments for user posts

* added tests

* Chnages
  • Loading branch information
Doraemon012 authored Apr 12, 2024
1 parent 54921a9 commit 2895a12
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 36 deletions.
12 changes: 12 additions & 0 deletions src/GraphQl/Queries/OrganizationQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ export const ORGANIZATION_POST_LIST = gql`
lastName
}
commentCount
comments {
_id
text
creator {
_id
}
createdAt
likeCount
likedBy {
_id
}
}
pinned
}
cursor
Expand Down
10 changes: 10 additions & 0 deletions src/components/UserPortal/CommentCard/CommentCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const MOCKS = [
},
];

const handleLikeComment = jest.fn();
const handleDislikeComment = jest.fn();
const link = new StaticMockLink(MOCKS, true);

describe('Testing CommentCard Component [User Portal]', () => {
Expand All @@ -81,6 +83,8 @@ describe('Testing CommentCard Component [User Portal]', () => {
},
],
text: 'testComment',
handleLikeComment: handleLikeComment,
handleDislikeComment: handleDislikeComment,
};

const beforeUserId = getItem('userId');
Expand Down Expand Up @@ -120,6 +124,8 @@ describe('Testing CommentCard Component [User Portal]', () => {
},
],
text: 'testComment',
handleLikeComment: handleLikeComment,
handleDislikeComment: handleDislikeComment,
};

const beforeUserId = getItem('userId');
Expand Down Expand Up @@ -159,6 +165,8 @@ describe('Testing CommentCard Component [User Portal]', () => {
},
],
text: 'testComment',
handleLikeComment: handleLikeComment,
handleDislikeComment: handleDislikeComment,
};

const beforeUserId = getItem('userId');
Expand Down Expand Up @@ -203,6 +211,8 @@ describe('Testing CommentCard Component [User Portal]', () => {
},
],
text: 'testComment',
handleLikeComment: handleLikeComment,
handleDislikeComment: handleDislikeComment,
};

const beforeUserId = getItem('userId');
Expand Down
4 changes: 4 additions & 0 deletions src/components/UserPortal/CommentCard/CommentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface InterfaceCommentCardProps {
id: string;
}[];
text: string;
handleLikeComment: (commentId: string) => void;
handleDislikeComment: (commentId: string) => void;
}

function commentCard(props: InterfaceCommentCardProps): JSX.Element {
Expand Down Expand Up @@ -49,6 +51,7 @@ function commentCard(props: InterfaceCommentCardProps): JSX.Element {
if (data) {
setLikes((likes) => likes - 1);
setIsLikedByUser(false);
props.handleDislikeComment(props.id);
}
} catch (error: any) {
/* istanbul ignore next */
Expand All @@ -65,6 +68,7 @@ function commentCard(props: InterfaceCommentCardProps): JSX.Element {
if (data) {
setLikes((likes) => likes + 1);
setIsLikedByUser(true);
props.handleLikeComment(props.id);
}
} catch (error: any) {
/* istanbul ignore next */
Expand Down
173 changes: 172 additions & 1 deletion src/components/UserPortal/PostCard/PostCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
CREATE_COMMENT_POST,
LIKE_POST,
UNLIKE_POST,
LIKE_COMMENT,
UNLIKE_COMMENT,
} from 'GraphQl/Mutations/mutations';
import useLocalStorage from 'utils/useLocalstorage';

Expand Down Expand Up @@ -77,6 +79,36 @@ const MOCKS = [
},
},
},
{
request: {
query: LIKE_COMMENT,
variables: {
commentId: '1',
},
},
result: {
data: {
likeComment: {
_id: '1',
},
},
},
},
{
request: {
query: UNLIKE_COMMENT,
variables: {
commentId: '1',
},
},
result: {
data: {
unlikeComment: {
_id: '1',
},
},
},
},
];

async function wait(ms = 100): Promise<void> {
Expand Down Expand Up @@ -107,7 +139,7 @@ describe('Testing PostCard Component [User Portal]', () => {
commentCount: 1,
comments: [
{
_id: '64eb13beca85de60ebe0ed0e',
id: '64eb13beca85de60ebe0ed0e',
creator: {
_id: '63d6064458fce20ee25c3bf7',
firstName: 'Noble',
Expand Down Expand Up @@ -377,6 +409,145 @@ describe('Testing PostCard Component [User Portal]', () => {
await wait();
});

test(`Comment should be liked when like button is clicked`, async () => {
const cardProps = {
id: '1',
creator: {
firstName: 'test',
lastName: 'user',
email: '[email protected]',
id: '1',
},
image: 'testImage',
video: '',
text: 'This is post test text',
title: 'This is post test title',
likeCount: 1,
commentCount: 1,
comments: [
{
id: '1',
creator: {
_id: '1',
id: '1',
firstName: 'test',
lastName: 'user',
email: '[email protected]',
},
likeCount: 1,
likedBy: [
{
id: '1',
},
],
text: 'testComment',
},
],
likedBy: [
{
firstName: 'test',
lastName: 'user',
id: '1',
},
],
};
const beforeUserId = getItem('userId');
setItem('userId', '2');

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<PostCard {...cardProps} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);

const showCommentsButton = screen.getByTestId('showCommentsBtn');

userEvent.click(showCommentsButton);

userEvent.click(screen.getByTestId('likeCommentBtn'));

await wait();

if (beforeUserId) {
setItem('userId', beforeUserId);
}
});

test(`Comment should be unliked when like button is clicked, if already liked`, async () => {
const cardProps = {
id: '1',
creator: {
firstName: 'test',
lastName: 'user',
email: '[email protected]',
id: '1',
},
image: 'testImage',
video: '',
text: 'This is post test text',
title: 'This is post test title',
likeCount: 1,
commentCount: 1,
comments: [
{
id: '1',
creator: {
_id: '1',
id: '1',
firstName: 'test',
lastName: 'user',
email: '[email protected]',
},
likeCount: 1,
likedBy: [
{
id: '1',
},
],
text: 'testComment',
},
],
likedBy: [
{
firstName: 'test',
lastName: 'user',
id: '1',
},
],
};
const beforeUserId = getItem('userId');
setItem('userId', '1');

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<PostCard {...cardProps} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);

const showCommentsButton = screen.getByTestId('showCommentsBtn');

userEvent.click(showCommentsButton);

userEvent.click(screen.getByTestId('likeCommentBtn'));

await wait();

if (beforeUserId) {
setItem('userId', beforeUserId);
}
});
test('Comment modal pops when show comments button is clicked.', async () => {
const cardProps = {
id: '',
Expand Down
41 changes: 41 additions & 0 deletions src/components/UserPortal/PostCard/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ interface InterfaceCommentCardProps {
id: string;
}[];
text: string;
handleLikeComment: (commentId: string) => void;
handleDislikeComment: (commentId: string) => void;
}

export default function postCard(props: InterfacePostCard): JSX.Element {
Expand Down Expand Up @@ -104,6 +106,41 @@ export default function postCard(props: InterfacePostCard): JSX.Element {
setCommentInput(comment);
};

const handleDislikeComment = (commentId: string): void => {
const updatedComments = comments.map((comment) => {
let updatedComment = { ...comment };
if (
comment.id === commentId &&
comment.likedBy.some((user) => user.id === userId)
) {
updatedComment = {
...comment,
likedBy: comment.likedBy.filter((user) => user.id !== userId),
likeCount: comment.likeCount - 1,
};
}
return updatedComment;
});
setComments(updatedComments);
};
const handleLikeComment = (commentId: string): void => {
const updatedComments = comments.map((comment) => {
let updatedComment = { ...comment };
if (
comment.id === commentId &&
!comment.likedBy.some((user) => user.id === userId)
) {
updatedComment = {
...comment,
likedBy: [...comment.likedBy, { id: userId }],
likeCount: comment.likeCount + 1,
};
}
return updatedComment;
});
setComments(updatedComments);
};

const createComment = async (): Promise<void> => {
try {
const { data: createEventData } = await create({
Expand All @@ -129,6 +166,8 @@ export default function postCard(props: InterfacePostCard): JSX.Element {
likeCount: createEventData.createComment.likeCount,
likedBy: createEventData.createComment.likedBy,
text: createEventData.createComment.text,
handleLikeComment: handleLikeComment,
handleDislikeComment: handleDislikeComment,
};

setComments([...comments, newComment]);
Expand Down Expand Up @@ -225,6 +264,8 @@ export default function postCard(props: InterfacePostCard): JSX.Element {
likeCount: comment.likeCount,
likedBy: comment.likedBy,
text: comment.text,
handleLikeComment: handleLikeComment,
handleDislikeComment: handleDislikeComment,
};

return <CommentCard key={index} {...cardProps} />;
Expand Down
9 changes: 9 additions & 0 deletions src/screens/OrgPost/OrgPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ interface InterfaceOrgPost {
createdAt: string;
likeCount: number;
commentCount: number;
likedBy: { _id: string }[];
comments: {
_id: string;
text: string;
creator: { _id: string };
createdAt: string;
likeCount: number;
likedBy: { _id: string }[];
}[];
}

function orgPost(): JSX.Element {
Expand Down
1 change: 1 addition & 0 deletions src/screens/UserPortal/Home/Home.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const MOCKS = [
},
],
text: 'This is the post two',
createdAt: '2024-03-03T09:26:56.524+00:00',
},
],
},
Expand Down
Loading

0 comments on commit 2895a12

Please sign in to comment.