Skip to content

Commit 74d5e7e

Browse files
committed
finish tests
1 parent d24b765 commit 74d5e7e

File tree

6 files changed

+219
-3
lines changed

6 files changed

+219
-3
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { expect, testStep } from '@epic-web/workshop-utils/test'
2+
import { render, screen } from '@testing-library/react'
3+
import { userEvent } from '@testing-library/user-event'
4+
import { App } from './app.tsx'
5+
6+
await testStep('can render the app', () => {
7+
render(<App />)
8+
})
9+
10+
const toggle = await testStep('Toggle is rendered', () =>
11+
screen.findByRole('switch', { name: /party mode/i }),
12+
)
13+
14+
await testStep('Toggle is off to start', () =>
15+
expect(toggle).to.have.attr('aria-checked', 'false'),
16+
)
17+
18+
await testStep(`Renders "Sad town 😭" when off`, () =>
19+
screen.findByText('Sad town 😭'),
20+
)
21+
22+
await testStep(
23+
`Does not render "Let's party 🥳" when off`,
24+
() => expect(screen.queryByText("Let's party 🥳")).to.be.null,
25+
)
26+
27+
await userEvent.click(toggle)
28+
29+
await testStep('Clicking the toggle turns it on', async () => {
30+
expect(toggle).to.have.attr('aria-checked', 'true')
31+
})
32+
33+
await testStep(`Renders "Let's party 🥳" when on`, () =>
34+
screen.findByText("Let's party 🥳"),
35+
)
36+
37+
await testStep(
38+
'Does not render "Sad town 😭" when on',
39+
() => expect(screen.queryByText('Sad town 😭')).to.be.null,
40+
)
41+
42+
const textField = await testStep('TextField is rendered', () =>
43+
screen.findByLabelText('Venue'),
44+
)
45+
46+
await testStep('TextField label and input are associated', () => {
47+
const label = screen.getByText('Venue')
48+
expect(label).to.have.attr('for', textField.id)
49+
})
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { expect, testStep } from '@epic-web/workshop-utils/test'
2+
import { render, screen } from '@testing-library/react'
3+
import { userEvent } from '@testing-library/user-event'
4+
import { App } from './app.tsx'
5+
6+
await testStep('can render the app', () => {
7+
render(<App />)
8+
})
9+
10+
const toggle = await testStep('Toggle is rendered', () =>
11+
screen.findByRole('switch', { name: /party mode/i }),
12+
)
13+
14+
await testStep('Toggle is off to start', () =>
15+
expect(toggle).to.have.attr('aria-checked', 'false'),
16+
)
17+
18+
await testStep(`Renders "Sad town 😭" when off`, () =>
19+
screen.findByText('Sad town 😭'),
20+
)
21+
22+
await testStep(
23+
`Does not render "Let's party 🥳" when off`,
24+
() => expect(screen.queryByText("Let's party 🥳")).to.be.null,
25+
)
26+
27+
await userEvent.click(toggle)
28+
29+
await testStep('Clicking the toggle turns it on', async () => {
30+
expect(toggle).to.have.attr('aria-checked', 'true')
31+
})
32+
33+
await testStep(`Renders "Let's party 🥳" when on`, () =>
34+
screen.findByText("Let's party 🥳"),
35+
)
36+
37+
await testStep(
38+
'Does not render "Sad town 😭" when on',
39+
() => expect(screen.queryByText('Sad town 😭')).to.be.null,
40+
)
41+
42+
const textField = await testStep('TextField is rendered', () =>
43+
screen.findByLabelText('Venue'),
44+
)
45+
46+
await testStep('TextField label and input are associated', () => {
47+
const label = screen.getByText('Venue')
48+
expect(label).to.have.attr('for', textField.id)
49+
})

