Skip to content

Commit

Permalink
Merge pull request #28383 from storybookjs/kasper/mount
Browse files Browse the repository at this point in the history
Test: Add mount property to the story context
  • Loading branch information
kasperpeulen authored Jul 3, 2024
2 parents 6476483 + 9678d6f commit 137c344
Show file tree
Hide file tree
Showing 52 changed files with 1,483 additions and 714 deletions.
2 changes: 1 addition & 1 deletion code/addons/links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"prep": "node --loader ../../../scripts/node_modules/esbuild-register/loader.js -r ../../../scripts/node_modules/esbuild-register/register.js ../../../scripts/prepare/addon-bundle.ts"
},
"dependencies": {
"@storybook/csf": "0.1.10",
"@storybook/csf": "0.1.11",
"@storybook/global": "^5.0.0",
"ts-dedent": "^2.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion code/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
"prep": "bun ./scripts/prep.ts"
},
"dependencies": {
"@storybook/csf": "0.1.10",
"@storybook/csf": "0.1.11",
"@types/express": "^4.17.21",
"@types/node": "^18.0.0",
"browser-assert": "^1.2.1",
Expand Down
7 changes: 6 additions & 1 deletion code/core/src/preview-api/modules/preview-web/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { StoryStore } from '../../store';
import { StoryRender } from './render/StoryRender';
import type { CsfDocsRender } from './render/CsfDocsRender';
import type { MdxDocsRender } from './render/MdxDocsRender';
import { mountDestructured } from './render/mount-utils';
import type { Args, Globals, Renderer, StoryId } from '@storybook/core/types';
import type {
ModuleImportFn,
Expand Down Expand Up @@ -291,7 +292,11 @@ export class Preview<TRenderer extends Renderer> {
await Promise.all(
this.storyRenders
.filter((r) => r.id === storyId && !r.renderOptions.forceInitialArgs)
.map((r) => r.rerender())
.map((r) =>
// We only run the play function, with in a force remount.
// But when mount is destructured, the rendering happens inside of the play function.
r.story && mountDestructured(r.story.playFunction) ? r.remount() : r.rerender()
)
);

this.channel.emit(STORY_ARGS_UPDATED, {
Expand Down
39 changes: 39 additions & 0 deletions code/core/src/preview-api/modules/preview-web/PreviewWeb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,41 @@ describe('PreviewWeb', () => {
expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a');
});

describe('if play function destructures mount', () => {
it('passes forceRemount to renderToCanvas', async () => {
document.location.search = '?id=component-one--a';
const newImportFn = vi.fn(async (path) => {
if (path === './src/ComponentOne.stories.js') {
return {
...componentOneExports,
a: { ...componentOneExports.a, play: ({ mount }: any) => mount() },
};
}
return importFn(path);
});
await createAndRenderPreview({ importFn: newImportFn });

mockChannel.emit.mockClear();
projectAnnotations.renderToCanvas.mockClear();
emitter.emit(UPDATE_STORY_ARGS, {
storyId: 'component-one--a',
updatedArgs: { new: 'arg' },
});
await waitForRender();

expect(projectAnnotations.renderToCanvas).toHaveBeenCalledWith(
expect.objectContaining({
forceRemount: true,
storyContext: expect.objectContaining({
initialArgs: { foo: 'a', one: 1 },
args: { foo: 'a', new: 'arg', one: 'mapped-1' },
}),
}),
'story-element'
);
});
});

describe('while story is still rendering', () => {
it('runs loaders again after renderToCanvas is done', async () => {
// Arrange - set up a gate to control when the loaders run
Expand Down Expand Up @@ -3626,6 +3661,7 @@ describe('PreviewWeb', () => {
"dev",
"test",
],
"testingLibraryRender": undefined,
"title": "Component One",
},
"component-one--b": {
Expand Down Expand Up @@ -3674,6 +3710,7 @@ describe('PreviewWeb', () => {
"dev",
"test",
],
"testingLibraryRender": undefined,
"title": "Component One",
},
"component-one--e": {
Expand All @@ -3700,6 +3737,7 @@ describe('PreviewWeb', () => {
"dev",
"test",
],
"testingLibraryRender": undefined,
"title": "Component One",
},
"component-two--c": {
Expand Down Expand Up @@ -3736,6 +3774,7 @@ describe('PreviewWeb', () => {
"dev",
"test",
],
"testingLibraryRender": undefined,
"title": "Component Two",
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,9 @@ export class PreviewWithSelection<TRenderer extends Renderer> extends Preview<TR
this.channel.emit(STORY_THREW_EXCEPTION, { name, message, stack });
this.channel.emit(STORY_RENDER_PHASE_CHANGED, { newPhase: 'errored', storyId });

// Ignored exceptions exist for control flow purposes, and are typically handled elsewhere.
//
// FIXME: Should be '=== IGNORED_EXCEPTION', but currently the object
// is coming from two different bundles (index.js vs index.mjs)
//
// https://github.com/storybookjs/storybook/issues/19321
if (!error.message?.startsWith('ignoredException')) {
this.view.showErrorDisplay(error);
logger.error(`Error rendering story '${storyId}':`);
logger.error(error);
}
this.view.showErrorDisplay(error);
logger.error(`Error rendering story '${storyId}':`);
logger.error(error);
}

// renderError is used by the various app layers to inform the user they have done something
Expand Down
Loading

0 comments on commit 137c344

Please sign in to comment.