85 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-10-27 19:40:17 -04:00
/* eslint-disable jest/no-standalone-expect, no-await-in-loop */
2022-10-27 17:01:12 -04:00
import type { Page } from '@playwright/test';
import { expect } from '@playwright/test';
2022-10-27 22:53:41 +11:00
import { toId } from '@storybook/csf';
2022-08-14 17:48:13 +08:00
export class SbPage {
readonly page: Page;
constructor(page: Page) {
this.page = page;
}
2022-10-27 22:53:41 +11:00
async openComponent(title: string, hasRoot = true) {
const parts = title.split('/');
for (let i = hasRoot ? 1 : 0; i < parts.length; i += 1) {
const parentId = toId(parts.slice(0, i + 1).join('/'));
const parentLink = this.page.locator(`#${parentId}`);
2022-08-14 17:48:13 +08:00
2022-10-27 22:53:41 +11:00
await expect(parentLink).toBeVisible();
if ((await parentLink.getAttribute('aria-expanded')) === 'false') {
await parentLink.click();
}
2022-08-14 17:48:13 +08:00
}
2022-10-27 22:53:41 +11:00
}
async navigateToStory(title: string, name: string) {
await this.openComponent(title);
2022-08-14 17:48:13 +08:00
2022-10-27 22:53:41 +11:00
const titleId = toId(title);
const storyId = toId(name);
2022-08-14 17:48:13 +08:00
const storyLinkId = `#${titleId}--${storyId}`;
await this.page.waitForSelector(storyLinkId);
const storyLink = this.page.locator(storyLinkId);
await storyLink.click({ force: true });
// assert url changes
const viewMode = name === 'docs' ? 'docs' : 'story';
const url = this.page.url();
await expect(url).toContain(`path=/${viewMode}/${titleId}--${storyId}`);
const selected = await storyLink.getAttribute('data-selected');
await expect(selected).toBe('true');
}
async waitUntilLoaded() {
const root = this.previewRoot();
const docsLoadingPage = root.locator('.sb-preparing-docs');
const storyLoadingPage = root.locator('.sb-preparing-story');
await docsLoadingPage.waitFor({ state: 'hidden' });
await storyLoadingPage.waitFor({ state: 'hidden' });
}
2022-08-14 17:48:13 +08:00
previewIframe() {
return this.page.frameLocator('#storybook-preview-iframe');
}
previewRoot() {
const preview = this.previewIframe();
2022-08-22 16:08:25 +02:00
return preview.locator('#storybook-root:visible, #storybook-docs:visible');
2022-08-14 17:48:13 +08:00
}
2022-08-14 19:20:33 +08:00
panelContent() {
return this.page.locator('#storybook-panel-root #panel-tab-content');
}
2022-08-14 17:48:13 +08:00
async viewAddonPanel(name: string) {
const tabs = await this.page.locator('[role=tablist] button[role=tab]');
const tab = tabs.locator(`text=/^${name}/`);
await tab.click();
}
async selectToolbar(toolbarSelector: string, itemSelector?: string) {
await this.page.locator(toolbarSelector).click();
if (itemSelector) {
await this.page.locator(itemSelector).click();
}
}
getCanvasBodyElement() {
return this.previewIframe().locator('body');
}
}