Skip to content

Commit

Permalink
feat: add addons panel url to stories with play
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Dec 15, 2021
1 parent 7cdc2b9 commit 12aa8ef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
16 changes: 9 additions & 7 deletions playwright/custom-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ class CustomEnvironment extends PlaywrightEnvironment {
await page.addScriptTag({
content: `
class StorybookTestRunnerError extends Error {
constructor(storyId, errorMessage) {
constructor(storyId, hasPlayFn, errorMessage) {
super(errorMessage);
this.name = 'StorybookTestRunnerError';
const storyUrl = \`${targetURL}?path=/story/\${storyId}&addonPanel=storybook/interactions/panel\`;
const storyUrl = \`${targetURL}?path=/story/\${storyId}\`;
const finalStoryUrl = storyUrl + (hasPlayFn ? '&addonPanel=storybook/interactions/panel' : '');

This comment has been minimized.

Copy link
@shilman

shilman Jan 24, 2022

Member

Can we always redirect to that addonPanel in the linked URL?

  • It would simplify the code a bunch
  • We don't have hasPlayFn in stories.json currently
this.message = \`\nAn error occurred in the following story:\n\${storyUrl}\n\nMessage:\n \${errorMessage}\`;
this.message = \`\nAn error occurred in the following story:\n\${finalStoryUrl}\n\nMessage:\n \${errorMessage}\`;
}
}
async function __throwError(storyId, errorMessage) {
throw new StorybookTestRunnerError(storyId, errorMessage);
}
async function __test(storyId) {
async function __test(storyId, hasPlayFn) {
const channel = window.__STORYBOOK_ADDONS_CHANNEL__;
if(!channel) {
throw new StorybookTestRunnerError(
storyId,
hasPlayFn,
'The test runner could not access the story. Are you sure the Storybook is running correctly in that URL?'
);
}
Expand All @@ -47,13 +49,13 @@ class CustomEnvironment extends PlaywrightEnvironment {
channel.on('storyRendered', () => resolve(document.getElementById('root')));
channel.on('storyUnchanged', () => resolve(document.getElementById('root')));
channel.on('storyErrored', ({ description }) => reject(
new StorybookTestRunnerError(storyId, description))
new StorybookTestRunnerError(storyId, hasPlayFn, description))
);
channel.on('storyThrewException', (error) => reject(
new StorybookTestRunnerError(storyId, error.message))
new StorybookTestRunnerError(storyId, hasPlayFn, error.message))
);
channel.on('storyMissing', (id) => id === storyId && reject(
new StorybookTestRunnerError(storyId, 'The story was missing when trying to access it.'))
new StorybookTestRunnerError(storyId, hasPlayFn, 'The story was missing when trying to access it.'))
);
channel.emit('setCurrentStory', { storyId });
Expand Down
2 changes: 2 additions & 0 deletions src/csf/transformCsf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface TestContext {
name: t.Literal;
title: t.Literal;
id: t.Literal;
hasPlayFn?: t.BooleanLiteral;
}
type FilePrefixer = () => t.Statement[];
type TestPrefixer = (context: TestContext) => t.Statement[];
Expand All @@ -37,6 +38,7 @@ const prefixFunction = (
name: t.stringLiteral(name), // FIXME .name annotation
title: t.stringLiteral(title), // FIXME: auto-title
id: t.stringLiteral(toId(title, name)),
hasPlayFn: t.booleanLiteral(!!input),
};
/*
This prefixes the play function with setup code
Expand Down
6 changes: 4 additions & 2 deletions src/playwright/transformPlaywright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ const testPrefixer = template(
page.evaluate(({ id, err }) => __throwError(id, err), { id: %%id%%, err: err.message });
});
return page.evaluate((id) => __test(id), %%id%%);
return page.evaluate(({ id, hasPlayFn }) => __test(id, hasPlayFn), {
id: %%id%%,
hasPlayFn: %%hasPlayFn%%,
});
}
`,
{
plugins: ['jsx'],
}
);

export const transformPlaywright = (src: string) => {
const result = transformCsf(src, {
// @ts-ignore
Expand Down

0 comments on commit 12aa8ef

Please sign in to comment.