FIX matching of refUrl & localUrl

This commit is contained in:
Norbert de Langen 2020-04-22 13:00:31 +02:00
parent 256934c2ce
commit d8725da3f4
No known key found for this signature in database
GPG Key ID: 976651DA156C2825
3 changed files with 30 additions and 18 deletions

View File

@ -44,20 +44,22 @@ export type Refs = Record<string, ComposedRef>;
export type RefId = string;
export type RefUrl = string;
export const getSourceType = (source: string) => {
const { origin, pathname } = location;
const findFilename = /(\/((?:[a-z]+?)\.(.+))|\/)$/;
if (
source === origin ||
source === `${origin + pathname}iframe.html` ||
source === `${origin + pathname.replace(/(?!.*\/).*\.html$/, '')}iframe.html`
) {
return 'local';
export const getSourceType = (source: string) => {
const { origin: localOrigin, pathname: localPathname } = location;
const { origin: sourceOrigin, pathname: sourcePathname } = new URL(source);
const localFull = `${localOrigin + localPathname}`.replace(findFilename, '');
const sourceFull = `${sourceOrigin + sourcePathname}`.replace(findFilename, '');
if (localFull === sourceFull) {
return ['local', sourceFull];
}
if (source) {
return 'external';
return ['external', sourceFull];
}
return null;
return [null, null];
};
export const defaultMapper: Mapper = (b, a) => {

View File

@ -289,7 +289,7 @@ export const init: ModuleFn = ({
const initModule = () => {
fullAPI.on(STORY_CHANGED, function handleStoryChange(storyId: string) {
const { source }: { source: string } = this;
const sourceType = getSourceType(source);
const [sourceType] = getSourceType(source);
if (sourceType === 'local') {
const options = fullAPI.getCurrentParameter('options');
@ -304,7 +304,7 @@ export const init: ModuleFn = ({
// the event originates from an iframe, event.source is the iframe's location origin + pathname
const { storyId } = store.getState();
const { source }: { source: string } = this;
const sourceType = getSourceType(source);
const [sourceType, sourceLocation] = getSourceType(source);
switch (sourceType) {
// if it's a local source, we do nothing special
@ -319,9 +319,10 @@ export const init: ModuleFn = ({
// if it's a ref, we need to map the incoming stories to a prefixed version, so it cannot conflict with others
case 'external': {
const ref = fullAPI.findRef(source);
const ref = fullAPI.findRef(sourceLocation);
if (ref) {
console.log('ref2', ref);
fullAPI.setRef(ref.id, { ...ref, ...data }, true);
break;
}
@ -345,7 +346,7 @@ export const init: ModuleFn = ({
[k: string]: any;
}) {
const { source }: { source: string } = this;
const sourceType = getSourceType(source);
const [sourceType, sourceLocation] = getSourceType(source);
switch (sourceType) {
case 'local': {
@ -354,7 +355,7 @@ export const init: ModuleFn = ({
}
case 'external': {
const ref = fullAPI.findRef(source);
const ref = fullAPI.findRef(sourceLocation);
fullAPI.selectStory(kind, story, { ...rest, ref: ref.id });
break;
}

View File

@ -27,15 +27,24 @@ describe('refs', () => {
describe('edge cases', () => {
it('returns "local" when source matches location with /index.html in path', () => {
// mockReturnValue(edgecaseLocations[0])
expect(getSourceType('https://storybook.js.org/storybook/iframe.html')).toBe('local');
expect(getSourceType('https://storybook.js.org/storybook/iframe.html')).toBe([
'local',
'https://storybook.js.org/storybook',
]);
});
});
// Other tests use "lastLocation" for the 'global' mock
it('returns "local" when source matches location', () => {
expect(getSourceType('https://storybook.js.org/storybook/iframe.html')).toBe('local');
expect(getSourceType('https://storybook.js.org/storybook/iframe.html')).toBe([
'local',
'https://storybook.js.org/storybook',
]);
});
it('returns "external" when source does not match location', () => {
expect(getSourceType('https://external.com/storybook/iframe.html')).toBe('external');
expect(getSourceType('https://external.com/storybook/iframe.html')).toBe(
'external',
'https://external.com/storybook'
);
});
});
});