-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cd5dbc
commit 630f722
Showing
54 changed files
with
861 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,60 @@ | ||
import { expect, type Page, test } from "@playwright/test" | ||
import { controls, part } from "./_utils" | ||
import { test } from "@playwright/test" | ||
import { MenuModel } from "./models/menu.model" | ||
|
||
const trigger = part("trigger") | ||
const menu = part("content") | ||
|
||
const expectToBeFocused = async (page: Page, id: string) => { | ||
return expect(page.locator(`[id=${id}]`).first()).toHaveAttribute("data-highlighted", "") | ||
} | ||
let I: MenuModel | ||
|
||
test.describe("menu", () => { | ||
test.beforeEach(async ({ page }) => { | ||
await page.goto("/menu") | ||
I = new MenuModel(page) | ||
await I.goto() | ||
}) | ||
|
||
test("should have no accessibility violation", async () => { | ||
await I.checkAccessibility() | ||
}) | ||
|
||
test("on arrow up and down, change highlighted item", async () => { | ||
await I.clickTrigger() | ||
await I.pressKey("ArrowDown", 2) | ||
await I.seeItemIsHighlighted("Duplicate") | ||
await I.pressKey("ArrowUp") | ||
await I.seeItemIsHighlighted("Edit") | ||
}) | ||
|
||
test("on typeahead, highlight matching item", async () => { | ||
await I.clickTrigger() | ||
await I.type("E") | ||
await I.seeItemIsHighlighted("Edit") | ||
await I.type("E") | ||
await I.seeItemIsHighlighted("Export") | ||
}) | ||
|
||
test("when closeOnSelect=false, stay open on selection", async () => { | ||
await I.controls.bool("closeOnSelect", false) | ||
await I.clickTrigger() | ||
await I.pressKey("ArrowDown") | ||
await I.pressKey("Enter") | ||
await I.seeDropdown() | ||
}) | ||
|
||
test("hover out, clear highlighted item", async () => { | ||
await I.clickViz() | ||
await I.clickTrigger() | ||
await I.hoverItem("Delete") | ||
await I.hoverOut() | ||
await I.dontSeeHighlightedItem() | ||
}) | ||
|
||
test("should stay open when `closeOnSelect` is false", async ({ page }) => { | ||
await controls(page).bool("closeOnSelect", false) | ||
await page.click(trigger) | ||
await page.keyboard.press("ArrowDown") | ||
await page.keyboard.press("Enter", { delay: 10 }) | ||
await expect(page.locator(menu)).toBeVisible() | ||
test("with keyboard, can select item", async () => { | ||
await I.clickTrigger() | ||
await I.pressKey("ArrowDown") | ||
await I.pressKey("Enter") | ||
await I.dontSeeDropdown() | ||
}) | ||
|
||
test("should navigate menu items with tab", async ({ page }) => { | ||
await page.click(trigger) | ||
await page.keyboard.press("Tab") | ||
await page.keyboard.press("Tab") | ||
await expectToBeFocused(page, "duplicate") | ||
await page.keyboard.press("Tab") | ||
await page.keyboard.press("Shift+Tab") | ||
await expectToBeFocused(page, "duplicate") | ||
test("on click outside, close menu", async () => { | ||
await I.clickTrigger() | ||
await I.clickOutside() | ||
await I.dontSeeDropdown() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { expect, type Page } from "@playwright/test" | ||
import { a11y, isInViewport } from "../_utils" | ||
import { Model } from "./model" | ||
|
||
export class MenuModel extends Model { | ||
constructor(public page: Page) { | ||
super(page) | ||
} | ||
|
||
checkAccessibility() { | ||
return a11y(this.page, "main") | ||
} | ||
|
||
goto(url = "/menu") { | ||
return this.page.goto(url) | ||
} | ||
|
||
private get trigger() { | ||
return this.page.locator("[data-scope=menu][data-part=trigger]") | ||
} | ||
|
||
private get content() { | ||
return this.page.locator("[data-scope=menu][data-part=content]") | ||
} | ||
|
||
getItem = (text: string) => { | ||
return this.page.locator(`[data-part=item]`, { hasText: text }) | ||
} | ||
|
||
get highlightedItem() { | ||
return this.page.locator("[data-part=item][data-highlighted]") | ||
} | ||
|
||
type(input: string) { | ||
return this.content.pressSequentially(input) | ||
} | ||
|
||
clickTrigger = async () => { | ||
await this.trigger.click() | ||
} | ||
|
||
hoverItem = async (text: string) => { | ||
await this.getItem(text).hover() | ||
} | ||
|
||
hoverOut = async () => { | ||
await this.page.mouse.move(0, 0) | ||
} | ||
|
||
seeDropdown = async () => { | ||
await expect(this.content).toBeVisible() | ||
} | ||
|
||
dontSeeDropdown = async () => { | ||
await expect(this.content).not.toBeVisible() | ||
} | ||
|
||
seeItemIsHighlighted = async (text: string) => { | ||
const item = this.getItem(text) | ||
await expect(item).toHaveAttribute("data-highlighted", "") | ||
} | ||
|
||
dontSeeHighlightedItem = async () => { | ||
await expect(this.highlightedItem).not.toBeVisible() | ||
} | ||
|
||
seeItemIsChecked = async (text: string) => { | ||
const item = this.getItem(text) | ||
await expect(item).toHaveAttribute("data-state", "checked") | ||
} | ||
|
||
seeItemIsNotChecked = async (text: string) => { | ||
const item = this.getItem(text) | ||
await expect(item).not.toHaveAttribute("data-state", "checked") | ||
} | ||
|
||
seeItemInViewport = async (text: string) => { | ||
const item = this.getItem(text) | ||
expect(await isInViewport(this.content, item)).toBe(true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useCallback, useRef } from "react" | ||
|
||
type AnyFunction = (...args: any[]) => any | ||
|
||
export function useEvent<T extends AnyFunction>(callback: T | undefined): T { | ||
const ref = useRef(callback) | ||
ref.current = callback | ||
return useCallback((...args: any[]) => ref.current?.(...args), []) as T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.