|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | + |
| 3 | +import BlogPostCard from '@/components/Common/BlogPostCard'; |
| 4 | +import { LocaleProvider } from '@/providers/localeProvider'; |
| 5 | + |
| 6 | +function renderBlogPostCard({ |
| 7 | + title = 'Blog post title', |
| 8 | + type = 'vulnerability', |
| 9 | + description = 'Blog post description', |
| 10 | + authors = [], |
| 11 | + date = new Date(), |
| 12 | +}) { |
| 13 | + render( |
| 14 | + <LocaleProvider> |
| 15 | + <BlogPostCard |
| 16 | + title={title} |
| 17 | + type={type} |
| 18 | + description={description} |
| 19 | + authors={authors} |
| 20 | + date={date} |
| 21 | + /> |
| 22 | + </LocaleProvider> |
| 23 | + ); |
| 24 | + |
| 25 | + return { title, type, description, authors, date }; |
| 26 | +} |
| 27 | + |
| 28 | +describe('BlogPostCard', () => { |
| 29 | + describe('Rendering', () => { |
| 30 | + it('Wraps the entire card within an article', () => { |
| 31 | + renderBlogPostCard({}); |
| 32 | + |
| 33 | + expect(screen.getByRole('article')).toBeVisible(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('Renders the title prop correctly', () => { |
| 37 | + const { title } = renderBlogPostCard({}); |
| 38 | + |
| 39 | + // Title from Preview component |
| 40 | + expect(screen.getByRole('heading', { level: 2 })).toHaveTextContent( |
| 41 | + title |
| 42 | + ); |
| 43 | + |
| 44 | + // The second title should be hidden for screen-readers |
| 45 | + // to prevent them from reading it twice |
| 46 | + expect(screen.getAllByText(title)[1]).toHaveAttribute( |
| 47 | + 'aria-hidden', |
| 48 | + 'true' |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('Renders the description prop correctly', () => { |
| 53 | + const { description } = renderBlogPostCard({}); |
| 54 | + |
| 55 | + expect(screen.getByText(description)).toBeVisible(); |
| 56 | + }); |
| 57 | + |
| 58 | + it.each([ |
| 59 | + { label: 'Vulnerabilities', type: 'vulnerability' }, |
| 60 | + { label: 'Announcements', type: 'announcement' }, |
| 61 | + { label: 'Releases', type: 'release' }, |
| 62 | + ])( |
| 63 | + 'Renders "%label" text when passing it the type "%type"', |
| 64 | + ({ label, type }) => { |
| 65 | + renderBlogPostCard({ type }); |
| 66 | + |
| 67 | + expect(screen.getByText(label)).toBeVisible(); |
| 68 | + } |
| 69 | + ); |
| 70 | + |
| 71 | + it('Renders all passed authors fullName(s), comma-separated', () => { |
| 72 | + const authors = [ |
| 73 | + { fullName: 'Jane Doe', src: '' }, |
| 74 | + { fullName: 'John Doe', src: '' }, |
| 75 | + ]; |
| 76 | + |
| 77 | + renderBlogPostCard({ authors }); |
| 78 | + |
| 79 | + const fullNames = authors.reduce((prev, curr, index) => { |
| 80 | + if (index === 0) { |
| 81 | + return curr.fullName; |
| 82 | + } |
| 83 | + |
| 84 | + return `${prev}, ${curr.fullName}`; |
| 85 | + }, ''); |
| 86 | + |
| 87 | + expect(screen.getByText(fullNames)).toBeVisible(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('Renders all passed authors fullName(s), comma-separated', () => { |
| 91 | + const authors = [ |
| 92 | + { fullName: 'Jane Doe', src: '' }, |
| 93 | + { fullName: 'John Doe', src: '' }, |
| 94 | + ]; |
| 95 | + |
| 96 | + renderBlogPostCard({ authors }); |
| 97 | + |
| 98 | + const fullNames = authors.reduce((prev, curr, index) => { |
| 99 | + if (index === 0) { |
| 100 | + return curr.fullName; |
| 101 | + } |
| 102 | + |
| 103 | + return `${prev}, ${curr.fullName}`; |
| 104 | + }, ''); |
| 105 | + |
| 106 | + expect(screen.getByText(fullNames)).toBeVisible(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('Renders date prop in short format', () => { |
| 110 | + const date = new Date(); |
| 111 | + |
| 112 | + renderBlogPostCard({ date }); |
| 113 | + |
| 114 | + const dateTimeFormat = new Intl.DateTimeFormat(navigator.language, { |
| 115 | + day: 'numeric', |
| 116 | + month: 'short', |
| 117 | + year: 'numeric', |
| 118 | + }); |
| 119 | + |
| 120 | + expect(screen.getByText(dateTimeFormat.format(date))).toBeVisible(); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments