Skip to content

Commit 47c9208

Browse files
committed
storybook play のテストケースを追加し、実際の画面操作を再現
1 parent 8b9a1b8 commit 47c9208

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

sample/src/pages/product-list.stories.tsx

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ProductList from '@/pages/product-list'
22
import type { Meta, StoryObj } from '@storybook/react'
3-
import { userEvent, within } from '@storybook/test'
3+
import { expect, userEvent, waitFor, within } from '@storybook/test'
44
import { MemoryRouter, Route, Routes } from 'react-router-dom'
55

66
const meta: Meta<typeof ProductList> = {
@@ -18,7 +18,7 @@ export default meta
1818
type Story = StoryObj<typeof ProductList>
1919

2020
/**
21-
*
21+
* デフォルト表示
2222
*/
2323
export const Default: Story = {
2424
render: () => {
@@ -30,9 +30,24 @@ export const Default: Story = {
3030
</MemoryRouter>
3131
)
3232
},
33+
}
34+
35+
/** ---- オブジェクト削除のストーリー ---- */
36+
export const DeleteProduct: Story = {
37+
render: () => {
38+
return (
39+
<MemoryRouter initialEntries={['/']}>
40+
<Routes>
41+
<Route path="/" element={<ProductList />} />
42+
</Routes>
43+
</MemoryRouter>
44+
)
45+
},
3346
play: async ({ canvasElement }) => {
3447
const canvas = within(canvasElement)
3548

49+
expect(await canvas.findByText('Product 1')).toBeInTheDocument()
50+
3651
const deleteButton = (
3752
await canvas.findAllByRole('button', { name: '削除' })
3853
)[0]
@@ -43,5 +58,53 @@ export const Default: Story = {
4358
)[0]
4459

4560
await userEvent.click(confirmButton)
61+
62+
await waitFor(() => {
63+
expect(canvas.queryByText('Product 1')).not.toBeInTheDocument()
64+
})
65+
},
66+
}
67+
68+
/** ---- オブジェクト追加のストーリー ---- */
69+
export const CreateProduct: Story = {
70+
render: () => {
71+
return (
72+
<MemoryRouter initialEntries={['/']}>
73+
<Routes>
74+
<Route path="/" element={<ProductList />} />
75+
</Routes>
76+
</MemoryRouter>
77+
)
78+
},
79+
play: async ({ canvasElement }) => {
80+
// モーダル内で `createPortal` を使用しており、body 直下に展開されてしまっているため
81+
// parentElement でハックしなければモーダルの要素が取得できない
82+
const canvas = within(canvasElement.parentElement!)
83+
84+
await userEvent.click(
85+
await canvas.findByRole('button', { name: '新規作成' }),
86+
)
87+
88+
await userEvent.type(await canvas.findByLabelText('商品名'), 'Product 100')
89+
await userEvent.type(canvas.getByLabelText('商品単価'), '100')
90+
await userEvent.type(
91+
canvas.getByLabelText('詳細'),
92+
'Description for product 3',
93+
)
94+
await userEvent.type(
95+
canvas.getByLabelText('イメージURL'),
96+
'https://placehold.jp/123456/abcdef/150x150.png',
97+
)
98+
await userEvent.click(await canvas.findByRole('button', { name: '作成' }))
99+
100+
await waitFor(() => {
101+
expect(canvas.queryByText('新規作成モーダル')).not.toBeInTheDocument()
102+
})
103+
104+
expect(await canvas.findByText('Product 100')).toBeInTheDocument()
105+
expect(await canvas.findByText('価格: 100円')).toBeInTheDocument()
106+
expect(
107+
await canvas.findByText('Description for product 3'),
108+
).toBeInTheDocument()
46109
},
47110
}

0 commit comments

Comments
 (0)