Skip to content

Commit 609ee76

Browse files
committed
add tests for exercise 3
1 parent 99c1cb5 commit 609ee76

18 files changed

+622
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
import './index.tsx'
5+
6+
const searchBox = await testStep(
7+
'The user can see the search box',
8+
async () => {
9+
const result = await screen.findByRole('searchbox', { name: /search/i })
10+
expect(result).toHaveValue('')
11+
return result
12+
},
13+
)
14+
15+
const dogCheckbox = await testStep(
16+
'The user can see the dog checkbox',
17+
async () => {
18+
const result = await screen.findByRole('checkbox', { name: /dog/i })
19+
expect(result).not.toBeChecked()
20+
return result
21+
},
22+
)
23+
24+
await testStep('The user can search for a checkbox value', async () => {
25+
fireEvent.change(searchBox, { target: { value: 'dog' } })
26+
})
27+
28+
await testStep('checkbox is checked automatically', async () => {
29+
expect(dogCheckbox).toBeChecked()
30+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
import './index.tsx'
5+
6+
const searchBox = await testStep(
7+
'The user can see the search box',
8+
async () => {
9+
const result = await screen.findByRole('searchbox', { name: /search/i })
10+
expect(result).toHaveValue('')
11+
return result
12+
},
13+
)
14+
15+
const dogCheckbox = await testStep(
16+
'The user can see the dog checkbox',
17+
async () => {
18+
const result = await screen.findByRole('checkbox', { name: /dog/i })
19+
expect(result).not.toBeChecked()
20+
return result
21+
},
22+
)
23+
24+
await testStep('The user can select the dog checkbox', async () => {
25+
fireEvent.click(dogCheckbox)
26+
expect(dogCheckbox).toBeChecked()
27+
})
28+
29+
await testStep(
30+
'Selecting the checkbox updates the search and results',
31+
async () => {
32+
// Check that the search box value has been updated
33+
expect(searchBox).toHaveValue('dog')
34+
35+
// Check that the results have been filtered
36+
await dtl.waitFor(async () => {
37+
await screen.findByText(/the joy of owning a dog/i)
38+
39+
const catResult = screen.queryByText(/caring for your feline friend/i)
40+
expect(catResult).not.toBeInTheDocument()
41+
})
42+
},
43+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
import './index.tsx'
5+
6+
const searchBox = await testStep(
7+
'The user can see the search box',
8+
async () => {
9+
const result = await screen.findByRole('searchbox', { name: /search/i })
10+
expect(result).toHaveValue('')
11+
return result
12+
},
13+
)
14+
15+
const catResult = await testStep('The user can see the results', async () => {
16+
const result = screen.getByText(/caring for your feline friend/i)
17+
expect(result).toBeInTheDocument()
18+
return result
19+
})
20+
21+
await testStep('The user can search for a term', async () => {
22+
fireEvent.change(searchBox, { target: { value: 'dog' } })
23+
})
24+
25+
await testStep('The results are filtered', async () => {
26+
await dtl.waitFor(() => {
27+
expect(catResult).not.toBeInTheDocument()
28+
})
29+
await screen.findByText(/the joy of owning a dog/i)
30+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
import './index.tsx'
5+
6+
await testStep('The user can see the posts', async () => {
7+
await screen.findByText(/caring for your feline friend/i)
8+
await screen.findByText(/the joy of owning a dog/i)
9+
})
10+
11+
const likeButtons = await testStep(
12+
'The user can see like buttons',
13+
async () => {
14+
const buttons = await screen.findAllByRole('button', {
15+
name: /add favorite/i,
16+
})
17+
expect(buttons.length).toBeGreaterThan(0)
18+
return buttons
19+
},
20+
)
21+
22+
const totalLikeButtons = likeButtons.length
23+
24+
await testStep('The user can like a post', async () => {
25+
fireEvent.click(likeButtons[0])
26+
await screen.findByRole('button', { name: /remove favorite/i })
27+
})
28+
29+
await testStep('The user can unlike a post', async () => {
30+
const unlikeButton = await screen.findByRole('button', {
31+
name: /remove favorite/i,
32+
})
33+
fireEvent.click(unlikeButton)
34+
await dtl.waitFor(() =>
35+
expect(
36+
screen.queryByRole('button', { name: /remove favorite/i }),
37+
).not.toBeInTheDocument(),
38+
)
39+
const buttons = await screen.findAllByRole('button', {
40+
name: /add favorite/i,
41+
})
42+
expect(buttons.length).toBe(totalLikeButtons)
43+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
const currentPath = window.location.pathname
5+
window.history.pushState({}, '', `${currentPath}?query=dog`)
6+
7+
await import('./index.tsx')
8+
9+
await testStep(
10+
'The search box is initialized with URL query parameter',
11+
async () => {
12+
const searchBox = await screen.findByRole('searchbox', { name: /search/i })
13+
expect(searchBox).toHaveValue('dog')
14+
},
15+
)
16+
17+
// wait for the event handler to be set up
18+
// for some reason it takes a bit
19+
await new Promise(resolve => setTimeout(resolve, 100))
20+
21+
await testStep(
22+
'The search box updates when popstate event is triggered',
23+
async () => {
24+
// Simulate navigation to a new URL
25+
const currentPath = window.location.pathname
26+
window.history.pushState({}, '', `${currentPath}?query=cat`)
27+
28+
// Trigger popstate event
29+
fireEvent.popState(window)
30+
31+
// Check if the search box value is updated
32+
await dtl.waitFor(async () =>
33+
expect(
34+
await screen.findByRole('searchbox', { name: /search/i }),
35+
).toHaveValue('cat'),
36+
)
37+
},
38+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
window.history.pushState({}, '', '?query=dog')
5+
6+
await import('./index.tsx')
7+
8+
await testStep(
9+
'The search box is initialized with URL query parameter',
10+
async () => {
11+
const searchBox = await screen.findByRole('searchbox', { name: /search/i })
12+
expect(searchBox).toHaveValue('dog')
13+
},
14+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
import './index.tsx'
5+
6+
const searchBox = await testStep(
7+
'The user can see the search box',
8+
async () => {
9+
const result = await screen.findByRole('searchbox', { name: /search/i })
10+
expect(result).toHaveValue('')
11+
return result
12+
},
13+
)
14+
15+
const dogCheckbox = await testStep(
16+
'The user can see the dog checkbox',
17+
async () => {
18+
const result = await screen.findByRole('checkbox', { name: /dog/i })
19+
expect(result).not.toBeChecked()
20+
return result
21+
},
22+
)
23+
24+
await testStep('The user can search for a checkbox value', async () => {
25+
fireEvent.change(searchBox, { target: { value: 'dog' } })
26+
})
27+
28+
await testStep('checkbox is checked automatically', async () => {
29+
expect(dogCheckbox).toBeChecked()
30+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
import './index.tsx'
5+
6+
const searchBox = await testStep(
7+
'The user can see the search box',
8+
async () => {
9+
const result = await screen.findByRole('searchbox', { name: /search/i })
10+
expect(result).toHaveValue('')
11+
return result
12+
},
13+
)
14+
15+
const dogCheckbox = await testStep(
16+
'The user can see the dog checkbox',
17+
async () => {
18+
const result = await screen.findByRole('checkbox', { name: /dog/i })
19+
expect(result).not.toBeChecked()
20+
return result
21+
},
22+
)
23+
24+
await testStep('The user can select the dog checkbox', async () => {
25+
fireEvent.click(dogCheckbox)
26+
expect(dogCheckbox).toBeChecked()
27+
})
28+
29+
await testStep(
30+
'Selecting the checkbox updates the search and results',
31+
async () => {
32+
// Check that the search box value has been updated
33+
expect(searchBox).toHaveValue('dog')
34+
35+
// Check that the results have been filtered
36+
await dtl.waitFor(async () => {
37+
await screen.findByText(/the joy of owning a dog/i)
38+
39+
const catResult = screen.queryByText(/caring for your feline friend/i)
40+
expect(catResult).not.toBeInTheDocument()
41+
})
42+
},
43+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
import './index.tsx'
5+
6+
const searchBox = await testStep(
7+
'The user can see the search box',
8+
async () => {
9+
const result = await screen.findByRole('searchbox', { name: /search/i })
10+
expect(result).toHaveValue('')
11+
return result
12+
},
13+
)
14+
15+
const catResult = await testStep('The user can see the results', async () => {
16+
const result = screen.getByText(/caring for your feline friend/i)
17+
expect(result).toBeInTheDocument()
18+
return result
19+
})
20+
21+
await testStep('The user can search for a term', async () => {
22+
fireEvent.change(searchBox, { target: { value: 'dog' } })
23+
})
24+
25+
await testStep('The results are filtered', async () => {
26+
await dtl.waitFor(() => {
27+
expect(catResult).not.toBeInTheDocument()
28+
})
29+
await screen.findByText(/the joy of owning a dog/i)
30+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { expect, testStep, dtl } from '@epic-web/workshop-utils/test'
2+
const { screen, fireEvent } = dtl
3+
4+
import './index.tsx'
5+
6+
await testStep('The user can see the posts', async () => {
7+
await screen.findByText(/caring for your feline friend/i)
8+
await screen.findByText(/the joy of owning a dog/i)
9+
})
10+
11+
const likeButtons = await testStep(
12+
'The user can see like buttons',
13+
async () => {
14+
const buttons = await screen.findAllByRole('button', {
15+
name: /add favorite/i,
16+
})
17+
expect(buttons.length).toBeGreaterThan(0)
18+
return buttons
19+
},
20+
)
21+
22+
const totalLikeButtons = likeButtons.length
23+
24+
await testStep('The user can like a post', async () => {
25+
fireEvent.click(likeButtons[1])
26+
await screen.findByRole('button', { name: /remove favorite/i })
27+
})
28+
29+
await testStep('The liked post moves to the top', async () => {
30+
const posts = screen.getAllByRole('listitem')
31+
const firstPost = posts[0]
32+
expect(
33+
firstPost,
34+
'The first post should have a remove favorite button',
35+
).toContainElement(screen.getByRole('button', { name: /remove favorite/i }))
36+
})
37+
38+
await testStep('The user can unlike a post', async () => {
39+
const unlikeButton = await screen.findByRole('button', {
40+
name: /remove favorite/i,
41+
})
42+
fireEvent.click(unlikeButton)
43+
44+
await dtl.waitFor(() =>
45+
expect(
46+
screen.queryByRole('button', { name: /remove favorite/i }),
47+
).not.toBeInTheDocument(),
48+
)
49+
const buttons = await screen.findAllByRole('button', {
50+
name: /add favorite/i,
51+
})
52+
expect(buttons.length).toBe(totalLikeButtons)
53+
})

0 commit comments

Comments
 (0)