Skip to content

Commit 4dd50e0

Browse files
committed
finish exercise 3
1 parent 3e318a2 commit 4dd50e0

File tree

1 file changed

+61
-49
lines changed

1 file changed

+61
-49
lines changed

exercises/03.optimistic/03.solution.message/message.test.ts

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,85 @@ const { screen, waitFor, fireEvent } = dtl
33

44
import './index.tsx'
55

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: /Dreadnought/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',
816
async () => {
17+
expect(screen.getByLabelText('Ship Name')).toBeInTheDocument()
18+
expect(screen.getByLabelText('Top Speed')).toBeInTheDocument()
19+
expect(screen.getByLabelText('Image')).toBeInTheDocument()
920
const createButton = screen.getByRole('button', { name: /Create/i })
1021
expect(createButton).toBeInTheDocument()
22+
return createButton
23+
},
24+
)
1125

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(/Ship Name/i), {
30+
target: { value: shipName },
31+
})
32+
fireEvent.change(screen.getByLabelText(/Top Speed/i), {
33+
target: { value: topSpeed },
34+
})
1435

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(/Image/i)
38+
imageInput.removeAttribute('required')
39+
40+
fireEvent.click(createButton)
41+
})
42+
43+
await testStep(
44+
'Create button shows "Creating..." during submission',
45+
async () => {
1646
await waitFor(() => {
1747
expect(
18-
screen.getByRole('button', { name: /Creating.../i }),
19-
).toBeInTheDocument()
48+
createButton,
49+
'🚨 The create button should show "Creating..." when the form is submitted',
50+
).toHaveTextContent('Creating...')
2051
})
52+
},
53+
)
2154

22-
// Check for "Created! Loading..." message
55+
await testStep(
56+
'Create button shows "Created! Loading..." after creation',
57+
async () => {
2358
await waitFor(
2459
() => {
2560
expect(
26-
screen.getByRole('button', { name: /Created! Loading.../i }),
27-
).toBeInTheDocument()
61+
createButton,
62+
'🚨 The create button should show "Created! Loading..." after the ship is created',
63+
).toHaveTextContent('Created! Loading...')
2864
},
2965
{ timeout: 5000 },
3066
)
31-
32-
// Wait for submission to complete and button to return to initial state
33-
await screen.findByRole('button', { name: /Create/i }, { timeout: 5000 })
3467
},
3568
)
3669

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: /Create/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+
)
5584

56-
// Check for optimistic update with "Creating..." message
85+
await testStep('New ship is displayed', async () => {
5786
await screen.findByRole('heading', { name: new RegExp(shipName, 'i') })
58-
await screen.findByText(new RegExp(topSpeed, 'i'))
59-
expect(
60-
screen.getByRole('button', { name: /Creating.../i }),
61-
).toBeInTheDocument()
62-
63-
// Check for "Created! Loading..." message
64-
await waitFor(
65-
() => {
66-
expect(
67-
screen.getByRole('button', { name: /Created! Loading.../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: /Create/i }, { timeout: 5000 })
7587
})

0 commit comments

Comments
 (0)