Skip to content
Snippets Groups Projects
Commit e8e812ae authored by bruno's avatar bruno
Browse files

add example stories and tests

parent 44806225
No related branches found
No related tags found
No related merge requests found
// Define your own mock data here:
export const standard = (/* vars, { ctx, req } */) => ({
examplePosts: [{ id: 42 }, { id: 43 }, { id: 44 }],
})
import type { Meta, StoryObj } from '@storybook/react'
import { Loading, Empty, Failure, Success } from './ExamplePostsCell'
import { standard } from './ExamplePostsCell.mock'
const meta: Meta = {
title: 'Cells/ExamplePostsCell',
}
export default meta
export const loading: StoryObj<typeof Loading> = {
render: () => {
return Loading ? <Loading /> : <></>
},
}
export const empty: StoryObj<typeof Empty> = {
render: () => {
return Empty ? <Empty /> : <></>
},
}
export const failure: StoryObj<typeof Failure> = {
render: (args) => {
return Failure ? <Failure error={new Error('Oh no')} {...args} /> : <></>
},
}
export const success: StoryObj<typeof Success> = {
render: (args) => {
return Success ? <Success {...standard()} {...args} /> : <></>
},
}
import { render } from '@redwoodjs/testing/web'
import { Loading, Empty, Failure, Success } from './ExamplePostsCell'
import { standard } from './ExamplePostsCell.mock'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-cells
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
describe('ExamplePostsCell', () => {
it('renders Loading successfully', () => {
expect(() => {
render(<Loading />)
}).not.toThrow()
})
it('renders Empty successfully', async () => {
expect(() => {
render(<Empty />)
}).not.toThrow()
})
it('renders Failure successfully', async () => {
expect(() => {
render(<Failure error={new Error('Oh no')} />)
}).not.toThrow()
})
// When you're ready to test the actual output of your component render
// you could test that, for example, certain text is present:
//
// 1. import { screen } from '@redwoodjs/testing/web'
// 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument()
it('renders Success successfully', async () => {
expect(() => {
render(<Success examplePosts={standard().examplePosts} />)
}).not.toThrow()
})
})
import type { ExamplePostsQuery } from 'types/graphql'
import type { CellSuccessProps, CellFailureProps } from '@redwoodjs/web'
export const QUERY = gql`
query ExamplePostsQuery {
examplePosts {
id
}
}
`
export const Loading = () => <div>Loading...</div>
export const Empty = () => <div>Empty</div>
export const Failure = ({ error }: CellFailureProps) => (
<div style={{ color: 'red' }}>Error: {error?.message}</div>
)
export const Success = ({
examplePosts,
}: CellSuccessProps<ExamplePostsQuery>) => {
return (
<ul>
{examplePosts.map((item) => {
return <li key={item.id}>{JSON.stringify(item)}</li>
})}
</ul>
)
}
import * as RTL from '@redwoodjs/testing/web'
import { HomePage } from './home-page'
describe('HomePage', () => {
it('renders successfully', () => {
RTL.render(<HomePage />)
RTL.screen.getByText(/welcome to my app/i)
RTL.screen.getByText(
/This is a simple example of how to add content to your app./i
)
})
})
import type { Meta, StoryObj } from '@storybook/react'
import { HomePage } from './home-page'
const meta: Meta = {
component: HomePage,
title: 'Home',
}
export default meta
export const Default: StoryObj<typeof HomePage> = {
render: () => <HomePage />,
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment