Skip to content

Commit

Permalink
Merge pull request #4176 from donaldpipowitch/allow-setting-params-to…
Browse files Browse the repository at this point in the history
…-storyshot

allow setting params to storyshot
  • Loading branch information
igor-dv authored Sep 17, 2018
2 parents 4ee8dc4 + b315898 commit c14b250
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion addons/storyshots/storyshots-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { imageSnapshot } from '@storybook/addon-storyshots-puppeteer';

initStoryshots({suite: 'Image storyshots', test: imageSnapshot({storybookUrl: 'http://my-specific-domain.com:9010'})});
```
The above config will use _<https://my-specific-domain.com:9010>_ for screenshots.
The above config will use _<https://my-specific-domain.com:9010>_ for screenshots. You can also use query parameters in your URL (e.g. for setting a different background for your storyshots, if you use `@storybook/addon-backgrounds`).


You may also use a local static build of storybook if you do not want to run the webpack dev-server:
Expand Down
49 changes: 49 additions & 0 deletions addons/storyshots/storyshots-puppeteer/src/__tests__/url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { constructUrl } from '../url';

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

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

it('can use a url without path and with query params', () => {
expect(constructUrl('http://localhost:9001?hello=world', 'someKind', 'someStory')).toEqual(
'http://localhost:9001/iframe.html?selectedKind=someKind&selectedStory=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(
'http://localhost:9001/iframe.html?selectedKind=someKind&selectedStory=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?selectedKind=someKind&selectedStory=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?selectedKind=someKind&selectedStory=someStory&hello=world'
);
});

it('can use a url with file protocol', () => {
expect(constructUrl('file://users/storybook', 'someKind', 'someStory')).toEqual(
'file://users/storybook/iframe.html?selectedKind=someKind&selectedStory=someStory'
);
});
});
7 changes: 2 additions & 5 deletions addons/storyshots/storyshots-puppeteer/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import puppeteer from 'puppeteer';
import { toMatchImageSnapshot } from 'jest-image-snapshot';
import { logger } from '@storybook/node-logger';
import { constructUrl } from './url';

expect.extend({ toMatchImageSnapshot });

Expand Down Expand Up @@ -43,11 +44,7 @@ export const imageSnapshot = (customConfig = {}) => {

return;
}

const encodedKind = encodeURIComponent(kind);
const encodedStoryName = encodeURIComponent(story);
const storyUrl = `/iframe.html?selectedKind=${encodedKind}&selectedStory=${encodedStoryName}`;
const url = storybookUrl + storyUrl;
const url = constructUrl(storybookUrl, kind, story);

if (!browser || !page) {
logger.error(
Expand Down
11 changes: 11 additions & 0 deletions addons/storyshots/storyshots-puppeteer/src/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { URL } from 'url';

export const constructUrl = (storybookUrl, kind, story) => {
const encodedKind = encodeURIComponent(kind);
const encodedStoryName = encodeURIComponent(story);
const storyUrl = `/iframe.html?selectedKind=${encodedKind}&selectedStory=${encodedStoryName}`;
const { protocol, host, pathname, search } = new URL(storybookUrl);
const pname = pathname.replace(/\/$/, ''); // removes trailing /
const query = search.replace('?', '&'); // convert leading $ to &
return `${protocol}//${host}${pname}${storyUrl}${query}`;
};

0 comments on commit c14b250

Please sign in to comment.