Skip to content
Open
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
12 changes: 12 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ jobs:
# The release notes page reads releases from the GitHub API, and the
# script fails in CI without a token.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: 🎭 Install Playwright Browsers
run: npx playwright install --with-deps chromium
# Runs against the site built above.
- name: 🎭 Run E2E Tests
run: npm run test:e2e
- name: 📤 Upload Playwright Report
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 14

cross-platform:
name: Cross-platform on ${{ matrix.os }}
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
.cspellcache
src/components/page/reference/ReleaseNotes/release-notes.json

# Playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/

# Misc
.DS_Store
.env.local
Expand Down
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ npm run build:preview

This builds the English site only, the same build as a Vercel preview.

### Browser Tests

[Playwright](https://playwright.dev/) tests in `e2e/` check the playgrounds and framework tabs in a browser. They run against the built site, so build it first:

```sh
npm run build:preview
npx playwright install chromium
npm run test:e2e
```

A failed run opens the HTML report. Run `npx playwright show-report` to open it after any run.

### Linting Documentation

This repository uses [ESLint](https://eslint.org/) to catch markup the build cannot check, such as raw links and headings and misaligned JSDoc comments, and [Prettier](https://prettier.io/), an opinionated code formatter, to keep consistent formatting throughout the documentation. Run the following command to report ESLint errors and automatically fix all formatting, and then push any changes:
Expand Down
186 changes: 186 additions & 0 deletions e2e/playground.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import { readFileSync } from 'node:fs';
import { expect, test, type Locator, type Page } from '@playwright/test';

/** The tab lists of the docs' framework tabs. Only their tabs control a panel, which tells them apart from a playground's. */
const docsFrameworkTabs = (page: Page) => page.locator('[role=tablist]:has([role=tab][aria-controls])');

const frameworkTab = (scope: Locator, name: string) =>
scope.getByRole('tablist', { name: 'Framework' }).getByRole('tab', { name, exact: true });

/**
* The modal page's "child state" playground, which has several files per framework and no JavaScript code.
*
* Its demo only loads once it scrolls into view, and its files change with the framework, so it is found
* by position rather than by what it contains.
*/
async function openChildStatePlayground(page: Page) {
await page.goto('api/modal');
const playgrounds = page.locator('.playground');
const childState = playgrounds.filter({ has: page.getByRole('tab', { name: 'src/app/child.component.ts' }) });
await expect(childState).toHaveCount(1);

const index = await playgrounds.evaluateAll((elements) =>
elements.findIndex((element) => element.textContent?.includes('src/app/child.component.ts'))
);
return playgrounds.nth(index);
}

/** The source of an example file, as written in its Markdown code fence. */
function readExampleFile(path: string) {
const markdown = readFileSync(`static/usage/v9/modal/can-dismiss/child-state/${path}`, 'utf8');
return markdown.replace(/^```\w*\n/, '').replace(/\n```\n*$/, '');
}

test.describe('framework choice', () => {
test('is shared between the docs tabs and playgrounds, and kept across reloads', async ({ page }) => {
await page.goto('api/alert');
const docsTabs = docsFrameworkTabs(page).first();
const playground = page.locator('.playground').first();

await docsTabs.getByRole('tab', { name: 'React', exact: true }).click();
await expect(frameworkTab(playground, 'React')).toHaveAttribute('aria-selected', 'true');

await frameworkTab(playground, 'Vue').click();
await expect(docsTabs.getByRole('tab', { name: 'Vue', exact: true })).toHaveAttribute('aria-selected', 'true');

await page.reload();
await expect(frameworkTab(playground, 'Vue')).toHaveAttribute('aria-selected', 'true');
await expect(docsTabs.getByRole('tab', { name: 'Vue', exact: true })).toHaveAttribute('aria-selected', 'true');
});

test('shows an Angular (Standalone) pick as Angular where there is no Standalone code', async ({ page }) => {
await page.addInitScript(() => localStorage.setItem('docusaurus.tab.framework', 'angular-standalone'));
await page.goto('api/alert');

await expect(frameworkTab(page.locator('.playground').first(), 'Angular')).toHaveAttribute('aria-selected', 'true');
});

test('disables a framework the playground has no code for, and skips it with the arrow keys', async ({ page }) => {
const playground = await openChildStatePlayground(page);

await expect(frameworkTab(playground, 'JavaScript')).toBeDisabled();

await frameworkTab(playground, 'Angular').focus();
await page.keyboard.press('ArrowRight');
await expect(frameworkTab(playground, 'React')).toBeFocused();

await page.keyboard.press('Enter');
await expect(frameworkTab(playground, 'React')).toHaveAttribute('aria-selected', 'true');
});
});

test.describe('file tabs', () => {
test('show one file at a time', async ({ page }) => {
const playground = await openChildStatePlayground(page);
const fileTabs = playground.locator('.playground__tabs');

await expect(fileTabs.getByRole('tab')).toHaveText([
'src/app/example.component.html',
'src/app/example.component.ts',
'src/app/child.component.html',
'src/app/child.component.ts',
]);
await expect(playground.locator('#playground_angular_src_app_example_component_html')).toBeVisible();

await fileTabs.getByRole('tab', { name: 'src/app/child.component.ts' }).click();
await expect(playground.locator('#playground_angular_src_app_child_component_ts')).toBeVisible();
await expect(playground.locator('#playground_angular_src_app_example_component_html')).toBeHidden();
});

test('select the first file after switching frameworks', async ({ page }) => {
const playground = await openChildStatePlayground(page);
const fileTabs = playground.locator('.playground__tabs');

await fileTabs.getByRole('tab', { name: 'src/app/child.component.ts' }).click();
await frameworkTab(playground, 'Vue').click();

await expect(fileTabs.getByRole('tab', { name: 'src/components/Example.vue' })).toHaveAttribute(
'aria-selected',
'true'
);
await expect(playground.locator('#playground_vue_src_components_Example_vue')).toBeVisible();
});

test('move focus with the arrow keys and select with Enter', async ({ page }) => {
const playground = await openChildStatePlayground(page);
const fileTabs = playground.locator('.playground__tabs');
const secondTab = fileTabs.getByRole('tab', { name: 'src/app/example.component.ts' });

await fileTabs.getByRole('tab', { name: 'src/app/example.component.html' }).focus();
await page.keyboard.press('ArrowRight');
await expect(secondTab).toBeFocused();
await expect(secondTab).toHaveAttribute('aria-selected', 'false');

await page.keyboard.press('Enter');
await expect(secondTab).toHaveAttribute('aria-selected', 'true');
});
});

test('StackBlitz receives every file of a multi-file example', async ({ page, context }) => {
// The SDK posts the project to StackBlitz in a new window. Answer it here, so the test stays offline.
const request = new Promise<URLSearchParams>((resolve) => {
context.route('https://stackblitz.com/**', (route) => {
resolve(new URLSearchParams(route.request().postData() ?? ''));
route.fulfill({ body: '' });
});
});

const playground = await openChildStatePlayground(page);
await expect(playground.locator('.playground__tabs')).toBeVisible();

// The StackBlitz button is the first icon button, and has no accessible name to find it by.
await playground.locator('.playground__control-group--end button').first().click();
const form = await request;

for (const [file, source] of [
['src/app/example.component.html', 'angular/example_component_html.md'],
['src/app/example.component.ts', 'angular/example_component_ts.md'],
['src/app/child.component.html', 'angular/child_component_html.md'],
['src/app/child.component.ts', 'angular/child_component_ts.md'],
]) {
// A form post sends every line break as CRLF.
expect(form.get(`project[files][${file}]`)?.replace(/\r\n/g, '\n'), file).toBe(readExampleFile(source));
}
});

test.describe('mode', () => {
test('switches the demo and is kept by every playground on the page', async ({ page }) => {
await page.goto('api/alert');
const playgrounds = page.locator('.playground');
const first = playgrounds.first();
const last = playgrounds.last();

await first.getByRole('button', { name: 'MD', exact: true }).click();
await expect(first.locator('iframe[src*="ionic:mode=md"]')).toBeVisible();
await expect(first.locator('iframe[src*="ionic:mode=ios"]')).toBeHidden();

// A playground out of view picks up the mode once it scrolls into view.
await last.scrollIntoViewIfNeeded();
await expect(last.locator('iframe[src*="ionic:mode=md"]')).toBeVisible();

await page.reload();
await first.scrollIntoViewIfNeeded();
await expect(first.locator('iframe[src*="ionic:mode=md"]')).toBeVisible();
});

test('disables the other mode for a playground restricted to one, and skips it with the Tab key', async ({
page,
}) => {
// A stored iOS pick must not override a playground that only runs in MD.
await page.addInitScript(() => localStorage.setItem('playground_mode', 'ios'));
await page.goto('api/input');

// The "fill" playground is the input page's only MD-only playground.
const playground = page
.locator('.playground')
.filter({ has: page.getByRole('button', { name: 'iOS', exact: true, disabled: true }) });
await playground.scrollIntoViewIfNeeded();

await expect(playground.locator('iframe[src*="ionic:mode=md"]')).toBeVisible();
await expect(playground.locator('iframe[src*="ionic:mode=ios"]')).toBeHidden();

await playground.getByRole('tablist', { name: 'Framework' }).getByRole('tab', { selected: true }).focus();
await page.keyboard.press('Tab');
await expect(playground.getByRole('button', { name: 'MD', exact: true })).toBeFocused();
});
});
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"prettier": "prettier \"./**/*.{html,ts,tsx,js,jsx,md,mdx}\" --cache",
"start": "docusaurus start",
"test": "vitest run",
"test:e2e": "playwright test",
"typecheck": "tsc --noEmit",
"swizzle": "docusaurus swizzle",
"spellcheck": "cspell --no-progress --cache --gitignore \"**/*.{md,mdx}\""
Expand Down Expand Up @@ -69,6 +70,7 @@
"@docusaurus/tsconfig": "^3.10.2",
"@docusaurus/types": "^3.10.2",
"@ionic/prettier-config": "^4.0.0",
"@playwright/test": "^1.65.1",
"@types/react": "^19.2.18",
"@typescript-eslint/parser": "^8.72.1",
"cspell": "^10.0.1",
Expand Down
36 changes: 36 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Browser tests for the site's interactive components, run against the built
* site. Run `npm run build:preview` first, then `npm run test:e2e`.
*
* Tests end in `.e2e.ts`, as in ionic-framework, because Vitest picks up every
* `.spec.ts` and `.test.ts`.
*/

import { defineConfig, devices } from '@playwright/test';

const baseURL = 'http://localhost:3100/docs/';

export default defineConfig({
testDir: './e2e',
testMatch: '*.e2e.ts',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
reporter: process.env.CI ? [['github'], ['html', { open: 'never' }]] : [['list'], ['html']],
use: {
baseURL,
trace: 'on-first-retry',
},
projects: [
{
name: 'Desktop Chrome',
use: { ...devices['Desktop Chrome'] },
},
],
webServer: {
command: 'npm run serve -- --port 3100 --no-open',
url: baseURL,
reuseExistingServer: !process.env.CI,
timeout: 120_000,
},
});
10 changes: 5 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"strict": true
},
/*
* `src` holds every TypeScript file in the project, and `index.d.ts` declares the
* `*.module.scss` imports those files rely on. Listing them explicitly also restores
* TypeScript's default excludes, notably `node_modules`, which the previous
* `exclude`-only config silently overrode.
* `src`, `e2e` and the Playwright config hold every TypeScript file in the project,
* and `index.d.ts` declares the `*.module.scss` imports those files rely on. Listing
* them explicitly also restores TypeScript's default excludes, notably `node_modules`,
* which the previous `exclude`-only config silently overrode.
*/
"include": ["src", "index.d.ts"]
"include": ["src", "e2e", "playwright.config.ts", "index.d.ts"]
}
Loading