Add blogpost page and cell
This commit is contained in:
@@ -12,6 +12,7 @@ import { Router, Route } from '@redwoodjs/router'
|
||||
const Routes = () => {
|
||||
return (
|
||||
<Router>
|
||||
<Route path="/blog-post/{id:Int}" page={BlogPostPage} name="blogPost" />
|
||||
<Route path="/posts/new" page={NewPostPage} name="newPost" />
|
||||
<Route path="/posts/{id:Int}/edit" page={EditPostPage} name="editPost" />
|
||||
<Route path="/posts/{id:Int}" page={PostPage} name="post" />
|
||||
|
||||
20
web/src/components/BlogPostCell/BlogPostCell.js
Normal file
20
web/src/components/BlogPostCell/BlogPostCell.js
Normal file
@@ -0,0 +1,20 @@
|
||||
export const QUERY = gql`
|
||||
query BlogPostQuery($id: Int!) {
|
||||
post(id: $id) {
|
||||
id
|
||||
title
|
||||
body
|
||||
createdAt
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
export const Loading = () => <div>Loading...</div>
|
||||
|
||||
export const Empty = () => <div>Empty</div>
|
||||
|
||||
export const Failure = ({ error }) => <div>Error: {error.message}</div>
|
||||
|
||||
export const Success = ({ post }) => {
|
||||
return JSON.stringify(post)
|
||||
}
|
||||
6
web/src/components/BlogPostCell/BlogPostCell.mock.js
Normal file
6
web/src/components/BlogPostCell/BlogPostCell.mock.js
Normal file
@@ -0,0 +1,6 @@
|
||||
// Define your own mock data here:
|
||||
export const standard = (/* vars, { ctx, req } */) => ({
|
||||
blogPost: {
|
||||
id: 42,
|
||||
},
|
||||
})
|
||||
20
web/src/components/BlogPostCell/BlogPostCell.stories.js
Normal file
20
web/src/components/BlogPostCell/BlogPostCell.stories.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Loading, Empty, Failure, Success } from './BlogPostCell'
|
||||
import { standard } from './BlogPostCell.mock'
|
||||
|
||||
export const loading = () => {
|
||||
return Loading ? <Loading /> : null
|
||||
}
|
||||
|
||||
export const empty = () => {
|
||||
return Empty ? <Empty /> : null
|
||||
}
|
||||
|
||||
export const failure = () => {
|
||||
return Failure ? <Failure error={new Error('Oh no')} /> : null
|
||||
}
|
||||
|
||||
export const success = () => {
|
||||
return Success ? <Success {...standard()} /> : null
|
||||
}
|
||||
|
||||
export default { title: 'Cells/BlogPostCell' }
|
||||
26
web/src/components/BlogPostCell/BlogPostCell.test.js
Normal file
26
web/src/components/BlogPostCell/BlogPostCell.test.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { render, screen } from '@redwoodjs/testing'
|
||||
import { Loading, Empty, Failure, Success } from './BlogPostCell'
|
||||
import { standard } from './BlogPostCell.mock'
|
||||
|
||||
describe('BlogPostCell', () => {
|
||||
test('Loading renders successfully', () => {
|
||||
render(<Loading />)
|
||||
// Use screen.debug() to see output
|
||||
expect(screen.getByText('Loading...')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('Empty renders successfully', async () => {
|
||||
render(<Empty />)
|
||||
expect(screen.getByText('Empty')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('Failure renders successfully', async () => {
|
||||
render(<Failure error={new Error('Oh no')} />)
|
||||
expect(screen.getByText(/Oh no/i)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('Success renders successfully', async () => {
|
||||
render(<Success blogPost={standard().blogPost} />)
|
||||
expect(screen.getByText(/42/i)).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
@@ -1,3 +1,5 @@
|
||||
import { Link, routes } from '@redwoodjs/router'
|
||||
|
||||
export const QUERY = gql`
|
||||
query BlogPostsQuery {
|
||||
posts {
|
||||
@@ -19,7 +21,9 @@ export const Success = ({ posts }) => {
|
||||
return posts.map((post) => (
|
||||
<article key={post.id}>
|
||||
<header>
|
||||
<h2>{post.title}</h2>
|
||||
<h2>
|
||||
<Link to={routes.blogPost({id: post.id})}>{post.title}</Link>
|
||||
</h2>
|
||||
</header>
|
||||
<p>{post.body}</p>
|
||||
<div>Posted on: {post.createdAt.split('T')[0]}</div>
|
||||
|
||||
12
web/src/pages/BlogPostPage/BlogPostPage.js
Normal file
12
web/src/pages/BlogPostPage/BlogPostPage.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import BlogLayout from 'src/layouts/BlogLayout'
|
||||
import BlogPostCell from 'src/components/BlogPostCell'
|
||||
|
||||
const BlogPostPage = ({id}) => {
|
||||
return (
|
||||
<BlogLayout>
|
||||
<BlogPostCell id={id}/>
|
||||
</BlogLayout>
|
||||
)
|
||||
}
|
||||
|
||||
export default BlogPostPage
|
||||
7
web/src/pages/BlogPostPage/BlogPostPage.stories.js
Normal file
7
web/src/pages/BlogPostPage/BlogPostPage.stories.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import BlogPostPage from './BlogPostPage'
|
||||
|
||||
export const generated = () => {
|
||||
return <BlogPostPage />
|
||||
}
|
||||
|
||||
export default { title: 'Pages/BlogPostPage' }
|
||||
11
web/src/pages/BlogPostPage/BlogPostPage.test.js
Normal file
11
web/src/pages/BlogPostPage/BlogPostPage.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { render } from '@redwoodjs/testing'
|
||||
|
||||
import BlogPostPage from './BlogPostPage'
|
||||
|
||||
describe('BlogPostPage', () => {
|
||||
it('renders successfully', () => {
|
||||
expect(() => {
|
||||
render(<BlogPostPage />)
|
||||
}).not.toThrow()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user