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

Core: Fix args values updated from url to control #16508

Merged
merged 10 commits into from
Nov 7, 2021
10 changes: 10 additions & 0 deletions lib/preview-web/src/PreviewWeb.test.ts
Expand Up @@ -185,6 +185,16 @@ describe('PreviewWeb', () => {
foo: 'url',
});
});
it('updates args from the URL', async () => {
document.location.search = '?id=component-one--a&args=foo:url';

await createAndRenderPreview();

expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, {
storyId: 'component-one--a',
args: { foo: 'url' },
});
});

it('allows async getProjectAnnotations', async () => {
const preview = new PreviewWeb();
Expand Down
15 changes: 8 additions & 7 deletions lib/preview-web/src/PreviewWeb.tsx
Expand Up @@ -333,14 +333,12 @@ export class PreviewWeb<TFramework extends AnyFramework> {
// - a story selected in "docs" viewMode,
// in which case we render the docsPage for that story
async renderSelection({ persistedArgs }: { persistedArgs?: Args } = {}) {
if (!this.urlStore.selection) {
const { selection } = this.urlStore;
if (!selection) {
throw new Error('Cannot render story as no selection was made');
}

const {
selection,
selection: { storyId },
} = this.urlStore;
const { storyId } = selection;

let story;
try {
Expand Down Expand Up @@ -389,8 +387,11 @@ export class PreviewWeb<TFramework extends AnyFramework> {
args,
});
}
// If the implementation changed, the args also may have changed
if (implementationChanged) {

// For v6 mode / compatibility
// If the implementation changed, or args were persisted, the args may have changed,
// and the STORY_PREPARED event above may not be respected.
if (implementationChanged || persistedArgs) {
this.channel.emit(Events.STORY_ARGS_UPDATED, { storyId, args });
}

Expand Down