@@ -3,73 +3,85 @@ const { screen, waitFor, fireEvent } = dtl
3
3
4
4
import './index.tsx'
5
5
6
- await testStep (
7
- 'Create button shows correct messages during form submission' ,
6
+ await testStep ( 'Initial ship details are loaded' , async ( ) => {
7
+ await screen . findByRole ( 'heading' , { name : / D r e a d n o u g h t / i } )
8
+ await waitFor (
9
+ ( ) => expect ( screen . queryAllByText ( 'loading' ) ) . toHaveLength ( 0 ) ,
10
+ { timeout : 5000 } ,
11
+ )
12
+ } )
13
+
14
+ const createButton = await testStep (
15
+ 'Create form renders correctly' ,
8
16
async ( ) => {
17
+ expect ( screen . getByLabelText ( 'Ship Name' ) ) . toBeInTheDocument ( )
18
+ expect ( screen . getByLabelText ( 'Top Speed' ) ) . toBeInTheDocument ( )
19
+ expect ( screen . getByLabelText ( 'Image' ) ) . toBeInTheDocument ( )
9
20
const createButton = screen . getByRole ( 'button' , { name : / C r e a t e / i } )
10
21
expect ( createButton ) . toBeInTheDocument ( )
22
+ return createButton
23
+ } ,
24
+ )
11
25
12
- // Mock form submission
13
- fireEvent . click ( createButton )
26
+ const shipName = 'New Test Ship'
27
+ const topSpeed = '9999'
28
+ await testStep ( 'Form submission functions' , async ( ) => {
29
+ fireEvent . change ( screen . getByLabelText ( / S h i p N a m e / i) , {
30
+ target : { value : shipName } ,
31
+ } )
32
+ fireEvent . change ( screen . getByLabelText ( / T o p S p e e d / i) , {
33
+ target : { value : topSpeed } ,
34
+ } )
14
35
15
- // Check for "Creating..." message
36
+ // We can't actually select files via JavaScript, so we need to remove the required attribute
37
+ const imageInput = screen . getByLabelText ( / I m a g e / i)
38
+ imageInput . removeAttribute ( 'required' )
39
+
40
+ fireEvent . click ( createButton )
41
+ } )
42
+
43
+ await testStep (
44
+ 'Create button shows "Creating..." during submission' ,
45
+ async ( ) => {
16
46
await waitFor ( ( ) => {
17
47
expect (
18
- screen . getByRole ( 'button' , { name : / C r e a t i n g .../ i } ) ,
19
- ) . toBeInTheDocument ( )
48
+ createButton ,
49
+ '🚨 The create button should show "Creating..." when the form is submitted' ,
50
+ ) . toHaveTextContent ( 'Creating...' )
20
51
} )
52
+ } ,
53
+ )
21
54
22
- // Check for "Created! Loading..." message
55
+ await testStep (
56
+ 'Create button shows "Created! Loading..." after creation' ,
57
+ async ( ) => {
23
58
await waitFor (
24
59
( ) => {
25
60
expect (
26
- screen . getByRole ( 'button' , { name : / C r e a t e d ! L o a d i n g .../ i } ) ,
27
- ) . toBeInTheDocument ( )
61
+ createButton ,
62
+ '🚨 The create button should show "Created! Loading..." after the ship is created' ,
63
+ ) . toHaveTextContent ( 'Created! Loading...' )
28
64
} ,
29
65
{ timeout : 5000 } ,
30
66
)
31
-
32
- // Wait for submission to complete and button to return to initial state
33
- await screen . findByRole ( 'button' , { name : / C r e a t e / i } , { timeout : 5000 } )
34
67
} ,
35
68
)
36
69
37
- await testStep ( 'Optimistic UI updates with correct messages' , async ( ) => {
38
- const shipName = 'Message Test Ship'
39
- const topSpeed = '8888'
40
-
41
- fireEvent . change ( screen . getByLabelText ( 'Ship Name' ) , {
42
- target : { value : shipName } ,
43
- } )
44
- fireEvent . change ( screen . getByLabelText ( 'Top Speed' ) , {
45
- target : { value : topSpeed } ,
46
- } )
47
-
48
- // Mock file input
49
- const file = new File ( [ 'dummy content' ] , 'test.png' , { type : 'image/png' } )
50
- fireEvent . change ( screen . getByLabelText ( 'Image' ) , {
51
- target : { files : [ file ] } ,
52
- } )
53
-
54
- fireEvent . click ( screen . getByRole ( 'button' , { name : / C r e a t e / i } ) )
70
+ await testStep (
71
+ 'Create button shows "Create" after full submission' ,
72
+ async ( ) => {
73
+ await waitFor (
74
+ ( ) => {
75
+ expect (
76
+ createButton ,
77
+ '🚨 The create button should show "Create" when the request is complete' ,
78
+ ) . toHaveTextContent ( 'Create' )
79
+ } ,
80
+ { timeout : 5000 } ,
81
+ )
82
+ } ,
83
+ )
55
84
56
- // Check for optimistic update with "Creating..." message
85
+ await testStep ( 'New ship is displayed' , async ( ) => {
57
86
await screen . findByRole ( 'heading' , { name : new RegExp ( shipName , 'i' ) } )
58
- await screen . findByText ( new RegExp ( topSpeed , 'i' ) )
59
- expect (
60
- screen . getByRole ( 'button' , { name : / C r e a t i n g .../ i } ) ,
61
- ) . toBeInTheDocument ( )
62
-
63
- // Check for "Created! Loading..." message
64
- await waitFor (
65
- ( ) => {
66
- expect (
67
- screen . getByRole ( 'button' , { name : / C r e a t e d ! L o a d i n g .../ i } ) ,
68
- ) . toBeInTheDocument ( )
69
- } ,
70
- { timeout : 5000 } ,
71
- )
72
-
73
- // Wait for the actual data to load and button to return to initial state
74
- await screen . findByRole ( 'button' , { name : / C r e a t e / i } , { timeout : 5000 } )
75
87
} )
0 commit comments