Diff for: exercises/04.slots/03.solution.prop/index.test.tsx

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { expect, testStep } from '@epic-web/workshop-utils/test'
2+
import { render, screen } from '@testing-library/react'
3+
import { userEvent } from '@testing-library/user-event'
4+
import { App } from './app.tsx'
5+
6+
await testStep('can render the app', () => {
7+
render(<App />)
8+
})
9+
10+
const toggle = await testStep('Toggle is rendered', () =>
11+
screen.findByRole('switch', { name: /party mode/i }),
12+
)
13+
14+
await testStep('Toggle is off to start', () =>
15+
expect(toggle).to.have.attr('aria-checked', 'false'),
16+
)
17+
18+
await testStep(`Renders "Sad town 😭" when off`, () =>
19+
screen.findByText('Sad town 😭'),
20+
)
21+
22+
await testStep(`Does not render "Let's party 🥳" when off`, () => {
23+
const textNode = screen.queryByText("Let's party 🥳")
24+
if (textNode) {
25+
expect(textNode).not.toBeVisible()
26+
}
27+
})
28+
29+
await userEvent.click(toggle)
30+
31+
await testStep('Clicking the toggle turns it on', async () => {
32+
expect(toggle).to.have.attr('aria-checked', 'true')
33+
})
34+
35+
await testStep(`Renders "Let's party 🥳" when on`, () =>
36+
screen.findByText("Let's party 🥳"),
37+
)
38+
39+
await testStep('Does not render "Sad town 😭" when on', () => {
40+
const textNode = screen.queryByText('Sad town 😭')
41+
if (textNode) {
42+
expect(textNode).not.toBeVisible()
43+
}
44+
})
45+
46+
const textField = await testStep('TextField is rendered', () =>
47+
screen.findByLabelText('Venue'),
48+
)
49+
50+
await testStep('TextField label and input are associated', () => {
51+
const label = screen.getByText('Venue')
52+
expect(label).to.have.attr('for', textField.id)
53+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expect, testStep } from '@epic-web/workshop-utils/test'
2+
import { render, screen } from '@testing-library/react'
3+
import { userEvent } from '@testing-library/user-event'
4+
import { App } from './app.tsx'
5+
6+
await testStep('can render the app', () => {
7+
render(<App />)
8+
})
9+
10+
await testStep('Toggle is rendered and initially on', async () => {
11+
const toggleElement = await screen.findByRole('switch')
12+
expect(toggleElement).toHaveAttribute('aria-checked', 'true')
13+
})
14+
15+
await testStep('Toggle can be turned off', async () => {
16+
const toggleElement = await screen.findByRole('switch')
17+
await userEvent.click(toggleElement)
18+
expect(toggleElement).toHaveAttribute('aria-checked', 'false')
19+
})
20+
21+
await testStep('Clicking reset turns the toggle back on', async () => {
22+
const resetButton = await screen.findByRole('button', { name: /reset/i })
23+
await userEvent.click(resetButton)
24+
const toggleElement = await screen.findByRole('switch')
25+
expect(toggleElement).toHaveAttribute('aria-checked', 'true')
26+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect, testStep } from '@epic-web/workshop-utils/test'
2+
import { render, screen } from '@testing-library/react'
3+
import { userEvent } from '@testing-library/user-event'
4+
import { App } from './app.tsx'
5+
6+
await testStep('can render the app', () => {
7+
render(<App />)
8+
})
9+
10+
await testStep('Toggle is rendered and initially on', async () => {
11+
const toggleElement = await screen.findByRole('switch')
12+
expect(toggleElement).toHaveAttribute('aria-checked', 'true')
13+
})
14+
15+
await testStep('Toggle can be turned off', async () => {
16+
const toggleElement = await screen.findByRole('switch')
17+
await userEvent.click(toggleElement)
18+
expect(toggleElement).toHaveAttribute('aria-checked', 'false')
19+
})
20+
21+
await testStep('Changing initialOn updates the initialOn option', async () => {
22+
const initialOnButton = await screen.findByRole('button', {
23+
name: /initialOn/i,
24+
})
25+
await userEvent.click(initialOnButton)
26+
})
27+
28+
await testStep(
29+
'Clicking reset turns the toggle back on even though the initialOn option was changed to false',
30+
async () => {
31+
const resetButton = await screen.findByRole('button', { name: /reset/i })
32+
await userEvent.click(resetButton)
33+
const toggleElement = await screen.findByRole('switch')
34+
expect(
35+
toggleElement,
36+
'🚨 Did you forget to stablize the initalOn value?',
37+
).toHaveAttribute('aria-checked', 'true')
38+
},
39+
)

Diff for: shared/switch.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// your `render` method or the `getTogglerProps` method
66
// (if we've gotten to that part)
77

8-
// this is only a class component so we can do some implementation-detail
9-
// tests to make sure you're doing things as instructed :)
108
export function Switch({
119
on,
1210
className = '',
@@ -25,7 +23,9 @@ export function Switch({
2523
return (
2624
<button
2725
role="switch"
28-
aria-label={ariaLabel ?? 'Toggle'}
26+
// if it has an id then it's probably going to be labeled, otherwise we'll use a generic label
27+
// note, this is mostly for testing purposes
28+
aria-label={ariaLabel ?? ('id' in props ? undefined : 'Toggle')}
2929
aria-checked={on}
3030
onClick={onClick}
3131
className={btnClassName}

0 commit comments

Comments
 (0)