Skip to content

Commit

Permalink
Removing dependency of storybook version
Browse files Browse the repository at this point in the history
  • Loading branch information
rishigupta1599 committed May 6, 2024
1 parent 5a4fadb commit ba25cc3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
7 changes: 2 additions & 5 deletions src/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { logger, PercyConfig } from '@percy/cli-command';
import { yieldAll } from '@percy/cli-command/utils';
import qs from 'qs';
import {
checkStorybookVersion,
fetchStorybookPreviewResource,
evalStorybookEnvironmentInfo,
evalStorybookStorySnapshots,
Expand Down Expand Up @@ -162,8 +161,6 @@ export async function* takeStorybookSnapshots(percy, callback, { baseUrl, flags
let lastCount;

log.debug(`Requesting Storybook: ${baseUrl}`);
let storybookVersion = await checkStorybookVersion();
log.debug(`Storybook Version: ${storybookVersion}`);
// start a timeout to show a log if storybook takes a few seconds to respond
let logTimeout = setTimeout(log.warn, 3000, 'Waiting on a response from Storybook...');
let previewResource = yield fetchStorybookPreviewResource(percy, previewUrl);
Expand All @@ -177,7 +174,7 @@ export async function* takeStorybookSnapshots(percy, callback, { baseUrl, flags
// gather storybook data in parallel
let [environmentInfo, stories] = yield* yieldAll([
withPage(percy, aboutUrl, p => p.eval(evalStorybookEnvironmentInfo)),
withPage(percy, previewUrl, p => p.eval(evalStorybookStorySnapshots, storybookVersion))
withPage(percy, previewUrl, p => p.eval(evalStorybookStorySnapshots))
]);

// map stories to snapshot options
Expand Down Expand Up @@ -218,7 +215,7 @@ export async function* takeStorybookSnapshots(percy, callback, { baseUrl, flags
} else {
log.debug(`Loading story: ${options.name}`);
// when not dry-running and javascript is not enabled, capture the story dom
yield page.eval(evalSetCurrentStory, { id, args, globals, queryParams }, storybookVersion);
yield page.eval(evalSetCurrentStory, { id, args, globals, queryParams });
/* istanbul ignore next: tested, but coverage is stripped */
let { dom, domSnapshot = dom } = yield page.snapshot(options);
options.domSnapshot = domSnapshot;
Expand Down
35 changes: 13 additions & 22 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export function evalStorybookEnvironmentInfo({ waitForXPath }) {

// Evaluate and return serialized Storybook stories to snapshot
/* istanbul ignore next: no instrumenting injected code */
export function evalStorybookStorySnapshots({ waitFor }, storybookVersion) {
export function evalStorybookStorySnapshots({ waitFor }) {
let serialize = (what, value, invalid) => {
if (what === 'include' || what === 'exclude') {
return [].concat(value).filter(Boolean).map(v => v.toString());
Expand All @@ -226,23 +226,18 @@ export function evalStorybookStorySnapshots({ waitFor }, storybookVersion) {
};

return waitFor(async () => {
if (storybookVersion === 8) {
await window.__STORYBOOK_PREVIEW__?.cacheAllCSFFiles?.();
await window.__STORYBOOK_PREVIEW__?.ready?.();
const storiesObj = await (window.__STORYBOOK_PREVIEW__?.extract?.());
if (storiesObj) {
return Object.values(storiesObj);
}
throw new Error();
} else {
await window.__STORYBOOK_PREVIEW__?.ready?.();
// uncache stories, if cached via storyStorev7: true
await (window.__STORYBOOK_PREVIEW__?.cacheAllCSFFiles?.() ||
window.__STORYBOOK_STORY_STORE__?.cacheAllCSFFiles?.());
// use newer storybook APIs before old APIs
await (window.__STORYBOOK_PREVIEW__?.extract?.() ||
window.__STORYBOOK_STORY_STORE__?.extract?.());
return window.__STORYBOOK_STORY_STORE__.raw();
await (window.__STORYBOOK_PREVIEW__?.cacheAllCSFFiles?.() ||
window.__STORYBOOK_STORY_STORE__?.cacheAllCSFFiles?.());

const storiesObj = await (window.__STORYBOOK_PREVIEW__?.extract?.());
if (storiesObj && !Array.isArray(storiesObj)) {
return Object.values(storiesObj);
}

await window.__STORYBOOK_STORY_STORE__?.extract?.();
return window.__STORYBOOK_STORY_STORE__.raw();
}, 5000).catch(() => Promise.reject(new Error(
'Storybook object not found on the window. ' +
'Open Storybook and check the console for errors.'
Expand All @@ -264,15 +259,11 @@ export function evalStorybookStorySnapshots({ waitFor }, storybookVersion) {

// Change the currently selected story within Storybook, decoding args and globals as necessary
/* istanbul ignore next: no instrumenting injected code */
export function evalSetCurrentStory({ waitFor }, story, storybookVersion) {
export function evalSetCurrentStory({ waitFor }, story) {
return waitFor(() => {
// get the correct channel depending on the storybook version
if (storybookVersion === 8) {
return window.__STORYBOOK_PREVIEW__?.channel;
} else {
return window.__STORYBOOK_PREVIEW__?.channel ||
return window.__STORYBOOK_PREVIEW__?.channel ||
window.__STORYBOOK_STORY_STORE__?._channel;
}
}, 5000).catch(() => Promise.reject(new Error(
'Storybook object not found on the window. ' +
'Open Storybook and check the console for errors.'
Expand Down

0 comments on commit ba25cc3

Please sign in to comment.