Skip to content

Commit 8ba2ef1

Browse files
committed
03/01: adjust the alert role assertion
1 parent 8b426d0 commit 8ba2ef1

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

exercises/03.best-practices/01.solution.network-mocking/README.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ Problem: performing requests to an actual server makes your test dependent on th
44

55
Mocking APIs is a part of the _setup_ so it makes sense we start from it. It unblocks proper testing in the future exercises.
66

7-
1. How to use MSW in Vitest Browser Mode. What integration you need and how to access the `worker` in test.
7+
## Steps
8+
9+
1. Install `msw`.
10+
1. Init the worker script.
11+
1. Create `/mocks/handlers.ts` with happy-path handlers, and `/mocks/browser.ts` with the worker.
12+
1. Create a custom _test context_ in the `test-extend.ts` file. Don't forget about `auto` to enable MSW even when it's not referenced in the test! (to rely on happy path handlers)
13+
1. Write the test cases for the discount form component.

exercises/03.best-practices/01.solution.network-mocking/src/discount-code-form.browser.test.tsx

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,8 @@ test('displays a warning for legacy discount codes', async ({ worker }) => {
5050
.element(page.getByText('Discount: LEGA2000 (-10%)'))
5151
.toBeVisible()
5252
await expect
53-
.element(
54-
page.getByText('"LEGA2000" is a legacy code. Discount amount halfed.'),
55-
/**
56-
* @todo @fixme Use the role-based selector when the Vitest bug is fixed.
57-
* @see https://github.com/vitest-dev/vitest/issues/7327
58-
*/
59-
// page.getByRole('alert', {
60-
// name: '"LEGA2000" is a legacy code. Discount amount halfed.',
61-
// }),
62-
)
63-
.toBeVisible()
53+
.element(page.getByRole('alert'))
54+
.toHaveTextContent('"LEGA2000" is a legacy code. Discount amount halfed.')
6455
})
6556

6657
test('displays an error when fetching the discount code', async ({
@@ -83,9 +74,6 @@ test('displays an error when fetching the discount code', async ({
8374
await userEvent.click(submitButton)
8475

8576
await expect
86-
.element(
87-
page.getByText('Failed to apply the discount code'),
88-
// page.getByRole('alert', { name: 'Failed to apply the discount code' }),
89-
)
90-
.toBeVisible()
77+
.element(page.getByRole('alert'))
78+
.toHaveTextContent('Failed to apply the discount code')
9179
})

exercises/03.best-practices/01.solution.network-mocking/src/discount-code-form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export function DiscountCodeForm() {
104104
return (
105105
<section className="w-96 rounded-lg border bg-white p-10">
106106
{state.submitting ? (
107-
<p>
107+
<p aria-hidden>
108108
<span className="inline-block animate-spin font-bold">{'◡'}</span>
109109
</p>
110110
) : state.discount ? (

exercises/03.best-practices/02.solution.accessibility-selectors/README.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ Best practices:
1515

1616
- Avoid `.getByTestId()`
1717
- Prefer `.getByLabelText()` vs `.getByPlaceholder()`
18+
19+
Resources:
20+
21+
- https://github.com/AriPerkkio/extend-to-be-announced for asserting `alert` and other live elements that get announced.

0 commit comments

Comments
 (0)