Skip to content

Commit

Permalink
add tests for 05
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Aug 22, 2024
1 parent 8b17bed commit ede24c9
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
22 changes: 22 additions & 0 deletions exercises/05.errors/01.solution.composition/error-boundary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
const { screen } = dtl

import './index.tsx'

testStep(
'Error boundary is rendered immediately due to render error',
async () => {
// Check if the error message is displayed
const errorMessage = await screen.findByRole('alert')
expect(errorMessage).toBeDefined()
expect(errorMessage.textContent).toContain('There was an error:')

// Check if the error message contains the specific error
const errorDetails = await screen.findByText(/invalid time/i)
expect(errorDetails).toBeDefined()

// Ensure the form is not rendered
const form = screen.queryByRole('form')
expect(form).toBeNull()
},
)
3 changes: 1 addition & 2 deletions exercises/05.errors/01.solution.composition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ function OnboardingForm() {
id="startDateInput"
name="startDate"
type="date"
// 💰 you can comment this out to avoid the runtime error
// This line causes the error during render
defaultValue={new Date('today').toISOString().slice(0, 10)}
// defaultValue={new Date().toISOString().slice(0, 10)}
/>
</div>
<button type="submit">Submit</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
const { screen } = dtl
import { userEvent } from '@testing-library/user-event'

const user = userEvent.setup()

import './index.tsx'

testStep('Error boundary is rendered after form submission', async () => {
// submit the form
await user.click(await screen.findByRole('button', { name: /submit/i }))

// Check if the error message is displayed
const errorMessage = await screen.findByRole('alert')
expect(errorMessage).toBeDefined()
expect(errorMessage.textContent).toContain('There was an error:')

// Check if the error message contains the specific error
const errorDetails = await screen.findByText(/toUpperCase/i)
expect(errorDetails).toBeDefined()

// Ensure the form is not rendered after error
const form = screen.queryByRole('form')
expect(form).toBeNull()
})
41 changes: 41 additions & 0 deletions exercises/05.errors/03.solution.reset/error-boundary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
const { screen } = dtl
import { userEvent } from '@testing-library/user-event'

const user = userEvent.setup()

import './index.tsx'

testStep('Error boundary is rendered after form submission', async () => {
// submit the form
await user.click(await screen.findByRole('button', { name: /submit/i }))

// Check if the error message is displayed
const errorMessage = await screen.findByRole('alert')
expect(errorMessage).toBeDefined()
expect(errorMessage.textContent).toContain('There was an error:')

// Check if the error message contains the specific error
const errorDetails = await screen.findByText(/toUpperCase/i)
expect(errorDetails).toBeDefined()

// Ensure the form is not rendered after error
const form = screen.queryByRole('form')
expect(form).toBeNull()
})

testStep('Clicking "Try again" resets the error boundary', async () => {
// Click the "Try again" button
const tryAgainButton = await screen.findByRole('button', {
name: /try again/i,
})
await user.click(tryAgainButton)

// Verify that the submit button appears again
const submitButton = await screen.findByRole('button', { name: /submit/i })
expect(submitButton).toBeDefined()

// Ensure the error message is no longer present
const errorMessage = screen.queryByRole('alert')
expect(errorMessage).toBeNull()
})

0 comments on commit ede24c9

Please sign in to comment.