Chapter 6 - Building a Component the Redwood Way - Testing

This commit is contained in:
2022-04-26 00:01:11 -04:00
parent 5d9a23422b
commit e180c93ada
4 changed files with 71 additions and 9 deletions

View File

@@ -2,17 +2,29 @@ import { render, screen } from '@redwoodjs/testing'
import Article from './Article' import Article from './Article'
describe('Article', () => { const ARTICLE = {
it('renders a blog post', () => {
const article = {
id: 1, id: 1,
title: 'First post', title: 'First post',
body: `Neutra tacos hot chicken prism raw denim, put a bird on it enamel pin post-ironic vape cred DIY. Street art next level umami squid. Hammock hexagon glossier 8-bit banjo. Neutra la croix mixtape echo park four loko semiotics kitsch forage chambray. Semiotics salvia selfies jianbing hella shaman. Letterpress helvetica vaporware cronut, shaman butcher YOLO poke fixie hoodie gentrify woke heirloom.`, body: `Neutra tacos hot chicken prism raw denim, put a bird on it enamel pin post-ironic vape cred DIY. Street art next level umami squid. Hammock hexagon glossier 8-bit banjo. Neutra la croix mixtape echo park four loko semiotics kitsch forage chambray. Semiotics salvia selfies jianbing hella shaman. Letterpress helvetica vaporware cronut, shaman butcher YOLO poke fixie hoodie gentrify woke heirloom.`,
createdAt: new Date().toISOString(), createdAt: new Date().toISOString(),
} }
render(<Article article={article} />)
expect(screen.getByText(article.title)).toBeInTheDocument() describe('Article', () => {
expect(screen.getByText(article.body)).toBeInTheDocument() it('renders a blog post', () => {
render(<Article article={ARTICLE} />)
expect(screen.getByText(ARTICLE.title)).toBeInTheDocument()
expect(screen.getByText(ARTICLE.body)).toBeInTheDocument()
})
it('renders a summary of a blog post', () => {
render(<Article article={ARTICLE} summary={true} />)
expect(screen.getByText(ARTICLE.title)).toBeInTheDocument()
expect(
screen.getByText(
'Neutra tacos hot chicken prism raw denim, put a bird on it enamel pin post-ironic vape cred DIY. Str...'
)
).toBeInTheDocument()
}) })
}) })

View File

@@ -0,0 +1,21 @@
const formattedDate = (datetime) => {
const parsedDate = new Date(datetime)
const month = parsedDate.toLocaleString('default', { month: 'long' })
return `${parsedDate.getDate()} ${month} ${parsedDate.getFullYear()}`
}
const Comment = ({ comment }) => {
return (
<div className="bg-gray-200 p-8 rounded-lg">
<header className="flex justify-between">
<h2 className="font-semibold text-gray-700">{comment.name}</h2>
<time className="text-xs text-gray-500" dateTime={comment.createdAt}>
{comment.createdAt}
</time>
</header>
<p className="text-sm mt-2">{comment.body}</p>
</div>
)
}
export default Comment

View File

@@ -0,0 +1,15 @@
import Comment from './Comment'
export const generated = () => {
return (
<Comment
comment={{
name: 'Rob Cameron',
body: 'This is the first comment!',
createdAt: '2022-04-25T23:53:34Z',
}}
/>
)
}
export default { title: 'Components/Comment' }

View File

@@ -0,0 +1,14 @@
import { render } from '@redwoodjs/testing/web'
import Comment from './Comment'
// Improve this test with help from the Redwood Testing Doc:
// https://redwoodjs.com/docs/testing#testing-components
describe('Comment', () => {
it('renders successfully', () => {
expect(() => {
render(<Comment />)
}).not.toThrow()
})
})