Skip to content

Commit

Permalink
Merge pull request #9291 from storybookjs/stpryshots-puppeteer-id
Browse files Browse the repository at this point in the history
Storyshots Puppeteer: don't infer story ID from its name
  • Loading branch information
shilman authored Jan 2, 2020
2 parents 81ab0e1 + b31fceb commit f7571d8
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions addons/storyshots/storyshots-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"regenerator-runtime": "^0.13.3"
},
"devDependencies": {
"@storybook/csf": "0.0.1",
"@types/puppeteer": "^2.0.0"
},
"peerDependencies": {
Expand Down
26 changes: 15 additions & 11 deletions addons/storyshots/storyshots-puppeteer/src/__tests__/url.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
import { toId } from '@storybook/csf';

import { constructUrl } from '../url';

jest.mock('@storybook/node-logger');

const id = toId('someKind', 'someStory');

describe('Construct URL for Storyshots', () => {
it('can use a url without path and without query params', () => {
expect(constructUrl('http://localhost:9001', 'someKind', 'someStory')).toEqual(
expect(constructUrl('http://localhost:9001', id)).toEqual(
'http://localhost:9001/iframe.html?id=somekind--somestory'
);
});

it('can use a url without path (but slash) and without query params', () => {
expect(constructUrl('http://localhost:9001/', 'someKind', 'someStory')).toEqual(
expect(constructUrl('http://localhost:9001/', id)).toEqual(
'http://localhost:9001/iframe.html?id=somekind--somestory'
);
});

it('can use a url without path and with query params', () => {
expect(constructUrl('http://localhost:9001?hello=world', 'someKind', 'someStory')).toEqual(
expect(constructUrl('http://localhost:9001?hello=world', id)).toEqual(
'http://localhost:9001/iframe.html?id=somekind--somestory&hello=world'
);
});

it('can use a url without path (buth slash) and with query params', () => {
expect(constructUrl('http://localhost:9001/?hello=world', 'someKind', 'someStory')).toEqual(
expect(constructUrl('http://localhost:9001/?hello=world', id)).toEqual(
'http://localhost:9001/iframe.html?id=somekind--somestory&hello=world'
);
});

it('can use a url with some path and query params', () => {
expect(
constructUrl('http://localhost:9001/nice-path?hello=world', 'someKind', 'someStory')
).toEqual('http://localhost:9001/nice-path/iframe.html?id=somekind--somestory&hello=world');
expect(constructUrl('http://localhost:9001/nice-path?hello=world', id)).toEqual(
'http://localhost:9001/nice-path/iframe.html?id=somekind--somestory&hello=world'
);
});

it('can use a url with some path (slash) and query params', () => {
expect(
constructUrl('http://localhost:9001/nice-path/?hello=world', 'someKind', 'someStory')
).toEqual('http://localhost:9001/nice-path/iframe.html?id=somekind--somestory&hello=world');
expect(constructUrl('http://localhost:9001/nice-path/?hello=world', id)).toEqual(
'http://localhost:9001/nice-path/iframe.html?id=somekind--somestory&hello=world'
);
});

it('can use a url with file protocol', () => {
expect(constructUrl('file://users/storybook', 'someKind', 'someStory')).toEqual(
expect(constructUrl('file://users/storybook', id)).toEqual(
'file://users/storybook/iframe.html?id=somekind--somestory'
);
});
Expand Down
4 changes: 2 additions & 2 deletions addons/storyshots/storyshots-puppeteer/src/puppeteerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const puppeteerTest = (customConfig: Partial<PuppeteerTestConfig> = {}) =
let page: Page; // Hold ref to the page to screenshot.

const testFn = async ({ context }: any) => {
const { kind, framework, name } = context;
const { kind, framework, name, id } = context;
if (framework === 'react-native') {
// Skip tests since RN is not a browser environment.
logger.error(
Expand All @@ -29,7 +29,7 @@ export const puppeteerTest = (customConfig: Partial<PuppeteerTestConfig> = {}) =
return;
}

const url = constructUrl(storybookUrl, kind, name);
const url = constructUrl(storybookUrl, id);
const options = { context, url };
if (testBody.filter != null && !testBody.filter(options)) {
return;
Expand Down
6 changes: 1 addition & 5 deletions addons/storyshots/storyshots-puppeteer/src/url.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { toId } from '@storybook/csf';

import { URL } from 'url';

export const constructUrl = (storybookUrl: string, kind: string, story: string) => {
const id = toId(kind, story);

export const constructUrl = (storybookUrl: string, id: string) => {
const storyUrl = `/iframe.html?id=${id}`;
const { protocol, host, pathname, search } = new URL(storybookUrl);
const pname = pathname.replace(/\/$/, ''); // removes trailing /
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const block = () => {
);
};
block.story = {
name: 'Block story',
parameters: {
async puppeteerTest(page) {
const element = await page.$('[data-test-block]');
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

1 comment on commit f7571d8

@vercel
Copy link

@vercel vercel bot commented on f7571d8 Jan 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.