Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storyshots Puppeteer: don't infer story ID from its name #9291

Merged
merged 1 commit into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.