UNPKG

@testplane/storybook

Version:

Testplane plugin that enables runtime screenshot storybook tests autogeneration

116 lines 5.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.execute = void 0; function openStoryScript(storyId, storybookGlobals, shouldRemount, done) { function onPageLoad(fn) { if (document.readyState === "complete") { fn(); } else { document.onreadystatechange = function () { if (document.readyState === "complete") { fn(); } }; } } onPageLoad(function () { const channel = window.__STORYBOOK_ADDONS_CHANNEL__; const result = { rootSelector: "" }; function doneJson(value) { channel.off("playFunctionThrewException", onPlayFunctionThrewException); channel.off("storyRendered", onStoryRendered); channel.off("storyMissing", onStoryMissing); channel.off("storyThrewException", onStoryThrewException); channel.off("storyErrored", onStoryErrored); done(JSON.stringify(value)); } function getStorybookDefaultGlobals() { var _a; const savedGlobals = window.__TESTPLANE_STORYBOOK_INITIAL_GLOBALS__; if (savedGlobals) { return savedGlobals; } const setGlobalCalls = (_a = window.__STORYBOOK_ADDONS_CHANNEL__.data) === null || _a === void 0 ? void 0 : _a.setGlobals; return (setGlobalCalls && setGlobalCalls[0].globals) || {}; } function onPlayFunctionThrewException() { result.playFunctionError = "an unknown exception was thrown"; } function onStoryRendered() { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion result.rootSelector = ["#storybook-root", "#root"].find(function (selector) { return document.querySelector(selector); }); if (result.playFunctionError) { const instrumenter = window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__; const storyState = instrumenter ? instrumenter[storyId] : null; const storyLastCall = storyState && storyState.calls ? storyState.calls[storyState.calls.length - 1] : null; if (storyLastCall && storyLastCall.exception) { result.playFunctionError = storyLastCall.exception.message; } } const storybookPreview = window.__STORYBOOK_PREVIEW__; const isStorybookPreviewAvailable = storybookPreview && storybookPreview.storeInitializationPromise; const shouldUpdateStorybookGlobals = storybookGlobals && isStorybookPreviewAvailable; if (shouldUpdateStorybookGlobals) { storybookPreview.storeInitializationPromise.then(function () { const defaultGlobals = getStorybookDefaultGlobals(); channel.once("globalsUpdated", function () { doneJson(result); }); channel.emit("updateGlobals", { globals: Object.assign({}, defaultGlobals, storybookGlobals) }); }); } else { doneJson(result); } } function onStoryMissing(storyId) { if (storyId === "") { return; } const errorMessage = document.querySelector("#error-stack").innerText; const regExpMatch = /\w+:\s+x\s+/.exec(errorMessage); if (regExpMatch) { result.loadError = errorMessage.slice(regExpMatch[0].length); } else { result.loadError = errorMessage; } doneJson(result); } function onStoryThrewException(exception) { if (exception.message && !exception.message.startsWith("ignoredException")) { result.loadError = exception.message; doneJson(result); } } function onStoryErrored(error) { result.loadError = error.title; doneJson(result); } if (!channel) { result.loadError = "Couldn't find storybook channel. Looks like the opened page is not storybook preview"; doneJson(result); } channel.once("playFunctionThrewException", onPlayFunctionThrewException); channel.once("storyRendered", onStoryRendered); channel.once("storyMissing", onStoryMissing); channel.once("storyThrewException", onStoryThrewException); channel.once("storyErrored", onStoryErrored); if (shouldRemount) { channel.emit("setCurrentStory", { storyId: "" }); } channel.emit("setCurrentStory", { storyId }); }); } async function execute(browser, storyId, storybookGlobals, shouldRemount) { const getResult = ({ disableRemount = false } = {}) => browser .executeAsync(openStoryScript, storyId, storybookGlobals, shouldRemount && !disableRemount) .then(JSON.parse); return getResult().catch(() => getResult({ disableRemount: true })); } exports.execute = execute; exports.default = { execute }; //# sourceMappingURL=testplane-open-story.js.map