Chapter 6 - Building a Component the Redwood Way - Testing
This commit is contained in:
@@ -2,17 +2,29 @@ import { render, screen } from '@redwoodjs/testing'
|
||||
|
||||
import Article from './Article'
|
||||
|
||||
const ARTICLE = {
|
||||
id: 1,
|
||||
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.`,
|
||||
createdAt: new Date().toISOString(),
|
||||
}
|
||||
|
||||
describe('Article', () => {
|
||||
it('renders a blog post', () => {
|
||||
const article = {
|
||||
id: 1,
|
||||
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.`,
|
||||
createdAt: new Date().toISOString(),
|
||||
}
|
||||
render(<Article article={article} />)
|
||||
render(<Article article={ARTICLE} />)
|
||||
|
||||
expect(screen.getByText(article.title)).toBeInTheDocument()
|
||||
expect(screen.getByText(article.body)).toBeInTheDocument()
|
||||
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()
|
||||
})
|
||||
})
|
||||
|
||||
21
web/src/components/Comment/Comment.js
Normal file
21
web/src/components/Comment/Comment.js
Normal 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
|
||||
15
web/src/components/Comment/Comment.stories.js
Normal file
15
web/src/components/Comment/Comment.stories.js
Normal 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' }
|
||||
14
web/src/components/Comment/Comment.test.js
Normal file
14
web/src/components/Comment/Comment.test.js
Normal 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()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user