Skip to content

Commit

Permalink
Merge pull request #18185 from storybookjs/fix/not-override-versions
Browse files Browse the repository at this point in the history
Composition: Fix  metadata.json incorrectly overriding main.js refs versions
  • Loading branch information
shilman committed May 10, 2022
2 parents a17ea78 + 1d7367e commit 0b3e2ea
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/api/src/modules/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const init: ModuleFn = ({ store, provider, singleStory }, { runCheck = tr
//
// then we fetch metadata if the above fetch succeeded

const loadedData: { error?: Error; v?: number; stories?: StoriesRaw; loginUrl?: string } = {};
const loadedData: SetRefData = {};
const query = version ? `?version=${version}` : '';
const credentials = isPublic ? 'omit' : 'include';

Expand Down Expand Up @@ -206,10 +206,14 @@ export const init: ModuleFn = ({ store, provider, singleStory }, { runCheck = tr
Object.assign(loadedData, { ...stories, ...metadata });
}

const versions =
ref.versions && Object.keys(ref.versions).length ? ref.versions : loadedData.versions;

await api.setRef(id, {
id,
url,
...loadedData,
...(versions ? { versions } : {}),
error: loadedData.error,
type: !loadedData.stories ? 'auto-inject' : 'lazy',
});
Expand Down
79 changes: 79 additions & 0 deletions lib/api/src/tests/refs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,85 @@ describe('Refs API', () => {
`);
});

it('checks refs (not replace versions)', async () => {
// given
const { api } = initRefs({ provider, store }, { runCheck: false });

setupResponses(
{
ok: true,
response: async () => ({ v: 2, stories: {} }),
},
{
ok: true,
response: async () => ({ v: 3, stories: {} }),
},
{
ok: true,
response: async () => {
throw new Error('not ok');
},
},
{
ok: true,
response: async () => ({
versions: {},
}),
}
);

await api.checkRef({
id: 'fake',
url: 'https://example.com',
title: 'Fake',
versions: { a: 'http://example.com/a', b: 'http://example.com/b' },
});

expect(fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"https://example.com/stories.json",
Object {
"credentials": "include",
"headers": Object {
"Accept": "application/json",
},
},
],
Array [
"https://example.com/metadata.json",
Object {
"cache": "no-cache",
"credentials": "include",
"headers": Object {
"Accept": "application/json",
},
},
],
]
`);

expect(store.setState.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"refs": Object {
"fake": Object {
"error": undefined,
"id": "fake",
"ready": false,
"stories": Object {},
"title": "Fake",
"type": "lazy",
"url": "https://example.com",
"versions": Object {
"a": "http://example.com/a",
"b": "http://example.com/b",
},
},
},
}
`);
});

it('checks refs (auth)', async () => {
// given
const { api } = initRefs({ provider, store }, { runCheck: false });
Expand Down

0 comments on commit 0b3e2ea

Please sign in to comment.