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

Add a new throwPlayFunctionExceptions parameter #19143

Merged
merged 4 commits into from
Sep 12, 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
4 changes: 4 additions & 0 deletions code/addons/interactions/src/preset/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ export const { step: runStep } = instrument(
{ step: (label: StepLabel, play: PlayFunction, context: PlayFunctionContext) => play(context) },
{ intercept: true }
);

export const parameters = {
throwPlayFunctionExceptions: false,
};
2 changes: 1 addition & 1 deletion code/lib/preview-web/src/PreviewWeb.mockdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const projectAnnotations = {
renderToDOM: jest.fn().mockReturnValue(teardownRenderToDOM),
parameters: { docs: { renderer: () => docsRenderer } },
};
export const getProjectAnnotations = () => projectAnnotations;
export const getProjectAnnotations = jest.fn(() => projectAnnotations as any);

export const storyIndex: StoryIndex = {
v: 4,
Expand Down
83 changes: 63 additions & 20 deletions code/lib/preview-web/src/PreviewWeb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,16 +511,13 @@ describe('PreviewWeb', () => {

it('renders helpful message if renderToDOM is undefined', async () => {
document.location.search = '?id=component-one--a';

getProjectAnnotations.mockReturnValueOnce({
...projectAnnotations,
renderToDOM: undefined,
});
const preview = new PreviewWeb();
await expect(
preview.initialize({
importFn,
getProjectAnnotations: () => ({
...getProjectAnnotations,
renderToDOM: undefined,
}),
})
).rejects.toThrow();
await expect(preview.initialize({ importFn, getProjectAnnotations })).rejects.toThrow();

expect(preview.view.showErrorDisplay).toHaveBeenCalled();
expect((preview.view.showErrorDisplay as jest.Mock).mock.calls[0][0])
Expand All @@ -533,20 +530,56 @@ describe('PreviewWeb', () => {
`);
});

it('emits but does not render exception if the play function throws', async () => {
const error = new Error('error');
componentOneExports.a.play.mockImplementationOnce(() => {
throw error;
describe('when `throwPlayFunctionExceptions` is set', () => {
it('emits but does not render exception if the play function throws', async () => {
const error = new Error('error');
componentOneExports.a.play.mockImplementationOnce(() => {
throw error;
});

getProjectAnnotations.mockReturnValueOnce({
...projectAnnotations,
parameters: {
...projectAnnotations.parameters,
throwPlayFunctionExceptions: false,
},
});

document.location.search = '?id=component-one--a';
const preview = await createAndRenderPreview();

expect(mockChannel.emit).toHaveBeenCalledWith(
PLAY_FUNCTION_THREW_EXCEPTION,
serializeError(error)
);
expect(preview.view.showErrorDisplay).not.toHaveBeenCalled();
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
expect(mockChannel.emit).not.toHaveBeenCalledWith(
STORY_THREW_EXCEPTION,
serializeError(error)
);
});
});

document.location.search = '?id=component-one--a';
const preview = await createAndRenderPreview();
describe('when `throwPlayFunctionExceptions` is unset', () => {
it('emits AND renders exception if the play function throws', async () => {
const error = new Error('error');
componentOneExports.a.play.mockImplementationOnce(() => {
throw error;
});

expect(mockChannel.emit).toHaveBeenCalledWith(
PLAY_FUNCTION_THREW_EXCEPTION,
serializeError(error)
);
expect(preview.view.showErrorDisplay).not.toHaveBeenCalled();
document.location.search = '?id=component-one--a';
const preview = await createAndRenderPreview();

expect(mockChannel.emit).toHaveBeenCalledWith(
PLAY_FUNCTION_THREW_EXCEPTION,
serializeError(error)
);
expect(preview.view.showErrorDisplay).toHaveBeenCalled();
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
expect(mockChannel.emit).toHaveBeenCalledWith(
STORY_THREW_EXCEPTION,
serializeError(error)
);
});
});

it('renders exception if the story calls showException', async () => {
Expand Down Expand Up @@ -3119,6 +3152,16 @@ describe('PreviewWeb', () => {
});
});

describe('with no selection', () => {
// eslint-disable-next-line jest/expect-expect
it('does not error', async () => {
const preview = await createAndRenderPreview();
await preview.onGetProjectAnnotationsChanged({
getProjectAnnotations: newGetProjectAnnotations,
});
});
});

it('shows an error the new value throws', async () => {
document.location.search = '?id=component-one--a';
const preview = await createAndRenderPreview();
Expand Down
4 changes: 3 additions & 1 deletion code/lib/preview-web/src/PreviewWeb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ export class PreviewWeb<TFramework extends AnyFramework> extends Preview<TFramew
}) {
await super.onGetProjectAnnotationsChanged({ getProjectAnnotations });

this.renderSelection();
if (this.urlStore.selection) {
this.renderSelection();
}
}

// This happens when a glob gets HMR-ed
Expand Down
1 change: 1 addition & 0 deletions code/lib/preview-web/src/render/StoryRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export class StoryRender<TFramework extends AnyFramework> implements Render<TFra
await this.runPhase(abortSignal, 'errored', async () => {
this.channel.emit(PLAY_FUNCTION_THREW_EXCEPTION, serializeError(error));
});
if (this.story.parameters.throwPlayFunctionExceptions !== false) throw error;
}
this.disableKeyListeners = false;
if (abortSignal.aborted) return;
Expand Down