Skip to content

Commit 0e8c217

Browse files
committed
restructure best practices
1 parent 71f3610 commit 0e8c217

File tree

94 files changed

+777
-1249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+777
-1249
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Accessibility selectors
2+
3+
Now that we have MSW set up, let's write some tests for the Discount code form! For that, we will need to select elements and interact with them just like our users would.
4+
5+
- Why accessibility selectors matter?
6+
- Why not `test-id`?
7+
- Implicit benefits of selectors (validate the accessibility of your markup; you cannot force your way to things the user cannot "see" on the page)
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"type": "module",
3-
"name": "exercises_03.best-practices_02.solution.accessibility-selectors",
3+
"name": "exercises_03.best-practices_01.solution.accessibility-selectors",
44
"scripts": {
55
"dev": "vite",
66
"test": "vitest"
@@ -16,18 +16,12 @@
1616
"@types/react": "^19.0.6",
1717
"@types/react-dom": "^19.0.3",
1818
"@vitejs/plugin-react": "^4.3.4",
19-
"@vitest/browser": "^3.0.2",
19+
"@vitest/browser": "^3.0.3",
2020
"autoprefixer": "^10.4.20",
21-
"msw": "^2.7.0",
2221
"playwright": "^1.49.1",
2322
"postcss": "^8.4.49",
2423
"tailwindcss": "^3.4.17",
2524
"vite": "^6.0.7",
26-
"vitest": "^3.0.2"
27-
},
28-
"msw": {
29-
"workerDirectory": [
30-
"public"
31-
]
25+
"vitest": "^3.0.3"
3226
}
3327
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { page } from '@vitest/browser/context'
2+
import { render } from 'vitest-browser-react'
3+
import { DiscountCodeForm } from './discount-code-form.js'
4+
5+
test('renders the discount form', async () => {
6+
render(<DiscountCodeForm />)
7+
8+
const discountInput = page.getByLabelText('Discount code')
9+
await expect.element(discountInput).toBeVisible()
10+
11+
const applyDiscountButton = page.getByRole('button', {
12+
name: 'Apply discount',
13+
})
14+
await expect.element(applyDiscountButton).toBeVisible()
15+
})
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { useState, type FormEventHandler } from 'react'
2+
3+
export interface Discount {
4+
code: string
5+
amount: number
6+
}
7+
8+
export function DiscountCodeForm() {
9+
const [appliedDiscount, setAppliedDiscount] = useState<Discount>()
10+
11+
const handleSubmit: FormEventHandler<HTMLFormElement> = async (event) => {
12+
event.preventDefault()
13+
14+
const data = new FormData(event.currentTarget)
15+
const code = data.get('discountCode') as string
16+
17+
setAppliedDiscount({
18+
code,
19+
amount: 20,
20+
})
21+
}
22+
23+
return (
24+
<section className="w-96 rounded-lg border bg-white p-10">
25+
{appliedDiscount ? (
26+
<p>
27+
Discount: <strong>{appliedDiscount.code}</strong> (-
28+
{appliedDiscount.amount}%)
29+
</p>
30+
) : (
31+
<form onSubmit={handleSubmit} className="flex flex-col gap-5">
32+
<div>
33+
<label htmlFor="discountCode" className="mb-1 block">
34+
Discount code
35+
</label>
36+
<input
37+
id="discountCode"
38+
name="discountCode"
39+
className="w-full rounded-md border px-2 py-1 focus:ring-4"
40+
placeholder="ABCD1234"
41+
pattern="[A-Z]{4}[0-9]{4}"
42+
autoComplete="off"
43+
required
44+
/>
45+
</div>
46+
47+
<button
48+
type="submit"
49+
className="rounded-md bg-blue-600 px-4 py-2 text-white hover:bg-blue-500 focus:ring-4"
50+
>
51+
Apply discount
52+
</button>
53+
</form>
54+
)}
55+
</section>
56+
)
57+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { StrictMode } from 'react'
2+
import { createRoot } from 'react-dom/client'
3+
import './index.css'
4+
import { App } from './app.jsx'
5+
6+
createRoot(document.getElementById('root')!).render(
7+
<StrictMode>
8+
<App />
9+
</StrictMode>,
10+
)

0 commit comments

Comments
 (0)