-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathget-all-stories.test.ts
60 lines (51 loc) · 2 KB
/
get-all-stories.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { test } from '@playwright/test';
import fs from 'fs';
import { storybookTest } from './storybook-fixture';
storybookTest.describe('Get a list of all stories', () => {
storybookTest('Get a list of all stories', async ({ page, baseURL }) => {
await page.goto(baseURL!);
const componentList: Record<string, string[]> = {};
const expandAllButton = page.locator("button[data-action='expand-all']");
await expandAllButton.click();
const tree = page.locator('#storybook-explorer-tree');
const componentsLocator = tree.locator('[data-nodetype="component"]');
const componentNames = await componentsLocator.allTextContents();
for (const componentName of componentNames) {
componentList[componentName] = [];
const componentLocator = componentsLocator.filter({
hasText: componentName,
});
const dataItemId = await componentLocator.getAttribute('data-item-id');
const stories = tree.locator(
`//*[@data-nodetype="story" and @data-parent-id="${dataItemId}"]`,
);
const count = await stories.count();
for (let i = 0; i < count; ++i) {
componentList[componentName].push(
(await stories.nth(i).getAttribute('data-item-id'))!,
);
}
}
const pathForpageList = `resources`;
!fs.existsSync(pathForpageList) &&
fs.mkdir(`resources`, (err) => {
if (err) throw err;
});
const filePath = `./storybook/componentList.json`;
if (process.env.CI) {
const contents = fs.readFileSync(filePath).toString('ascii');
if (
JSON.stringify(componentList) !== JSON.stringify(JSON.parse(contents))
) {
throw new Error(
`Contents of componentList.json is outdated. Please run this test locally and commit the changes to the file. Current component list: ${JSON.stringify(
componentList,
)}`,
);
}
}
fs.writeFile(filePath, JSON.stringify(componentList), function (err) {
if (err) throw err;
});
});
});