Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, test } from "vitest";
import {
abilityTrigger,
championTrigger,
gameFactAttributes,
parseGameFactTrigger,
} from "#src/game-facts/trigger.ts";

describe("game fact triggers", () => {
test("a champion trigger carries the Data Dragon key and the display name", () => {
expect(championTrigger(103)).toEqual({
kind: "champion",
key: "Ahri",
name: "Ahri",
});
expect(championTrigger(62)).toEqual({
kind: "champion",
key: "MonkeyKing",
name: "Wukong",
});
});

test("an ability trigger is named like its fact file", () => {
expect(abilityTrigger(103, "E", "Charm")).toEqual({
kind: "ability",
key: "Ahri-E",
name: "Charm",
});
});

test("attributes round-trip through the parser", () => {
const trigger = { kind: "item", key: "3118", name: "Malignance" } as const;
const attributes = gameFactAttributes(trigger);
expect(attributes).toEqual({
"data-game-fact": "item/3118",
"data-game-fact-name": "Malignance",
});
expect(
parseGameFactTrigger(
attributes["data-game-fact"] ?? "",
attributes["data-game-fact-name"] ?? null,
),
).toEqual(trigger);
});

test("no trigger adds no attributes", () => {
expect(gameFactAttributes(undefined)).toEqual({});
});

test("a malformed trigger throws", () => {
expect(() => parseGameFactTrigger("item", "Malignance")).toThrow();
expect(() => parseGameFactTrigger("item/", "Malignance")).toThrow();
expect(() => parseGameFactTrigger("potion/1", "Potion")).toThrow();
expect(() => parseGameFactTrigger("item/3118", null)).toThrow();
});
});
83 changes: 83 additions & 0 deletions packages/scout-for-lol/packages/data/src/game-facts/trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
getBrowserChampionName,
normalizeBrowserChampionKey,
} from "#src/browser-assets.ts";
import {
GameFactKindSchema,
type GameFactKind,
} from "#src/game-facts/schema.ts";

/**
* How a server-rendered page marks an element as a tooltip trigger. The page
* renders `data-game-fact="<kind>/<key>"` with the entity's name beside it,
* and one delegated script opens the entity's tooltip (the file at
* `/assets/scout/game/<patch>/facts/<kind>/<key>.json`) on hover, focus or
* tap. The key is the one the fact file is named by: a champion key (`Ahri`),
* an ability (`Ahri-E`) or a numeric id for items, runes and summoner spells.
*/

export const GAME_FACT_ATTRIBUTE = "data-game-fact";
export const GAME_FACT_NAME_ATTRIBUTE = "data-game-fact-name";

/** One game entity a page marks as a tooltip trigger. */
export type GameFactTrigger = {
kind: GameFactKind;
key: string;
/** The entity's name, which names the tooltip for assistive technology. */
name: string;
};

export type GameFactTriggerAttributes = {
"data-game-fact": string;
"data-game-fact-name": string;
};

/** The attributes to put on the trigger element; nothing when there is none. */
export function gameFactAttributes(
trigger: GameFactTrigger | undefined,
): Partial<GameFactTriggerAttributes> {
if (trigger === undefined) return {};
return {
[GAME_FACT_ATTRIBUTE]: `${trigger.kind}/${trigger.key}`,
[GAME_FACT_NAME_ATTRIBUTE]: trigger.name,
};
}

/** Reads a trigger element's two attributes back; throws on a malformed pair. */
export function parseGameFactTrigger(
fact: string,
name: string | null,
): GameFactTrigger {
const separator = fact.indexOf("/");
const key = fact.slice(separator + 1);
if (separator === -1 || key === "" || name === null || name === "") {
throw new Error(`Malformed game fact trigger ${fact} (${String(name)})`);
}
return {
kind: GameFactKindSchema.parse(fact.slice(0, separator)),
key,
name,
};
}

/** A champion by its numeric id (as match data carries it) or Data Dragon key. */
export function championTrigger(champion: string | number): GameFactTrigger {
return {
kind: "champion",
key: normalizeBrowserChampionKey(champion),
name: getBrowserChampionName(champion),
};
}

