@canonical/storybook-addon-msw 0.42.0 → 0.44.0

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.
@@ -1,10 +1,11 @@
1
1
  import { KEY } from "./constants.js";
2
- import { withMSW } from "./withMSW.js";
2
+ import { mswLoader, withMSW } from "./withMSW.js";
3
3
  /**
4
4
  * Note: if you want to use JSX in this file, rename it to `preview.tsx`
5
5
  * and update the entry prop in tsup.config.ts to use "src/preview.tsx",
6
6
  */
7
7
  const preview = {
8
+ loaders: [mswLoader],
8
9
  decorators: [withMSW],
9
10
  initialGlobals: {
10
11
  [KEY]: true,
@@ -1 +1 @@
1
- {"version":3,"file":"preview.js","sourceRoot":"","sources":["../../src/preview.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC;;;GAGG;AAEH,MAAM,OAAO,GAAiC;IAC5C,UAAU,EAAE,CAAC,OAAO,CAAC;IACrB,cAAc,EAAE;QACd,CAAC,GAAG,CAAC,EAAE,IAAI;KACZ;CACF,CAAC;AAEF,eAAe,OAAO,CAAC"}
1
+ {"version":3,"file":"preview.js","sourceRoot":"","sources":["../../src/preview.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAElD;;;GAGG;AAEH,MAAM,OAAO,GAAiC;IAC5C,OAAO,EAAE,CAAC,SAAS,CAAC;IACpB,UAAU,EAAE,CAAC,OAAO,CAAC;IACrB,cAAc,EAAE;QACd,CAAC,GAAG,CAAC,EAAE,IAAI;KACZ;CACF,CAAC;AAEF,eAAe,OAAO,CAAC"}
@@ -1,6 +1,5 @@
1
1
  import { setupWorker } from "msw/browser";
2
- import React from "react";
3
- import { useEffect, useGlobals, useParameter, useRef, useState, } from "storybook/internal/preview-api";
2
+ import { useEffect, useGlobals, useParameter, useRef, } from "storybook/internal/preview-api";
4
3
  import { KEY, PARAM_KEY } from "./constants.js";
5
4
  // Singleton worker instance
6
5
  let worker = null;
@@ -27,58 +26,61 @@ const initializeWorker = async () => {
27
26
  })();
28
27
  return workerPromise;
29
28
  };
29
+ /**
30
+ * Whether MSW should serve this story. The toolbar stores the global as a
31
+ * boolean, and only an explicit `false` turns it off; a story opts out with
32
+ * `parameters.msw.disable`.
33
+ */
34
+ const isActive = (globalValue, parameter) => globalValue !== false && !(parameter?.disable ?? false);
35
+ /**
36
+ * Install the story's handlers on a started worker, or clear them when MSW is
37
+ * off for the story. A worker that failed to start leaves the story to run
38
+ * against the network.
39
+ */
40
+ const applyHandlers = async (active, handlers) => {
41
+ if (!active) {
42
+ worker?.resetHandlers();
43
+ return;
44
+ }
45
+ const activeWorker = await initializeWorker();
46
+ if (!activeWorker)
47
+ return;
48
+ activeWorker.resetHandlers();
49
+ if (handlers && handlers.length > 0) {
50
+ activeWorker.use(...handlers);
51
+ }
52
+ };
53
+ /**
54
+ * Starts the worker and installs the story's handlers before the story
55
+ * renders. Storybook awaits loaders before it renders a story and before its
56
+ * play function runs, so the story's first render already has its mocks and a
57
+ * play function never waits on MSW.
58
+ */
59
+ export const mswLoader = async ({ globals, parameters, }) => {
60
+ const parameter = parameters[PARAM_KEY];
61
+ await applyHandlers(isActive(globals[KEY], parameter), parameter?.handlers);
62
+ return {};
63
+ };
64
+ /**
65
+ * Keeps the handlers in step with the toolbar toggle while a story is shown.
66
+ * It never holds the story back: the loader has already started the worker,
67
+ * and preview-hook effects only run once the story and its play function have
68
+ * finished, so gating the render here would deadlock a play function.
69
+ */
30
70
  export const withMSW = (StoryFn) => {
31
71
  const [globals] = useGlobals();
32
- const mswGlobals = globals[KEY];
33
- const mswParameter = useParameter(PARAM_KEY);
34
- const isEnabled = mswGlobals?.enabled ?? true;
35
- const isDisabled = mswParameter?.disable ?? false;
36
- const handlers = mswParameter?.handlers;
37
- // Effects of Storybook preview hooks only run once the story has rendered,
38
- // which includes its play function. When MSW is off there is nothing to wait
39
- // for, so render the story straight away rather than a placeholder the play
40
- // function would see instead of the story.
41
- const [isWorkerReady, setIsWorkerReady] = useState(!isEnabled || isDisabled);
42
- // Store handlers in a ref to avoid re-running effect on every render
43
- // The handlers array reference changes on each render even if contents are same
44
- const handlersRef = useRef(handlers);
45
- handlersRef.current = handlers;
72
+ const parameter = useParameter(PARAM_KEY);
73
+ const active = isActive(globals[KEY], parameter);
74
+ // The handlers array is a new reference on every render even when its
75
+ // contents are the same; read it through a ref so the effect does not re-run.
76
+ const handlersRef = useRef(parameter?.handlers);
77
+ handlersRef.current = parameter?.handlers;
46
78
  useEffect(() => {
47
- if (!isEnabled || isDisabled) {
48
- setIsWorkerReady(true);
49
- return;
50
- }
51
- let cancelled = false;
52
- const setupHandlers = async () => {
53
- const activeWorker = await initializeWorker();
54
- if (cancelled)
55
- return;
56
- // If worker failed to initialize, just mark as ready and continue without MSW
57
- if (!activeWorker) {
58
- setIsWorkerReady(true);
59
- return;
60
- }
61
- const currentHandlers = handlersRef.current;
62
- if (currentHandlers && currentHandlers.length > 0) {
63
- activeWorker.resetHandlers();
64
- activeWorker.use(...currentHandlers);
65
- }
66
- else {
67
- activeWorker.resetHandlers();
68
- }
69
- setIsWorkerReady(true);
70
- };
71
- setupHandlers();
79
+ void applyHandlers(active, handlersRef.current);
72
80
  return () => {
73
- cancelled = true;
74
- if (worker) {
75
- worker.resetHandlers();
76
- }
81
+ worker?.resetHandlers();
77
82
  };
78
- }, [isEnabled, isDisabled]);
79
- if (!isWorkerReady) {
80
- return React.createElement("div", {}, "Loading MSW...");
81
- }
83
+ }, [active]);
82
84
  return StoryFn();
83
85
  };
