-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove shortHumanReadableDate abstraction, and add unit tes…
…ts to Common/BlogPostCard
- Loading branch information
1 parent
46ec79e
commit 040f26f
Showing
5 changed files
with
154 additions
and
50 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
components/Common/BlogPostCard/__tests__/index.test.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import BlogPostCard from '@/components/Common/BlogPostCard'; | ||
import { LocaleProvider } from '@/providers/localeProvider'; | ||
|
||
function renderBlogPostCard({ | ||
title = 'Blog post title', | ||
type = 'vulnerability', | ||
description = 'Blog post description', | ||
authors = [], | ||
date = new Date(), | ||
}) { | ||
render( | ||
<LocaleProvider> | ||
<BlogPostCard | ||
title={title} | ||
type={type} | ||
description={description} | ||
authors={authors} | ||
date={date} | ||
/> | ||
</LocaleProvider> | ||
); | ||
|
||
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({}); | ||
|
||
// Title from Preview component | ||
expect(screen.getByRole('heading', { level: 2 })).toHaveTextContent( | ||
title | ||
); | ||
|
||
// The second title should be hidden for screen-readers | ||
// to prevent them from reading it twice | ||
expect(screen.getAllByText(title)[1]).toHaveAttribute( | ||
'aria-hidden', | ||
'true' | ||
); | ||
}); | ||
|
||
it('Renders the description prop correctly', () => { | ||
const { description } = renderBlogPostCard({}); | ||
|
||
expect(screen.getByText(description)).toBeVisible(); | ||
}); | ||
|
||
it.each([ | ||
{ label: 'Vulnerabilities', type: 'vulnerability' }, | ||
{ label: 'Announcements', type: 'announcement' }, | ||
{ label: 'Releases', 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 = [ | ||
{ fullName: 'Jane Doe', src: '' }, | ||
{ fullName: 'John Doe', src: '' }, | ||
]; | ||
|
||
renderBlogPostCard({ authors }); | ||
|
||
const fullNames = authors.reduce((prev, curr, index) => { | ||
if (index === 0) { | ||
return curr.fullName; | ||
} | ||
|
||
return `${prev}, ${curr.fullName}`; | ||
}, ''); | ||
|
||
expect(screen.getByText(fullNames)).toBeVisible(); | ||
}); | ||
|
||
it('Renders all passed authors fullName(s), comma-separated', () => { | ||
const authors = [ | ||
{ fullName: 'Jane Doe', src: '' }, | ||
{ fullName: 'John Doe', src: '' }, | ||
]; | ||
|
||
renderBlogPostCard({ authors }); | ||
|
||
const fullNames = authors.reduce((prev, curr, index) => { | ||
if (index === 0) { | ||
return curr.fullName; | ||
} | ||
|
||
return `${prev}, ${curr.fullName}`; | ||
}, ''); | ||
|
||
expect(screen.getByText(fullNames)).toBeVisible(); | ||
}); | ||
|
||
it('Renders date prop in short format', () => { | ||
const date = new Date(); | ||
|
||
renderBlogPostCard({ date }); | ||
|
||
const dateTimeFormat = new Intl.DateTimeFormat(navigator.language, { | ||
day: 'numeric', | ||
month: 'short', | ||
year: 'numeric', | ||
}); | ||
|
||
expect(screen.getByText(dateTimeFormat.format(date))).toBeVisible(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.