Skip to content

Commit c6bbbc4

Browse files
committed
finished main unit tests for blog component
1 parent be653e4 commit c6bbbc4

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

www/__tests__/Blogs.test.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import React from 'react';
55

66
const posts = [
77
{
8-
title: 'Our Legendary Article here',
9-
href: '#',
10-
category: { name: 'Greatness', href: '#' },
8+
title: 'Traveling through time with Reactime',
9+
href: 'https://medium.com/@reactime/reactime-v17-0-0-now-with-support-for-the-context-api-and-a-modern-ui-f0edf9e54dae',
10+
category: { name: 'React Debugging', href: 'https://medium.com/tag/react-devtools' },
1111
description:
12-
'Reactime v17, we have come a long way from beta. Now introducing full Context API support and CustomHooks support: thereby allowing developers to better visualize the states and ... ',
13-
date: 'Dec 14, 2022',
14-
datetime: '2022-12-14',
12+
'Reactime v17, we have come a long way from beta. Now introducing full Context API support: thereby allowing developers to better visualize the states and ... ',
13+
date: 'Jan 3, 2023',
14+
datetime: '2023-1-03',
1515
imageUrl:
1616
'https://images.unsplash.com/photo-1496128858413-b36217c2ce36?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1679&q=80',
1717
readingTime: '6 min',
1818
author: {
1919
name: 'James Nghiem',
20-
href: '#',
20+
href: 'https://medium.com/@reactime',
2121
imageUrl:
22-
'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80',
22+
'https://github.com/jemzir.png',
2323
},
2424
},
2525
{
@@ -70,10 +70,19 @@ describe('Blog component test ', () => {
7070
});
7171

7272
it ('displays the correct information for each blog post', () => {
73-
const blogs = screen.getAllByTestId('blog')
74-
blogs.forEach((blog) => {
75-
73+
posts.forEach(post => {
74+
expect(screen.getByText(post.title)).toBeInTheDocument();
75+
expect(screen.getByText(post.description.trim())).toBeInTheDocument();
76+
expect(screen.getByText(post.date)).toBeInTheDocument();
77+
expect(screen.getByText(post.category.name)).toBeInTheDocument();
78+
expect(screen.getAllByText(post.author.name)[0]).toBeInTheDocument();
79+
})
80+
});
7681

82+
it ('clicking a post link navigates to the correct URL', () => {
83+
posts.forEach((post, index) => {
84+
const postLink = post.href
85+
expect(screen.getAllByTestId('link')[index]).toHaveAttribute('href', postLink);
7786
})
7887
});
7988
});

www/src/pages/components/Blogs.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const posts = [
55
category: { name: 'React Debugging', href: 'https://medium.com/tag/react-devtools' },
66
description:
77
'Reactime v17, we have come a long way from beta. Now introducing full Context API support: thereby allowing developers to better visualize the states and ... ',
8-
date: 'Jan 3, 2022',
9-
datetime: '2022-1-03',
8+
date: 'Jan 3, 2023',
9+
datetime: '2023-1-03',
1010
imageUrl:
1111
'https://images.unsplash.com/photo-1496128858413-b36217c2ce36?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1679&q=80',
1212
readingTime: '6 min',
@@ -80,7 +80,7 @@ export default function Blogs() {
8080
{post.category.name}
8181
</a>
8282
</p>
83-
<a href={post.href} className="mt-2 block">
83+
<a href={post.href} data-testid='link' className="mt-2 block">
8484
<p className="text-xl font-semibold text-gray-900">{post.title}</p>
8585
<p className="mt-3 text-base text-gray-500">{post.description}</p>
8686
</a>

0 commit comments

Comments
 (0)