Skip to content

Commit 3f5a06b

Browse files
committed
test(popover): add more keyboard tests
1 parent 2ebf684 commit 3f5a06b

File tree

1 file changed

+79
-3
lines changed

1 file changed

+79
-3
lines changed

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

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,87 @@ test.describe('Mouse Behavior', () => {
154154

155155
expect((popoverBoundingBox?.y ?? 0) - triggerBottomAbsolutePosition).toBe(24);
156156
});
157+
158+
// test(`GIVEN a combobox with a flip configured
159+
// WHEN scrolling the page
160+
// THEN the popover flip to the opposite end once space runs out`, async ({ page }) => {
161+
// const { driver: d } = await setup(page, 'flip');
162+
163+
// const popover = d.getPopover();
164+
// const trigger = d.getTrigger();
165+
166+
// // Introduce artificial spacing
167+
// await trigger.evaluate((element) => (element.style.top = '800px'));
168+
169+
// await trigger.click();
170+
171+
// await expect(popover).toBeVisible();
172+
173+
// const popoverBoundingBox = await popover.boundingBox();
174+
// const triggerBoundingBox = await trigger.boundingBox();
175+
176+
// console.log(triggerBoundingBox, popoverBoundingBox);
177+
178+
// const triggerBottomAbsolutePosition =
179+
// (triggerBoundingBox?.y ?? 0) + (triggerBoundingBox?.height ?? 0);
180+
181+
// expect((popoverBoundingBox?.y ?? 0) - triggerBottomAbsolutePosition).toBe(24);
182+
// });
157183
});
158184

159185
test.describe('Keyboard Behavior', () => {
186+
for (const key of ['Enter', 'Space']) {
187+
test(`GIVEN a closed hero popover
188+
WHEN focusing on the button and pressing the '${key}' key
189+
THEN the popover should open`, async ({ page }) => {
190+
const { driver: d } = await setup(page, 'hero');
191+
await expect(d.getPopover()).toBeHidden();
192+
193+
await d.getTrigger().focus();
194+
await d.getTrigger().press(key);
195+
196+
await expect(d.getPopover()).toBeVisible();
197+
});
198+
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 }) => {
202+
const { driver: d } = await setup(page, 'hero');
203+
204+
// Open the popover
205+
await d.getTrigger().focus();
206+
await d.getTrigger().press(key);
207+
208+
await expect(d.getPopover()).toBeVisible();
209+
210+
await d.getTrigger().blur();
211+
212+
// Close the popover
213+
await d.getTrigger().focus();
214+
await d.getTrigger().press(key);
215+
216+
await expect(d.getPopover()).toBeHidden();
217+
});
218+
}
219+
220+
test(`GIVEN a open hero popover
221+
WHEN focusing on the button and pressing the 'Escape' key
222+
THEN the popover should close and the trigger be focused`, async ({ page }) => {
223+
const { driver: d } = await setup(page, 'hero');
224+
225+
// Open the popover
226+
await d.getTrigger().focus();
227+
await d.getTrigger().press('Enter');
228+
229+
await expect(d.getPopover()).toBeVisible();
230+
231+
// Close the popover
232+
page.keyboard.press('Escape');
233+
234+
await expect(d.getPopover()).toBeHidden();
235+
await expect(d.getTrigger()).toBeFocused();
236+
});
237+
160238
test(`GIVEN a programmatic popover with a programmatic trigger
161239
WHEN focusing on the button and pressing the 'o' key
162240
THEN the popover should open`, async ({ page }) => {
@@ -170,12 +248,11 @@ test.describe('Keyboard Behavior', () => {
170248

171249
await expect(d.getPopover()).toBeVisible();
172250
});
173-
174251
test(`GIVEN an open auto popover
175252
WHEN the first trigger open and the focus changes to the second popover
176253
THEN the first popover should close and the second one appear`, async ({ page }) => {
177254
const { driver: d } = await setup(page, 'auto');
178-
//ask shai: is it good to use nth here???
255+
179256
const [firstPopOver, secondPopOver] = await d.getAllPopovers();
180257
const [firstPopoverTrigger, secondPopoverTrigger] = await d.getAllTriggers();
181258

@@ -199,7 +276,6 @@ test.describe('Keyboard Behavior', () => {
199276
THEN then both popovers should be opened`, async ({ page }) => {
200277
const { driver: d } = await setup(page, 'manual');
201278

202-
//ask shai: is it good to use nth here???
203279
const [firstPopOver, secondPopOver] = await d.getAllPopovers();
204280
const [firstPopoverTrigger, secondPopoverTrigger] = await d.getAllTriggers();
205281

0 commit comments

Comments
 (0)