Skip to content

Commit ace0d26

Browse files
test(select): show docs select tests
1 parent abc767a commit ace0d26

File tree

8 files changed

+107
-92
lines changed

8 files changed

+107
-92
lines changed

apps/website/e2e/select.spec.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.

apps/website/playwright.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const baseURL = process.env['BASE_URL'] || 'http://localhost:5173';
1616
* See https://playwright.dev/docs/test-configuration.
1717
*/
1818
export default defineConfig({
19-
...nxE2EPreset(__filename, { testDir: './e2e' }),
19+
...nxE2EPreset(__filename, { testDir: './src' }),
2020
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
2121
use: {
2222
baseURL,

apps/website/src/routes/docs/headless/contributing/index.mdx

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Currently, the component testing integration for Qwik & Playwright is in develop
110110

111111
#### Getting started w/ testing
112112

113-
Here's an example way of getting a testid of the `Hero` select docs example, without affecting any visible markup.
113+
Here's an example way of getting a testid of the `Hero` select docs example in `index.mdx`, without affecting any visible markup.
114114

115115
```tsx
116116
<div data-testid="select-hero-test">
@@ -121,6 +121,10 @@ Here's an example way of getting a testid of the `Hero` select docs example, wit
121121
Then, we get our testDriver, you can think of it as reusable code we use throughout the test. For example, in the Select component we constantly grab the listbox, trigger, etc.
122122

123123
```tsx
124+
import { Locator, Page } from '@playwright/test';
125+
126+
export type DriverLocator = Locator | Page;
127+
124128
export function selectTestDriver<T extends DriverLocator>(locator: T) {
125129
const getRoot = () => {
126130
return locator.getByRole('combobox');
@@ -136,49 +140,64 @@ export function selectTestDriver<T extends DriverLocator>(locator: T) {
136140
getTrigger() {
137141
return getRoot().getByRole('button');
138142
},
143+
// get all options
144+
getOptions() {
145+
return getRoot().getByRole('option').all();
146+
},
139147
};
140148
}
141149
```
142150

143151
Now we can write some tests:
144152

145153
```tsx
146-
test(`GIVEN a basic select
147-
WHEN clicking on the trigger
148-
THEN open up the listbox`, async ({ page }) => {
154+
import { test, expect } from '@playwright/test';
155+
import { selectTestDriver } from './select.driver';
156+
157+
test.beforeEach(async ({ page }) => {
149158
await page.goto('/docs/headless/select');
159+
});
150160

151-
const testDriver = selectTestDriver(page.getByTestId('select-hero-test'));
161+
test.describe('critical functionality', () => {
162+
test(`GIVEN a basic select
163+
WHEN clicking on the trigger
164+
THEN open up the listbox
165+
AND aria-expanded should be true`, async ({ page }) => {
166+
const testDriver = selectTestDriver(page.getByTestId('select-hero-test'));
152167

153-
const { getListbox, getTrigger } = testDriver;
168+
const { getListbox, getTrigger } = testDriver;
154169

155-
await getTrigger().click();
170+
await getTrigger().click();
156171

157-
await expect(getListbox()).toBeVisible();
158-
});
159-
160-
test(`GIVEN a basic select
161-
WHEN focusing the trigger and hitting enter
162-
THEN open up the listbox`, async ({ page }) => {
163-
await page.goto('/docs/headless/select');
172+
await expect(getListbox()).toBeVisible();
173+
await expect(getTrigger()).toHaveAttribute('aria-expanded', 'true');
174+
});
164175

165-
const testDriver = selectTestDriver(page.getByTestId('select-hero-test'));
176+
test(`GIVEN a basic select
177+
WHEN focusing the trigger and hitting enter
178+
THEN open up the listbox
179+
AND aria-expanded should be true`, async ({ page }) => {
180+
const testDriver = selectTestDriver(page.getByTestId('select-hero-test'));
166181

167-
const { getListbox, getTrigger } = testDriver;
182+
const { getListbox, getTrigger } = testDriver;
168183

169-
await getTrigger().focus();
170-
await page.keyboard.press('Enter');
184+
await getTrigger().focus();
185+
await page.keyboard.press('Enter');
171186

172-
await expect(getListbox()).toBeVisible();
187+
await expect(getListbox()).toBeVisible();
188+
await expect(getTrigger()).toHaveAttribute('aria-expanded', 'true');
189+
});
173190
});
174191
```
175192

176-
To run the tests, use the `nx e2e website` command. You can also do `--ui` to open the UI mode (which is pretty awesome!)
193+
To run the tests, use the `pnpm playwright` (only in my branch) command, or the `nx e2e website` longform. You can also do `--ui` to open the UI mode (which is pretty awesome!)
177194

178-
> This example is in `spec.select.tsx` in the e2e folder of the website.
195+
> This example is in `spec.select.tsx` in the `src/components/select` folder of the website.
179196
180197
Once we've added a failing test with what we expect, we can now go ahead and implement the feature for that!
181198

199+
Heads up, I (Jack) am also relatively new to playwright myself 😅. I'm guessing there is a way to not need the testDriver to be consumed on each test. Feel free to experiment!
200+
182201
## Docs
183202

184203
We use [MDX](https://mdxjs.com/docs/what-is-mdx/) for interactive markdown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Locator, Page } from '@playwright/test';
2+
3+
export type DriverLocator = Locator | Page;
4+
5+
export function selectTestDriver<T extends DriverLocator>(locator: T) {
6+
const getRoot = () => {
7+
return locator.getByRole('combobox');
8+
};
9+
10+
return {
11+
...locator,
12+
locator,
13+
getRoot,
14+
getListbox() {
15+
return getRoot().getByRole('listbox');
16+
},
17+
getTrigger() {
18+
return getRoot().getByRole('button');
19+
},
20+
// get all options
21+
getOptions() {
22+
return getRoot().getByRole('option').all();
23+
},
24+
};
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { test, expect } from '@playwright/test';
2+
import { selectTestDriver } from './select.driver';
3+
4+
test.beforeEach(async ({ page }) => {
5+
await page.goto('/docs/headless/select');
6+
});
7+
8+
test.describe('critical functionality', () => {
9+
test(`GIVEN a basic select
10+
WHEN clicking on the trigger
11+
THEN open up the listbox
12+
AND aria-expanded should be true`, async ({ page }) => {
13+
const testDriver = selectTestDriver(page.getByTestId('select-hero-test'));
14+
15+
const { getListbox, getTrigger } = testDriver;
16+
17+
await getTrigger().click();
18+
19+
await expect(getListbox()).toBeVisible();
20+
await expect(getTrigger()).toHaveAttribute('aria-expanded', 'true');
21+
});
22+
23+
test(`GIVEN a basic select
24+
WHEN focusing the trigger and hitting enter
25+
THEN open up the listbox
26+
AND aria-expanded should be true`, async ({ page }) => {
27+
const testDriver = selectTestDriver(page.getByTestId('select-hero-test'));
28+
29+
const { getListbox, getTrigger } = testDriver;
30+
31+
await getTrigger().focus();
32+
await page.keyboard.press('Enter');
33+
34+
await expect(getListbox()).toBeVisible();
35+
await expect(getTrigger()).toHaveAttribute('aria-expanded', 'true');
36+
});
37+
});

apps/website/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
"path": "./tsconfig.spec.json"
2727
}
2828
],
29-
"include": ["src", "e2e/select.spec.ts"]
29+
"include": ["src", "src/routes/docs/headless/select/select.spec.ts"]
3030
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"changeset": "changeset",
1616
"changeset.version": "changeset version && pnpm install --no-frozen-lockfile && git add --all",
1717
"dev": "nx serve website",
18+
"playwright": "nx e2e website",
1819
"format.fix": "pretty-quick --staged",
1920
"link.dist": "cd dist/packages/kit-headless && pnpm link --global",
2021
"lint": "nx affected:lint",

packages/kit-headless/src/components/select/notes.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ What do they all have in common? How do people use them? What are the most impor
4040

4141
### State
4242

43-
name: bind:selected
43+
name: bind:value
4444
type: Signal
4545
description: controlled selected value, manages the selected option.
4646

47-
name: defaultSelected
48-
type: boolean
49-
description: uncontrolled selected value, sets the initial selected option.
50-
51-
name: onSelectedChange$
47+
name: onChange$
5248
type: PropFunction
5349
description: function called when the selected value changes.
5450

0 commit comments

Comments
 (0)