84
86
  //# sourceMappingURL=withMSW.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"withMSW.js","sourceRoot":"","sources":["../../src/withMSW.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,WAAW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CAAC;IACxD,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC,CAAC;IAEjD,sEAAsE;IACtE,8EAA8E;IAC9E,MAAM,WAAW,GAAG,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAChD,WAAW,CAAC,OAAO,GAAG,SAAS,EAAE,QAAQ,CAAC;IAE1C,SAAS,CAAC,GAAG,EAAE;QACb,KAAK,aAAa,CAAC,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;QAChD,OAAO,GAAG,EAAE;YACV,MAAM,EAAE,aAAa,EAAE,CAAC;QAC1B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAEb,OAAO,OAAO,EAAE,CAAC;AACnB,CAAC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"preview.d.ts","sourceRoot":"","sources":["../../src/preview.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAK7E;;;GAGG;AAEH,QAAA,MAAM,OAAO,EAAE,kBAAkB,CAAC,QAAQ,CAKzC,CAAC;eAEa,OAAO"}
1
+ {"version":3,"file":"preview.d.ts","sourceRoot":"","sources":["../../src/preview.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAK7E;;;GAGG;AAEH,QAAA,MAAM,OAAO,EAAE,kBAAkB,CAAC,QAAQ,CAMzC,CAAC;eAEa,OAAO"}
@@ -1,3 +1,16 @@
1
- import type { Renderer, PartialStoryFn as StoryFunction } from "storybook/internal/types";
1
+ import type { LoaderFunction, Renderer, PartialStoryFn as StoryFunction } from "storybook/internal/types";
2
+ /**
3
+ * Starts the worker and installs the story's handlers before the story
4
+ * renders. Storybook awaits loaders before it renders a story and before its
5
+ * play function runs, so the story's first render already has its mocks and a
6
+ * play function never waits on MSW.
7
+ */
8
+ export declare const mswLoader: LoaderFunction<Renderer>;
9
+ /**
10
+ * Keeps the handlers in step with the toolbar toggle while a story is shown.
11
+ * It never holds the story back: the loader has already started the worker,
12
+ * and preview-hook effects only run once the story and its play function have
13
+ * finished, so gating the render here would deadlock a play function.
14
+ */
2
15
  export declare const withMSW: (StoryFn: StoryFunction<Renderer>) => any;
3
16
  //# sourceMappingURL=withMSW.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"withMSW.d.ts","sourceRoot":"","sources":["../../src/withMSW.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,QAAQ,EACR,cAAc,IAAI,aAAa,EAChC,MAAM,0BAA0B,CAAC;AAgClC,eAAO,MAAM,OAAO,YAAa,aAAa,CAAC,QAAQ,CAAC,QA+DvD,CAAC"}
1
+ {"version":3,"file":"withMSW.d.ts","sourceRoot":"","sources":["../../src/withMSW.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACV,cAAc,EACd,QAAQ,EACR,cAAc,IAAI,aAAa,EAChC,MAAM,0BAA0B,CAAC;AA+DlC;;;;;GAKG;AACH,eAAO,MAAM,SAAS,EAAE,cAAc,CAAC,QAAQ,CAO9C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,OAAO,YAAa,aAAa,CAAC,QAAQ,CAAC,QAkBvD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonical/storybook-addon-msw",
3
- "version": "0.42.0",
3
+ "version": "0.44.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -19,10 +19,10 @@
19
19
  ],
20
20
  "repository": {
21
21
  "type": "git",
22
- "url": "https://github.com/canonical/pragma"
22
+ "url": "https://github.com/canonical/pragma-web"
23
23
  },
24
24
  "bugs": {
25
- "url": "https://github.com/canonical/pragma/issues"
25
+ "url": "https://github.com/canonical/pragma-web/issues"
26
26
  },
27
27
  "license": "LGPL-3.0",
28
28
  "exports": {
@@ -50,15 +50,15 @@
50
50
  "test": "echo 'No unit tests yet'"
51
51
  },
52
52
  "dependencies": {
53
- "@canonical/styles-debug": "^0.42.0",
53
+ "@canonical/styles-debug": "^0.44.0",
54
54
  "@storybook/icons": "^2.0.1"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@biomejs/biome": "2.5.14",
58
- "@canonical/biome-config": "^0.42.0",
59
- "@canonical/storybook-config": "^0.42.0",
60
- "@canonical/typescript-config-react": "^0.42.0",
61
- "@canonical/webarchitect": "^0.42.0",
58
+ "@canonical/biome-config": "0.43.0",
59
+ "@canonical/storybook-config": "^0.44.0",
60
+ "@canonical/typescript-config-react": "^0.44.0",
61
+ "@canonical/webarchitect": "0.43.0",
62
62
  "@storybook/addon-docs": "^10.6.0",
63
63
  "@storybook/react-vite": "^10.6.0",
64
64
  "@types/node": "^26.0.0",
@@ -97,5 +97,5 @@
97
97
  "public"
98
98
  ]
99
99
  },
100
- "gitHead": "75c2b17029ba899be3fa645be28290ac6fcb83a9"
100
+ "gitHead": "c1f2abc7f04aa29a88f0e6036645469e82a5dd33"
101
101
  }