Chapter 5 - Testing Article

This commit is contained in:
2022-04-25 23:27:47 -04:00
parent 1c2d6f2b4a
commit 5d9a23422b
4 changed files with 32 additions and 20 deletions

View File

@@ -1,6 +1,10 @@
import { Link, routes } from '@redwoodjs/router' import { Link, routes } from '@redwoodjs/router'
const Article = ({ article }) => { const truncate = (text, length) => {
return text.substring(0, length) + '...'
}
const Article = ({ article, summary = false }) => {
return ( return (
<article> <article>
<header> <header>
@@ -8,7 +12,9 @@ const Article = ({ article }) => {
<Link to={routes.article({ id: article.id })}>{article.title}</Link> <Link to={routes.article({ id: article.id })}>{article.title}</Link>
</h2> </h2>
</header> </header>
<div className="mt-2 text-gray-900 font-light">{article.body}</div> <div className="mt-2 text-gray-900 font-light">
{summary ? truncate(article.body, 100) : article.body}
</div>
</article> </article>
) )
} }

View File

@@ -1,17 +1,17 @@
import Article from './Article' import Article from './Article'
export const generated = () => { const ARTICLE = {
return ( id: 1,
<Article title: 'First Post',
article={{ 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.',
id: 1, }
title: 'First Post',
body: export const full = () => {
'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.', return <Article article={ARTICLE} />
createdAt: '2020-01-01T12:34:56Z', }
}}
/> export const summart = () => {
) return <Article article={ARTICLE} summary={true} />
} }
export default { title: 'Components/Article' } export default { title: 'Components/Article' }

View File

@@ -21,7 +21,7 @@ export const Success = ({ articles }) => {
return ( return (
<div className="space-y-10"> <div className="space-y-10">
{articles.map((article) => ( {articles.map((article) => (
<Article article={article} key={article.id} /> <Article article={article} key={article.id} summary={true} />
))} ))}
</div> </div>
) )

View File

@@ -1,4 +1,4 @@
import { render, screen } from '@redwoodjs/testing' import { render, screen, within } from '@redwoodjs/testing'
import { Loading, Empty, Failure, Success } from './ArticlesCell' import { Loading, Empty, Failure, Success } from './ArticlesCell'
import { standard } from './ArticlesCell.mock' import { standard } from './ArticlesCell.mock'
@@ -25,9 +25,15 @@ describe('ArticlesCell', () => {
const articles = standard().articles const articles = standard().articles
render(<Success articles={articles} />) render(<Success articles={articles} />)
expect(screen.getByText(articles[0].title)).toBeInTheDocument() articles.forEach((article) => {
expect(screen.getByText(articles[0].body)).toBeInTheDocument() const truncatedBody = article.body.substring(0, 10)
expect(screen.getByText(articles[1].title)).toBeInTheDocument() const matchedBody = screen.getByText(truncatedBody, { exact: false })
expect(screen.getByText(articles[1].body)).toBeInTheDocument() const ellipsis = within(matchedBody).getByText('...', { exact: false })
expect(screen.getByText(article.title)).toBeInTheDocument()
expect(screen.queryByText(article.body)).not.toBeInTheDocument()
expect(matchedBody).toBeInTheDocument()
expect(ellipsis).toBeInTheDocument()
})
}) })
}) })