Skip to content

Commit c3c2a9e

Browse files
refactor: improve middle popover tests
1 parent 0523043 commit c3c2a9e

File tree

2 files changed

+36
-50
lines changed

2 files changed

+36
-50
lines changed

packages/kit-headless/src/components/popover/popover.driver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect, type Locator, type Page } from '@playwright/test';
22

33
export type DriverLocator = Locator | Page;
44

5-
type OpenKeys = 'Enter' | 'Space';
5+
export type PopoverOpenKeys = 'Enter' | 'Space';
66

77
export function createTestDriver<T extends DriverLocator>(rootLocator: T) {
88
const getPopover = () => {
@@ -13,7 +13,7 @@ export function createTestDriver<T extends DriverLocator>(rootLocator: T) {
1313
return rootLocator.locator('[popovertarget]');
1414
};
1515

16-
const openPopover = async (key: OpenKeys | 'click', index?: number) => {
16+
const openPopover = async (key: PopoverOpenKeys | 'click', index?: number) => {
1717
const action = key === 'click' ? 'click' : 'press';
1818
const trigger = index !== undefined ? getTrigger().nth(index) : getTrigger();
1919
const popover = index !== undefined ? getPopover().nth(index) : getPopover();

packages/kit-headless/src/components/popover/popover.test.ts

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test, type Page } from '@playwright/test';
2-
import { createTestDriver } from './popover.driver';
2+
import { PopoverOpenKeys, createTestDriver } from './popover.driver';
33

44
async function setup(page: Page, exampleName: string) {
55
await page.goto(`/headless/popover/${exampleName}`);
@@ -184,44 +184,37 @@ test.describe('Mouse Behavior', () => {
184184
});
185185

186186
test.describe('Keyboard Behavior', () => {
187-
for (const key of ['Enter', 'Space']) {
188-
test(`GIVEN a closed hero popover
189-
WHEN focusing on the button and pressing the '${key}' key
190-
THEN the popover should open`, async ({ page }) => {
187+
for (const key of ['Enter', 'Space'] as PopoverOpenKeys[]) {
188+
test(`GIVEN a closed popover
189+
WHEN focusing on the button and pressing the '${key}' key
190+
THEN the popover should open`, async ({ page }) => {
191191
const { driver: d } = await setup(page, 'hero');
192192
await expect(d.getPopover()).toBeHidden();
193193

194194
await d.getTrigger().press(key);
195-
196195
await expect(d.getPopover()).toBeVisible();
197196
});
198197

199-
test(`GIVEN a open hero popover
200-
WHEN focusing on the button and pressing the '${key}' key
201-
THEN the popover should close`, async ({ page }) => {
198+
test(`GIVEN an open popover
199+
WHEN focusing on the button and pressing the '${key}' key
200+
THEN the popover should close`, async ({ page }) => {
202201
const { driver: d } = await setup(page, 'hero');
203202

204203
// Open the popover
205-
await d.getTrigger().press(key);
204+
await d.openPopover(key);
206205

207-
await expect(d.getPopover()).toBeVisible();
208-
209-
// Close the popover
210206
await d.getTrigger().press(key);
211-
212207
await expect(d.getPopover()).toBeHidden();
213208
});
214209
}
215210

216-
test(`GIVEN a open hero popover
217-
WHEN focusing on the button and pressing the 'Escape' key
218-
THEN the popover should close and the trigger be focused`, async ({ page }) => {
211+
test(`GIVEN an open popover
212+
WHEN focusing on the button and pressing the 'Escape' key
213+
THEN the popover should close and the trigger be focused`, async ({ page }) => {
219214
const { driver: d } = await setup(page, 'hero');
220215

221216
// Open the popover
222-
await d.getTrigger().press('Enter');
223-
224-
await expect(d.getPopover()).toBeVisible();
217+
await d.openPopover('Enter');
225218

226219
// Close the popover
227220
page.keyboard.press('Escape');
@@ -230,9 +223,9 @@ test.describe('Keyboard Behavior', () => {
230223
await expect(d.getTrigger()).toBeFocused();
231224
});
232225

233-
test(`GIVEN a programmatic popover with a programmatic trigger
234-
WHEN focusing on the button and pressing the 'o' key
235-
THEN the popover should open`, async ({ page }) => {
226+
test(`GIVEN a popover
227+
WHEN focusing on the button and pressing the 'o' key
228+
THEN the popover should be programmatically opened`, async ({ page }) => {
236229
const { driver: d } = await setup(page, 'programmatic');
237230

238231
await expect(d.getPopover()).toBeHidden();
@@ -249,44 +242,37 @@ test.describe('Keyboard Behavior', () => {
249242
await expect(d.getPopover()).toBeVisible();
250243
});
251244
test(`GIVEN an open auto popover
252-
WHEN the first trigger open and the focus changes to the second popover
253-
THEN the first popover should close and the second one appear`, async ({ page }) => {
245+
WHEN the first trigger opens
246+
AND the focus changes to the second popover
247+
THEN the first popover should close and the second one appear`, async ({
248+
page,
249+
}) => {
254250
const { driver: d } = await setup(page, 'auto');
255251

256-
const [firstPopOver, secondPopOver] = await d.getAllPopovers();
257-
const [firstPopoverTrigger, secondPopoverTrigger] = await d.getAllTriggers();
252+
const firstPopover = d.getPopover().nth(0);
253+
const secondPopover = d.getPopover().nth(1);
254+
const firstTrigger = d.getTrigger().nth(0);
255+
const secondTrigger = d.getTrigger().nth(1);
258256

259-
await expect(firstPopOver).toBeHidden();
260-
await expect(secondPopOver).toBeHidden();
257+
await expect(firstPopover).toBeHidden();
258+
await expect(secondPopover).toBeHidden();
261259

262-
await firstPopoverTrigger.press('Enter');
263-
await expect(firstPopOver).toBeVisible();
264-
await firstPopoverTrigger.press('Tab');
265-
await expect(secondPopoverTrigger).toBeFocused();
260+
await d.openPopover('Enter', 0);
261+
await firstTrigger.press('Tab');
266262

267-
await secondPopoverTrigger.press('Enter');
268-
await expect(secondPopOver).toBeVisible();
263+
await expect(secondTrigger).toBeFocused();
264+
await d.openPopover('Enter', 1);
269265

270-
await expect(firstPopOver).toBeHidden();
266+
await expect(firstPopover).toBeHidden();
271267
});
272268

273269
test(`GIVEN a pair of manual popovers
274270
WHEN clicking the first trigger on the page and then clicking the second trigger
275271
THEN then both popovers should be opened`, async ({ page }) => {
276272
const { driver: d } = await setup(page, 'manual');
277273

278-
const [firstPopOver, secondPopOver] = await d.getAllPopovers();
279-
const [firstPopoverTrigger, secondPopoverTrigger] = await d.getAllTriggers();
280-
281-
await expect(firstPopOver).toBeHidden();
282-
await expect(secondPopOver).toBeHidden();
283-
284-
await firstPopoverTrigger.press('Enter');
285-
286-
await secondPopoverTrigger.press('Enter');
287-
288-
await expect(firstPopOver).toBeVisible();
289-
await expect(secondPopOver).toBeVisible();
274+
await d.openPopover('click', 0);
275+
await d.openPopover('click', 1);
290276
});
291277

292278
test(`GIVEN a pair of manual opened popovers

0 commit comments

Comments
 (0)