Skip to content

Commit 35921a1

Browse files
committed
03/05: improve exercise
1 parent 487ff6e commit 35921a1

File tree

6 files changed

+34
-21
lines changed

6 files changed

+34
-21
lines changed

Diff for: exercises/03.best-practices/04.problem.element-presence/README.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ So far we've covered finding and interacting with elements that are present or w
44

55
This is where you would normally reach for an [inverse assertion](https://www.epicweb.dev/inverse-assertions) to get a predictable result and guard yourself from false-positive tests:
66

7-
```ts
7+
```ts nonumber
88
const notificationVisiblePromise = vi.waitFor(() => {
99
expect(notification).toBeVisible()
1010
})
@@ -17,7 +17,7 @@ await expect(notificationVisiblePromise).rejects.toThrow()
1717

1818
Or, alternatively, use the designated `waitForElementToBeRemoved()` utility from React Testing Library:
1919

20-
```ts
20+
```ts nonumber
2121
await waitForElementToBeRemoved(notification)
2222
```
2323

Diff for: exercises/03.best-practices/05.problem.page-navigation/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"dependencies": {
1010
"react": "^19.0.0",
11-
"react-dom": "^19.0.0"
11+
"react-dom": "^19.0.0",
12+
"react-router": "^7.2.0"
1213
},
1314
"devDependencies": {
1415
"@tailwindcss/vite": "^4.0.7",

Diff for: exercises/03.best-practices/05.problem.page-navigation/src/discount-code-form.browser.test.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { DiscountCodeForm, type Discount } from './discount-code-form'
88
// Make it a React component that renders <MemoryRouter>
99
// from the "react-router" package around any `children`
1010
// provided to the `wrapper` component.
11-
// 💰 const wrapper: React.JSXElementConstructor<{ children: React.ReactNode }> => () => {}
11+
// 💰 const wrapper: React.JSXElementConstructor<{ children: React.ReactNode }> = () => {}
1212
// 💰 <MemoryRouter>{children}</MemoryRouter>
1313

1414
test('applies a discount code', async () => {
@@ -119,11 +119,14 @@ test('displays the "Back to cart" link', async () => {
119119
// 🐨 Provide the `wrapper` for this render.
120120
render(<DiscountCodeForm />)
121121

122-
// 🐨 Locate the "Back to cart" link on the page.
123-
// Assign its locator to a variable called `backToCartLink`.
122+
// 🐨 Declare a new variable called `backToCartLink` and
123+
// assign it a locator of the element with the role "link"
124+
// and accessible text "Back to cart".
124125
// 💰 page.getByRole(role, { name })
125126

126-
// 🐨 Write an assertion that the `backToCartLink` has the
127-
// correct value of the "href" attribute.
127+
// 🐨 Add an assertion that the `backToCartLink` element is visible.
128+
129+
// 🐨 Add another assertion that the `backToCartLink` has the
130+
// correct value of the "href" attribute ("/cart").
128131
// 💰 .toHaveAttribute(attributeName, attributeValue)
129132
})

Diff for: exercises/03.best-practices/05.solution.page-navigation/README.mdx

+13
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ test('displays the "Back to cart" link', async () => {
6060
})
6161
```
6262

63+
Next, I will add a quick assertion that this link is visible:
64+
65+
```tsx filename=discount-code-form.browser.test.tsx add=5
66+
test('displays the "Back to cart" link', async () => {
67+
render(<DiscountCodeForm />, { wrapper })
68+
69+
const backToCartLink = page.getByRole('link', { name: 'Back to cart' })
70+
await expect.element(backToCartLink).toBeVisible()
71+
})
72+
```
73+
74+
> 🦉 The `.toBeVisible()` matcher provides a bunch of implicit assertions to make sure that the element is visible and also **accessible** to the user.
75+
6376
This is the moment where the user would click on the link and land on the cart page. **But our test is not going to do that**. Instead, all I will do is make sure that the link got rendered with the correct attributes:
6477

6578
```tsx filename=discount-code-form.browser.test.tsx add=5

Diff for: exercises/03.best-practices/05.solution.page-navigation/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"dependencies": {
1010
"react": "^19.0.0",
11-
"react-dom": "^19.0.0"
11+
"react-dom": "^19.0.0",
12+
"react-router": "^7.2.0"
1213
},
1314
"devDependencies": {
1415
"@tailwindcss/vite": "^4.0.7",
@@ -18,7 +19,6 @@
1819
"@vitest/browser": "^3.0.5",
1920
"msw": "^2.7.3",
2021
"playwright": "^1.49.1",
21-
"react-router": "^7.1.3",
2222
"tailwindcss": "^4.0.7",
2323
"vite": "^6.0.7",
2424
"vitest": "^3.0.5"

Diff for: package-lock.json

+7-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)