-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathindex.test.mjs
113 lines (87 loc) · 2.88 KB
/
index.test.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { render, screen } from '@testing-library/react';
import BlogPostCard from '@/components/Blog/BlogPostCard';
function renderBlogPostCard({
title = 'Blog post title',
type = 'vulnerability',
description = 'Blog post description',
authors = [],
date = new Date(),
slug = '',
}) {
render(
<BlogPostCard
title={title}
category={type}
description={description}
authors={authors}
date={date}
slug={slug}
/>
);
return { title, type, description, authors, date };
}
describe('BlogPostCard', () => {
describe('Rendering', () => {
it('Wraps the entire card within an article', () => {
renderBlogPostCard({});
expect(screen.getByRole('article')).toBeVisible();
});
it('Renders the title prop correctly', () => {
const { title } = renderBlogPostCard({});
expect(screen.getAllByText(title).length).toBe(2);
// title from preview should be ignored as the one from Links
// and blog card/post are what matter
expect(screen.getAllByText(title)[0]).toHaveAttribute(
'aria-hidden',
'true'
);
});
it('Renders the description prop correctly', () => {
const { description } = renderBlogPostCard({});
expect(screen.getByText(description)).toBeVisible();
});
it.each([
{ label: 'layouts.blog.categories.vulnerability', type: 'vulnerability' },
{ label: 'layouts.blog.categories.announcements', type: 'announcements' },
{ label: 'layouts.blog.categories.release', type: 'release' },
])(
'Renders "%label" text when passing it the type "%type"',
({ label, type }) => {
renderBlogPostCard({ type });
expect(screen.getByText(label)).toBeVisible();
}
);
it('Renders all passed authors fullName(s), comma-separated', () => {
const authors = ['Jane Doe', 'John Doe'];
renderBlogPostCard({ authors });
const fullNames = authors.reduce((prev, curr, index) => {
if (index === 0) {
return curr;
}
return `${prev}, ${curr}`;
}, '');
expect(screen.getByText(fullNames)).toBeVisible();
});
it('Renders all passed authors fullName(s), comma-separated', () => {
const authors = ['Jane Doe', 'John Doe'];
renderBlogPostCard({ authors });
const fullNames = authors.reduce((prev, curr, index) => {
if (index === 0) {
return curr;
}
return `${prev}, ${curr}`;
}, '');
expect(screen.getByText(fullNames)).toBeVisible();
});
it('Renders date prop in short format', () => {
const date = new Date();
renderBlogPostCard({ date });
const dateTimeFormat = new Intl.DateTimeFormat('en-US', {
day: '2-digit',
month: 'short',
year: 'numeric',
});
expect(screen.getByText(dateTimeFormat.format(date))).toBeVisible();
});
});
});