Skip to content

Commit

Permalink
Flatten function since we're using async anyway
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuth committed May 13, 2024
1 parent ae095c0 commit c070b43
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions src/browser/StorybookPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ async function fetchStoriesFromWindow(): Promise<StorybookStory[]> {
);
}

await ready(storybookPreview);
await storybookPreview.extract();

const storyStore = storybookPreview.storyStore as StoryStore<Renderer>;
return storyStore.raw().map(pickOnlyNecessaryAndSerializableStoryProperties);

// Ensure the storybook preview is "ready", in a way that works in both Storybook 7 and 8.
// Alternatively, we could drop Storybook 7 support...
function ready(storybookPreview: PreviewWeb<Renderer>) {
Expand All @@ -86,36 +92,26 @@ async function fetchStoriesFromWindow(): Promise<StorybookStory[]> {
}
}

await ready(storybookPreview);

return storybookPreview.extract().then(() => {
// Pick only the properties we need from Storybook's representation of a story.
//
// This is necessary because Playwright's `page.evaluate` requires return values to be JSON
// serializable, so we need to make sure there are no non-serializable things in this object.
// There's no telling what Storybook addons people are using, and whether their parameters are
// serializable or not.
//
// See https://github.com/chanzuckerberg/axe-storybook-testing/issues/44 for a bug caused by this.
function pickOnlyNecessaryAndSerializableStoryProperties(
story: Story,
): StorybookStory {
return {
id: story.id,
name: story.name,
title: story.title,
parameters: {
axe: story.parameters.axe,
},
};
}

const storyStore = storybookPreview.storyStore as StoryStore<Renderer>;

return storyStore
.raw()
.map(pickOnlyNecessaryAndSerializableStoryProperties);
});
// Pick only the properties we need from Storybook's representation of a story.
//
// This is necessary because Playwright's `page.evaluate` requires return values to be JSON
// serializable, so we need to make sure there are no non-serializable things in this object.
// There's no telling what Storybook addons people are using, and whether their parameters are
// serializable or not.
//
// See https://github.com/chanzuckerberg/axe-storybook-testing/issues/44 for a bug caused by this.
function pickOnlyNecessaryAndSerializableStoryProperties(
story: Story,
): StorybookStory {
return {
id: story.id,
name: story.name,
title: story.title,
parameters: {
axe: story.parameters.axe,
},
};
}
}

/**
Expand Down

0 comments on commit c070b43

Please sign in to comment.