/** An ability by champion id and slot letter (`Q`, `W`, `E`, `R`, `passive`). */
export function abilityTrigger(
championId: number,
slot: string,
name: string,
): GameFactTrigger {
return {
kind: "ability",
key: `${normalizeBrowserChampionKey(championId)}-${slot}`,
name,
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, type Page } from "@playwright/test";
import { assertSemanticForms } from "#src/semantic-form-checks.ts";
import { assertTooltipTriggers } from "#src/tooltip-triggers.ts";

/**
* `role="dialog"` covers two different widgets, and only one of them is modal.
Expand Down Expand Up @@ -60,7 +61,7 @@ async function assertVisibleDialogs(page: Page): Promise<void> {

export async function assertInteractiveStates(page: Page): Promise<void> {
const dialogTriggers = page.locator(
'[aria-haspopup="dialog"]:visible:not([aria-label="Open navigation"]), [data-dialog-trigger]:visible',
'[aria-haspopup="dialog"]:visible:not([aria-label="Open navigation"]):not([data-game-fact]), [data-dialog-trigger]:visible',
);
const dialogTriggerCount = await dialogTriggers.count();
for (let index = 0; index < dialogTriggerCount; index += 1) {
Expand Down Expand Up @@ -88,6 +89,7 @@ export async function assertInteractiveStates(page: Page): Promise<void> {
}
}

await assertTooltipTriggers(page);
await assertVisibleDialogs(page);

const tabLists = page.locator('[role="tablist"]:visible');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from "vitest";
import { tooltipTriggerSamples } from "./tooltip-triggers.ts";

test("one sample per kind of tooltip, the first in page order", () => {
expect(
tooltipTriggerSamples([
"rune/8112",
"summoner-spell/4",
"rune/8126",
"item/3118",
"summoner-spell/14",
"champion/Ahri",
"item/3157",
]),
).toEqual(["rune/8112", "summoner-spell/4", "item/3118", "champion/Ahri"]);
});

test("a page with no triggers has no samples", () => {
expect(tooltipTriggerSamples([])).toEqual([]);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { expect, type Page } from "@playwright/test";

/**
* Elements a page marks with `data-game-fact="<kind>/<key>"` open one shared
* tooltip. That tooltip moves from element to element, so the element holding
* `aria-haspopup`, `aria-expanded` and `aria-controls` changes as the reader
* does, and a sweep that counts them once and revisits them by index loses
* its place. These triggers are audited on their own, each found again by its
* own `data-game-fact` value on every visit.
*/

const TRIGGER = "[data-game-fact]";

/** The first fact of each kind, in page order: one sample per kind of tooltip. */
export function tooltipTriggerSamples(facts: readonly string[]): string[] {
const samples = new Map<string, string>();
for (const fact of facts) {
const kind = fact.split("/")[0];
if (kind !== undefined && !samples.has(kind)) samples.set(kind, fact);
}
return [...samples.values()];
}

/** The tooltips hydrate with the page; hovering before that opens nothing. */
async function waitForHydration(page: Page): Promise<void> {
await page.waitForFunction(
() => document.querySelector("astro-island[ssr]") === null,
);
}

/** A phone has no hover: a tap opens the layer, or a bottom sheet. */
function isPhone(page: Page): boolean {
const viewport = page.viewportSize();
return viewport !== null && viewport.width < 640;
}

/**
* Every kind of tooltip trigger on the page opens a named dialog when reached
* the way its reader reaches it, announces it, and lets go of it on Escape.
*/
export async function assertTooltipTriggers(page: Page): Promise<void> {
const facts = await page
.locator(`${TRIGGER}:visible`)
.evaluateAll((elements) =>
elements.map((element) =>
element instanceof HTMLElement
? (element.dataset["gameFact"] ?? "")
: "",
),
);
if (facts.length === 0) return;
await waitForHydration(page);

for (const fact of tooltipTriggerSamples(facts)) {
const trigger = page.locator(`[data-game-fact="${fact}"]:visible`).first();
const name = await trigger.evaluate((element) =>
element instanceof HTMLElement
? (element.dataset["gameFactName"] ?? null)
: null,
);
expect(name, `${fact} names its tooltip`).not.toBeNull();
await trigger.scrollIntoViewIfNeeded();
await page.mouse.move(0, 0);
if (isPhone(page)) {
await trigger.tap();
} else {
await trigger.hover();
}
const dialog = page.getByRole("dialog", { name: name ?? "", exact: true });
await expect(
dialog,
`${fact} opens a dialog named ${name ?? ""}`,
).toBeVisible();
await expect(trigger).toHaveAttribute("aria-haspopup", "dialog");
await page.keyboard.press("Escape");
await expect(page.locator('[role="dialog"]:visible')).toHaveCount(0);
await page.mouse.move(0, 0);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions packages/scout-for-lol/packages/design-system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ restoration, and native form validation semantics. They target specific
stories by id, so renaming those stories or their exports breaks the specs —
`Runtime/ThemeMenu` → `Default`, `Components/Overlays/Dialog` → `CreateReport`,
and `Components/Forms/Field` → `SemanticFormStates` are load-bearing names.
`e2e/rich-tooltip.spec.ts` drives `Domain/EntityTooltip` → `Interactive` and
`ThreeLayers` the same way.
`e2e/rich-tooltip.spec.ts` drives `Domain/EntityTooltip` → `Interactive`,
`ThreeLayers` and `Delegated` the same way.

The parts both Scout catalogs share — the story-index loader, the page-level
axe exclusions, the mount assertions, and the CI reporter shape — live in
Expand Down Expand Up @@ -143,6 +143,16 @@ request when the layer closes. The facts' types come from
`@scout-for-lol/data/game-facts/schema.ts`, and the stories load the generated
fact JSON itself (`entity-tooltip.facts.ts`), never retyped values.

A server-rendered page that has no React tree around its icons marks each one
with `data-game-fact="<kind>/<key>"` and `data-game-fact-name` (the
`gameFactAttributes` helper in `@scout-for-lol/data/game-facts/trigger.ts`) and
mounts one `DelegatedGameFactTooltips` island inside a `GameFactProvider`. The
island listens on the document for the pointer or focus reaching a marked
element and gives it a `RichTooltip` through `triggerElement`, so the page pays
for one island, not one per icon. It renders no Scout block. The public
champion and matchup pages use it; `Domain/EntityTooltip` → `Delegated` is its
story.

## Tokens

`tokens/themes.json` is the source of truth. `bun run generate` writes
Expand Down
Loading