-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4176 from donaldpipowitch/allow-setting-params-to…
…-storyshot allow setting params to storyshot
- Loading branch information
Showing
4 changed files
with
63 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
addons/storyshots/storyshots-puppeteer/src/__tests__/url.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
}; |