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

Composition: Fix metadata.json incorrectly overriding main.js refs versions #18185

Merged
merged 3 commits into from
May 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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