@argos-ci/storybook 2.0.1 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -2,6 +2,19 @@ import { TestContext } from '@storybook/test-runner';
2
2
  import { ArgosScreenshotOptions as ArgosScreenshotOptions$1 } from '@argos-ci/playwright';
3
3
  import { Page } from 'playwright';
4
4
 
5
+ /**
6
+ * Storybook mode.
7
+ */
8
+ type StorybookGlobals = Record<string, any>;
9
+ /**
10
+ * Argos parameters in Storybook.
11
+ */
12
+ interface ArgosStorybookParameters {
13
+ /**
14
+ * Modes for the story.
15
+ */
16
+ modes?: Record<string, StorybookGlobals>;
17
+ }
5
18
  type ArgosScreenshotOptions = {
6
19
  /**
7
20
  * Fit the screenshot to the content size.
@@ -41,4 +54,4 @@ context: TestContext,
41
54
  */
42
55
  options?: ArgosScreenshotOptions): Promise<void>;
43
56
 
44
- export { type ArgosScreenshotOptions, argosScreenshot };
57
+ export { type ArgosScreenshotOptions, type ArgosStorybookParameters, argosScreenshot };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { waitForPageReady } from "@storybook/test-runner";
2
+ import { getStoryContext, waitForPageReady } from "@storybook/test-runner";
3
3
  import {
4
4
  argosScreenshot as argosPlaywrightScreenshot,
5
5
  DO_NOT_USE_setMetadataConfig
@@ -18,7 +18,6 @@ async function getArgosStorybookVersion() {
18
18
  // src/index.ts
19
19
  async function argosScreenshot(page, context, options) {
20
20
  const { fitToContent = true, ...screenshotOptions } = options ?? {};
21
- await waitForPageReady(page);
22
21
  const fitToContentOptions = (() => {
23
22
  if (options?.element || !fitToContent) {
24
23
  return {};
@@ -29,12 +28,7 @@ async function argosScreenshot(page, context, options) {
29
28
  argosCSS: `#storybook-root { padding: ${padding}px; width: fit-content; height: fit-content; zoom: ${zoom}; }` + (options?.argosCSS ?? "")
30
29
  };
31
30
  })();
32
- const version = await getArgosStorybookVersion();
33
- DO_NOT_USE_setMetadataConfig({
34
- sdk: { name: "@argos-ci/storybook", version },
35
- playwrightLibraries: ["@storybook/test-runner"]
36
- });
37
- await argosPlaywrightScreenshot(page, join(context.title, context.name), {
31
+ const argosOptions = {
38
32
  ...screenshotOptions,
39
33
  // Disable aria-busy stabilization by default
40
34
  stabilize: screenshotOptions.stabilize ?? {
@@ -42,7 +36,98 @@ async function argosScreenshot(page, context, options) {
42
36
  ...typeof screenshotOptions.stabilize === "object" ? screenshotOptions.stabilize : {}
43
37
  },
44
38
  ...fitToContentOptions
39
+ };
40
+ const [version, storyContext] = await Promise.all([
41
+ getArgosStorybookVersion(),
42
+ getStoryContext(page, context)
43
+ ]);
44
+ const defaultViewport = getDefaultViewport(storyContext.parameters);
45
+ if (defaultViewport) {
46
+ await page.setViewportSize(defaultViewport);
47
+ } else {
48
+ await page.setViewportSize({ width: 1280, height: 720 });
49
+ }
50
+ DO_NOT_USE_setMetadataConfig({
51
+ sdk: { name: "@argos-ci/storybook", version },
52
+ playwrightLibraries: ["@storybook/test-runner"]
45
53
  });
54
+ const argosParameters = getArgosParameters(storyContext.parameters);
55
+ const modes = argosParameters?.modes;
56
+ if (modes) {
57
+ for (const [name, globals] of Object.entries(modes)) {
58
+ if (globals.disabled) {
59
+ continue;
60
+ }
61
+ await setStorybookGlobals({ page, globals });
62
+ if (globals.viewport) {
63
+ const viewport = getViewport(storyContext.parameters, globals.viewport);
64
+ if (viewport) {
65
+ await page.setViewportSize(viewport);
66
+ }
67
+ }
68
+ await stabilizeAndScreenshot({
69
+ page,
70
+ context,
71
+ options: argosOptions,
72
+ suffix: ` ${name}`
73
+ });
74
+ }
75
+ } else {
76
+ await stabilizeAndScreenshot({ page, context, options: argosOptions });
77
+ }
78
+ }
79
+ async function setStorybookGlobals(args) {
80
+ const { page, globals } = args;
81
+ await page.evaluate((globals2) => {
82
+ const channel = globalThis.__STORYBOOK_PREVIEW__.channel;
83
+ channel.emit("updateGlobals", {
84
+ globals: {
85
+ ...channel.last("globalsUpdated")?.[0].initialGlobals,
86
+ ...globals2
87
+ }
88
+ });
89
+ }, globals);
90
+ }
91
+ async function stabilizeAndScreenshot(args) {
92
+ const { page, context, options } = args;
93
+ await waitForPageReady(page);
94
+ return argosPlaywrightScreenshot(
95
+ page,
96
+ join(context.title, context.name) + (args.suffix ?? ""),
97
+ options
98
+ );
99
+ }
100
+ function getDefaultViewport(parameters) {
101
+ const defaultViewport = parameters?.viewport?.defaultViewport;
102
+ if (defaultViewport) {
103
+ return getViewport(parameters, defaultViewport);
104
+ }
105
+ return null;
106
+ }
107
+ function getViewport(parameters, viewportName) {
108
+ if (typeof viewportName === "number") {
109
+ return { width: viewportName, height: 720 };
110
+ }
111
+ const viewports = parameters?.viewport?.viewports;
112
+ if (viewports && viewportName in viewports) {
113
+ if ("styles" in viewports[viewportName] && viewports[viewportName].styles) {
114
+ const width = parseInt(viewports[viewportName].styles.width, 10);
115
+ const height = parseInt(viewports[viewportName].styles.height, 10);
116
+ if (!isNaN(width) && !isNaN(height)) {
117
+ return { width, height };
118
+ }
119
+ }
120
+ }
121
+ return null;
122
+ }
123
+ function getArgosParameters(parameters) {
124
+ if ("argos" in parameters && parameters.argos && typeof parameters.argos === "object") {
125
+ return parameters.argos;
126
+ }
127
+ if ("chromatic" in parameters && parameters.chromatic && typeof parameters.chromatic === "object") {
128
+ return parameters.chromatic;
129
+ }
130
+ return null;
46
131
  }
47
132
  export {
48
133
  argosScreenshot
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@argos-ci/storybook",
3
3
  "description": "Visual testing for Storybook test runner.",
4
- "version": "2.0.1",
4
+ "version": "2.1.1",
5
5
  "author": "Smooth Code",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -39,7 +39,7 @@
39
39
  "node": ">=18.16.0"
40
40
  },
41
41
  "dependencies": {
42
- "@argos-ci/playwright": "5.0.1",
42
+ "@argos-ci/playwright": "5.0.3",
43
43
  "@argos-ci/util": "2.3.1"
44
44
  },
45
45
  "peerDependencies": {
@@ -52,6 +52,8 @@
52
52
  "@storybook/addon-essentials": "^8.6.9",
53
53
  "@storybook/addon-interactions": "^8.6.9",
54
54
  "@storybook/addon-links": "^8.6.9",
55
+ "@storybook/addon-themes": "^8.6.11",
56
+ "@storybook/addon-viewport": "^8.6.11",
55
57
  "@storybook/blocks": "^8.6.9",
56
58
  "@storybook/react": "^8.6.9",
57
59
  "@storybook/react-vite": "^8.6.9",
@@ -79,5 +81,5 @@
79
81
  "check-format": "prettier --check --ignore-unknown --ignore-path=./.gitignore --ignore-path=../../.gitignore --ignore-path=../../.prettierignore .",
80
82
  "lint": "eslint ."
81
83
  },
82
- "gitHead": "333ae8a299204b5d32edcdec5b7cb9bbb54766ca"
84
+ "gitHead": "fe8946c2796b7565834b4ec275e50f6bad20f498"
83
85
  }