From c58c62e718eb546c0b1195462b4a47b7dcff1765 Mon Sep 17 00:00:00 2001 From: gauravsingh94 Date: Mon, 18 Mar 2024 02:05:35 +0530 Subject: [PATCH] Fixes #1514 added the test for the PostCard and Home.tsx --- .../UserPortal/PostCard/PostCard.test.tsx | 31 +++++++ .../UserPortal/PostCard/PostCard.tsx | 3 +- src/screens/UserPortal/Home/Home.test.tsx | 18 ++-- src/screens/UserPortal/Home/Home.tsx | 83 +------------------ 4 files changed, 49 insertions(+), 86 deletions(-) diff --git a/src/components/UserPortal/PostCard/PostCard.test.tsx b/src/components/UserPortal/PostCard/PostCard.test.tsx index 84e7e5d925..fdbf36f006 100644 --- a/src/components/UserPortal/PostCard/PostCard.test.tsx +++ b/src/components/UserPortal/PostCard/PostCard.test.tsx @@ -24,6 +24,20 @@ describe('PostCard compoenent', () => { title: 'This is post test title', createdAt: 1647398400000, }; + const postWithImage = { + id: '1', + creator: { + firstName: 'John', + lastName: 'Doe', + image: '', + id: '1', + }, + image: 'https://example.com/image.jpg', + video: '', + text: 'Lorem ipsum dolor sit amet', + title: 'Example Post', + createdAt: 1626166078000, + }; test('Render post card with the correct content.', () => { render( @@ -65,4 +79,21 @@ describe('PostCard compoenent', () => { expect(screen.getByText('Pin Post')).toBeInTheDocument(); expect(screen.getByText('Share')).toBeInTheDocument(); }); + + test('Renders image if the image prop is provided', async () => { + render( + + + + + + + + + , + ); + const Image = screen.getByRole('img'); + expect(Image).toBeInTheDocument(); + expect(Image).toHaveAttribute('src', postWithImage.image); + }); }); diff --git a/src/components/UserPortal/PostCard/PostCard.tsx b/src/components/UserPortal/PostCard/PostCard.tsx index 626979cb65..253d6259f9 100644 --- a/src/components/UserPortal/PostCard/PostCard.tsx +++ b/src/components/UserPortal/PostCard/PostCard.tsx @@ -48,7 +48,8 @@ export default function postCard(props: InterfacePostCardProps): JSX.Element { + alt="postImage" + /> )} {!props.creator.image && (
diff --git a/src/screens/UserPortal/Home/Home.test.tsx b/src/screens/UserPortal/Home/Home.test.tsx index 8fc02daa9b..1354ce5bd8 100644 --- a/src/screens/UserPortal/Home/Home.test.tsx +++ b/src/screens/UserPortal/Home/Home.test.tsx @@ -52,7 +52,7 @@ const MOCKS = [ commentCount: 0, comments: [], likedBy: [], - pinned: false, + pinned: true, }, { _id: '6411e54835d7ba2344a78e29', @@ -266,7 +266,7 @@ describe('Testing Home Screen [User Portal]', () => { expect(postInput).toHaveValue('Testing post content'); }); - test('Scroll right button should be in document for pinned posts.', async () => { + test('Should scroll the carousel right on the" click', async () => { render( @@ -278,10 +278,18 @@ describe('Testing Home Screen [User Portal]', () => { , ); - await wait(); - - const scrollRightButton = screen.getByTestId('scrollRightButton'); + const carousel = screen.getByTestId('carousel'); + expect(carousel).toBeInTheDocument(); + const scrollRightButton = screen.getByRole('button', { + name: 'Scroll Right', + }); expect(scrollRightButton).toBeInTheDocument(); + expect(carousel.scrollLeft).toBe(0); + act(() => { + userEvent.click(scrollRightButton); + }); + await new Promise((resolve) => setTimeout(resolve, 500)); + expect(carousel.scrollLeft).toBeGreaterThan(0); }); }); diff --git a/src/screens/UserPortal/Home/Home.tsx b/src/screens/UserPortal/Home/Home.tsx index 5dc9f6db71..b0d04681f0 100644 --- a/src/screens/UserPortal/Home/Home.tsx +++ b/src/screens/UserPortal/Home/Home.tsx @@ -85,11 +85,8 @@ export default function home(): JSX.Element { const carouselRef = useRef(null); const handleScrollRight = (): void => { - if (carouselRef.current) { - (carouselRef.current as any).scrollBy({ - left: 300, // Adjust this value to change the scroll amount - behavior: 'smooth', - }); + if (carouselRef.current instanceof HTMLElement) { + carouselRef.current.scrollLeft += 300; } }; @@ -210,43 +207,6 @@ export default function home(): JSX.Element { {posts .filter((post) => post.pinned === true) .map((post: any) => { - const allLikes: any = []; - post.likedBy.forEach((value: any) => { - const singleLike = { - firstName: value.firstName, - lastName: value.lastName, - id: value._id, - }; - allLikes.push(singleLike); - }); - - const postComments: any = []; - post.comments.forEach((value: any) => { - const commentLikes: any = []; - - value.likedBy.forEach((commentLike: any) => { - const singleLike = { - id: commentLike._id, - }; - commentLikes.push(singleLike); - }); - - const singleCommnet: any = { - id: value._id, - creator: { - firstName: value.creator.firstName, - lastName: value.creator.lastName, - id: value.creator._id, - image: value.creator.image, - }, - likeCount: value.likeCount, - likedBy: commentLikes, - text: value.text, - }; - - postComments.push(singleCommnet); - }); - const cardProps: InterfacePostCardProps = { id: post._id, creator: { @@ -268,7 +228,7 @@ export default function home(): JSX.Element { @@ -281,43 +241,6 @@ export default function home(): JSX.Element { {posts .filter((post) => post.pinned === false) .map((post: any) => { - const allLikes: any = []; - post.likedBy.forEach((value: any) => { - const singleLike = { - firstName: value.firstName, - lastName: value.lastName, - id: value._id, - }; - allLikes.push(singleLike); - }); - - const postComments: any = []; - post.comments.forEach((value: any) => { - const commentLikes: any = []; - - value.likedBy.forEach((commentLike: any) => { - const singleLike = { - id: commentLike._id, - }; - commentLikes.push(singleLike); - }); - - const singleCommnet: any = { - id: value._id, - creator: { - firstName: value.creator.firstName, - lastName: value.creator.lastName, - id: value.creator._id, - image: value.creator.image, - }, - likeCount: value.likeCount, - likedBy: commentLikes, - text: value.text, - }; - - postComments.push(singleCommnet); - }); - const cardProps: InterfacePostCardProps = { id: post._id, creator: {