1
1
import ProductList from '@/pages/product-list'
2
2
import type { Meta , StoryObj } from '@storybook/react'
3
- import { userEvent , within } from '@storybook/test'
3
+ import { expect , userEvent , waitFor , within } from '@storybook/test'
4
4
import { MemoryRouter , Route , Routes } from 'react-router-dom'
5
5
6
6
const meta : Meta < typeof ProductList > = {
@@ -18,7 +18,7 @@ export default meta
18
18
type Story = StoryObj < typeof ProductList >
19
19
20
20
/**
21
- *
21
+ * デフォルト表示
22
22
*/
23
23
export const Default : Story = {
24
24
render : ( ) => {
@@ -30,9 +30,24 @@ export const Default: Story = {
30
30
</ MemoryRouter >
31
31
)
32
32
} ,
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
+ } ,
33
46
play : async ( { canvasElement } ) => {
34
47
const canvas = within ( canvasElement )
35
48
49
+ expect ( await canvas . findByText ( 'Product 1' ) ) . toBeInTheDocument ( )
50
+
36
51
const deleteButton = (
37
52
await canvas . findAllByRole ( 'button' , { name : '削除' } )
38
53
) [ 0 ]
@@ -43,5 +58,53 @@ export const Default: Story = {
43
58
) [ 0 ]
44
59
45
60
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 ( )
46
109
} ,
47
110
}
0 commit comments