Skip to content

Commit 0b4c7d5

Browse files
committed
tests: add first test
1 parent 41b4943 commit 0b4c7d5

File tree

8 files changed

+216
-0
lines changed

8 files changed

+216
-0
lines changed

.github/workflows/playwright.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Playwright Tests
2+
on:
3+
push:
4+
branches: [ main, master ]
5+
pull_request:
6+
branches: [ main, master ]
7+
jobs:
8+
test:
9+
timeout-minutes: 60
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: lts/*
16+
- name: Install dependencies
17+
run: npm install -g pnpm && pnpm install
18+
- name: Install Playwright Browsers
19+
run: pnpm exec playwright install --with-deps
20+
- name: Run Playwright tests
21+
run: pnpm exec playwright test
22+
- uses: actions/upload-artifact@v4
23+
if: always()
24+
with:
25+
name: playwright-report
26+
path: playwright-report/
27+
retention-days: 30

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ dist-ssr
2222
*.njsproj
2323
*.sln
2424
*.sw?
25+
/test-results/
26+
/playwright-report/
27+
/blob-report/
28+
/playwright/.cache/

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"zod": "^3.23.8"
2525
},
2626
"devDependencies": {
27+
"@playwright/test": "^1.45.1",
2728
"@types/node": "^20.14.10",
2829
"@types/react": "^18.3.3",
2930
"@types/react-dom": "^18.3.0",

playwright.config.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { defineConfig, devices } from '@playwright/test';
2+
3+
/**
4+
* Read environment variables from file.
5+
* https://github.com/motdotla/dotenv
6+
*/
7+
// import dotenv from 'dotenv';
8+
// dotenv.config({ path: path.resolve(__dirname, '.env') });
9+
10+
/**
11+
* See https://playwright.dev/docs/test-configuration.
12+
*/
13+
export default defineConfig({
14+
testDir: './tests',
15+
/* Run tests in files in parallel */
16+
fullyParallel: true,
17+
/* Fail the build on CI if you accidentally left test.only in the source code. */
18+
forbidOnly: !!process.env.CI,
19+
/* Retry on CI only */
20+
retries: process.env.CI ? 2 : 0,
21+
/* Opt out of parallel tests on CI. */
22+
workers: process.env.CI ? 1 : undefined,
23+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
24+
reporter: 'html',
25+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
26+
use: {
27+
/* Base URL to use in actions like `await page.goto('/')`. */
28+
// baseURL: 'http://127.0.0.1:3000',
29+
30+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
31+
trace: 'on-first-retry',
32+
},
33+
34+
/* Configure projects for major browsers */
35+
projects: [
36+
{
37+
name: 'chromium',
38+
use: { ...devices['Desktop Chrome'] },
39+
},
40+
41+
{
42+
name: 'firefox',
43+
use: { ...devices['Desktop Firefox'] },
44+
},
45+
46+
{
47+
name: 'webkit',
48+
use: { ...devices['Desktop Safari'] },
49+
},
50+
51+
/* Test against mobile viewports. */
52+
// {
53+
// name: 'Mobile Chrome',
54+
// use: { ...devices['Pixel 5'] },
55+
// },
56+
// {
57+
// name: 'Mobile Safari',
58+
// use: { ...devices['iPhone 12'] },
59+
// },
60+
61+
/* Test against branded browsers. */
62+
// {
63+
// name: 'Microsoft Edge',
64+
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
65+
// },
66+
// {
67+
// name: 'Google Chrome',
68+
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
69+
// },
70+
],
71+
72+
/* Run your local dev server before starting the tests */
73+
// webServer: {
74+
// command: 'npm run start',
75+
// url: 'http://127.0.0.1:3000',
76+
// reuseExistingServer: !process.env.CI,
77+
// },
78+
});

pnpm-lock.yaml

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

src/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function App() {
8383
<FancySwitch
8484
{...field}
8585
options={orderTypes}
86+
data-testid="orderType"
8687
className="flex rounded-full bg-muted p-2"
8788
highlighterClassName="bg-primary rounded-full"
8889
radioClassName={cn(

src/components/fancy-switch/fancy-switch.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ const FancySwitch = React.forwardRef<HTMLDivElement, FancySwitchProps>(
174174
...highlighterStyle
175175
}}
176176
aria-hidden="true"
177+
data-highlighter
177178
/>
178179

179180
{memoizedOptions.map((option, index) => (

tests/first.spec.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { test, expect } from '@playwright/test'
2+
3+
test.describe('FancySwitch Component', () => {
4+
test.beforeEach(async ({ page }) => {
5+
await page.goto('http://localhost:5173')
6+
})
7+
8+
test('renders options', async ({ page }) => {
9+
const radiogroups = await page.getByTestId('orderType')
10+
const options = await radiogroups.getByRole('radio').all()
11+
12+
await expect(options).toHaveLength(3)
13+
await expect(options[0]).toHaveText('Delivery')
14+
await expect(options[1]).toHaveText('Pickup')
15+
await expect(options[2]).toHaveText('Shipping')
16+
})
17+
18+
test('selects option on click', async ({ page }) => {
19+
const radiogroups = await page.getByTestId('orderType')
20+
const secondOption = await radiogroups.getByRole('radio').nth(1)
21+
await secondOption.click()
22+
await expect(secondOption).toHaveAttribute('aria-checked', 'true')
23+
})
24+
25+
test('changes selection with arrow keys', async ({ page }) => {
26+
const firstOption = await page.locator('[role="radio"]').nth(0)
27+
await firstOption.focus()
28+
await page.keyboard.press('ArrowRight')
29+
30+
const secondOption = await page.locator('[role="radio"]').nth(1)
31+
await expect(secondOption).toHaveAttribute('aria-checked', 'true')
32+
})
33+
34+
test('highlighter moves to selected option', async ({ page }) => {
35+
const radiogroups = await page.getByTestId('orderType')
36+
const secondOption = await radiogroups.getByRole('radio').nth(1)
37+
await secondOption.click()
38+
39+
const highlighter = await radiogroups.locator('[data-highlighter]')
40+
const optionBox = await secondOption.boundingBox()
41+
42+
// get second option margin
43+
const margin = await secondOption.evaluate((el) => {
44+
const { marginTop, marginBottom, marginLeft, marginRight } =
45+
getComputedStyle(el)
46+
return {
47+
marginTop,
48+
marginBottom,
49+
marginLeft,
50+
marginRight
51+
}
52+
})
53+
54+
// wait for the highlighter to move
55+
await page.waitForTimeout(500)
56+
57+
const newHighlighterBox = await highlighter.boundingBox()
58+
if (newHighlighterBox && optionBox) {
59+
expect(newHighlighterBox.width).toBeCloseTo(
60+
optionBox.width +
61+
parseInt(margin.marginLeft) +
62+
parseInt(margin.marginRight)
63+
)
64+
}
65+
})
66+
})

0 commit comments

Comments
 (0)