diff --git a/lib/preview-web/src/Preview.tsx b/lib/preview-web/src/Preview.tsx index ab3f4be91186..2eae90f861bf 100644 --- a/lib/preview-web/src/Preview.tsx +++ b/lib/preview-web/src/Preview.tsx @@ -1,7 +1,18 @@ import dedent from 'ts-dedent'; import global from 'global'; import { SynchronousPromise } from 'synchronous-promise'; -import Events from '@storybook/core-events'; +import { + CONFIG_ERROR, + FORCE_REMOUNT, + FORCE_RE_RENDER, + GLOBALS_UPDATED, + RESET_STORY_ARGS, + SET_GLOBALS, + STORY_ARGS_UPDATED, + STORY_INDEX_INVALIDATED, + UPDATE_GLOBALS, + UPDATE_STORY_ARGS, +} from '@storybook/core-events'; import { logger } from '@storybook/client-logger'; import { addons, Channel } from '@storybook/addons'; import { AnyFramework, StoryId, ProjectAnnotations, Args, Globals } from '@storybook/csf'; @@ -80,13 +91,13 @@ export class Preview { } setupListeners() { - this.serverChannel?.on(Events.STORY_INDEX_INVALIDATED, this.onStoryIndexChanged.bind(this)); + this.serverChannel?.on(STORY_INDEX_INVALIDATED, this.onStoryIndexChanged.bind(this)); - this.channel.on(Events.UPDATE_GLOBALS, this.onUpdateGlobals.bind(this)); - this.channel.on(Events.UPDATE_STORY_ARGS, this.onUpdateArgs.bind(this)); - this.channel.on(Events.RESET_STORY_ARGS, this.onResetArgs.bind(this)); - this.channel.on(Events.FORCE_RE_RENDER, this.onForceReRender.bind(this)); - this.channel.on(Events.FORCE_REMOUNT, this.onForceRemount.bind(this)); + this.channel.on(UPDATE_GLOBALS, this.onUpdateGlobals.bind(this)); + this.channel.on(UPDATE_STORY_ARGS, this.onUpdateArgs.bind(this)); + this.channel.on(RESET_STORY_ARGS, this.onResetArgs.bind(this)); + this.channel.on(FORCE_RE_RENDER, this.onForceReRender.bind(this)); + this.channel.on(FORCE_REMOUNT, this.onForceRemount.bind(this)); } getProjectAnnotationsOrRenderError( @@ -144,7 +155,7 @@ export class Preview { } emitGlobals() { - this.channel.emit(Events.SET_GLOBALS, { + this.channel.emit(SET_GLOBALS, { globals: this.storyStore.globals.get() || {}, globalTypes: this.storyStore.projectAnnotations.globalTypes || {}, }); @@ -227,7 +238,7 @@ export class Preview { await Promise.all(this.storyRenders.map((r) => r.rerender())); - this.channel.emit(Events.GLOBALS_UPDATED, { + this.channel.emit(GLOBALS_UPDATED, { globals: this.storyStore.globals.get(), initialGlobals: this.storyStore.globals.initialGlobals, }); @@ -238,7 +249,7 @@ export class Preview { await Promise.all(this.storyRenders.filter((r) => r.id === storyId).map((r) => r.rerender())); - this.channel.emit(Events.STORY_ARGS_UPDATED, { + this.channel.emit(STORY_ARGS_UPDATED, { storyId, args: this.storyStore.args.get(storyId), }); @@ -344,6 +355,6 @@ export class Preview { this.previewEntryError = err; logger.error(reason); logger.error(err); - this.channel.emit(Events.CONFIG_ERROR, err); + this.channel.emit(CONFIG_ERROR, err); } } diff --git a/lib/preview-web/src/PreviewWeb.integration.test.ts b/lib/preview-web/src/PreviewWeb.integration.test.ts index 2e452c167c45..34b7434b8b26 100644 --- a/lib/preview-web/src/PreviewWeb.integration.test.ts +++ b/lib/preview-web/src/PreviewWeb.integration.test.ts @@ -76,8 +76,9 @@ describe('PreviewWeb', () => { const preview = new PreviewWeb(); const docsRoot = window.document.createElement('div'); - // @ts-ignore - preview.view.prepareForDocs.mockReturnValue(docsRoot); + ( + preview.view.prepareForDocs as any as jest.Mock + ).mockReturnValue(docsRoot); componentOneExports.default.parameters.docs.container.mockImplementationOnce(() => React.createElement('div', {}, 'INSIDE') ); diff --git a/lib/preview-web/src/PreviewWeb.mockdata.ts b/lib/preview-web/src/PreviewWeb.mockdata.ts index d33563686c1c..ae2eadf8c707 100644 --- a/lib/preview-web/src/PreviewWeb.mockdata.ts +++ b/lib/preview-web/src/PreviewWeb.mockdata.ts @@ -1,5 +1,12 @@ import { EventEmitter } from 'events'; -import Events from '@storybook/core-events'; +import { + DOCS_RENDERED, + STORY_ERRORED, + STORY_MISSING, + STORY_RENDERED, + STORY_RENDER_PHASE_CHANGED, + STORY_THREW_EXCEPTION, +} from '@storybook/core-events'; import { StoryIndex } from '@storybook/store'; import { RenderPhase } from './PreviewWeb'; @@ -99,15 +106,15 @@ export const waitForEvents = ( // the async parts, so we need to listen for the "done" events export const waitForRender = () => waitForEvents([ - Events.STORY_RENDERED, - Events.DOCS_RENDERED, - Events.STORY_THREW_EXCEPTION, - Events.STORY_ERRORED, - Events.STORY_MISSING, + STORY_RENDERED, + DOCS_RENDERED, + STORY_THREW_EXCEPTION, + STORY_ERRORED, + STORY_MISSING, ]); export const waitForRenderPhase = (phase: RenderPhase) => - waitForEvents([Events.STORY_RENDER_PHASE_CHANGED], ({ newPhase }) => newPhase === phase); + waitForEvents([STORY_RENDER_PHASE_CHANGED], ({ newPhase }) => newPhase === phase); // A little trick to ensure that we always call the real `setTimeout` even when timers are mocked const realSetTimeout = setTimeout; diff --git a/lib/preview-web/src/PreviewWeb.test.ts b/lib/preview-web/src/PreviewWeb.test.ts index ba915bbf1bc7..0b507fb35f85 100644 --- a/lib/preview-web/src/PreviewWeb.test.ts +++ b/lib/preview-web/src/PreviewWeb.test.ts @@ -1,7 +1,30 @@ import global from 'global'; import * as ReactDOM from 'react-dom'; import merge from 'lodash/merge'; -import Events, { IGNORED_EXCEPTION } from '@storybook/core-events'; +import { + CONFIG_ERROR, + CURRENT_STORY_WAS_SET, + DOCS_RENDERED, + FORCE_REMOUNT, + FORCE_RE_RENDER, + GLOBALS_UPDATED, + IGNORED_EXCEPTION, + PREVIEW_KEYDOWN, + RESET_STORY_ARGS, + SET_CURRENT_STORY, + SET_GLOBALS, + STORY_ARGS_UPDATED, + STORY_CHANGED, + STORY_ERRORED, + STORY_MISSING, + STORY_PREPARED, + STORY_RENDERED, + STORY_SPECIFIED, + STORY_THREW_EXCEPTION, + STORY_UNCHANGED, + UPDATE_GLOBALS, + UPDATE_STORY_ARGS, +} from '@storybook/core-events'; import { logger } from '@storybook/client-logger'; import { addons, mockChannel as createMockChannel } from '@storybook/addons'; import type { AnyFramework } from '@storybook/csf'; @@ -100,10 +123,10 @@ beforeEach(() => { projectAnnotations.renderToDOM.mockReset(); projectAnnotations.render.mockClear(); projectAnnotations.decorators[0].mockClear(); - // @ts-ignore - ReactDOM.render.mockReset().mockImplementation((_: any, _2: any, cb: () => any) => cb()); - // @ts-ignore - logger.warn.mockClear(); + (ReactDOM.render as any as jest.Mock) + .mockReset() + .mockImplementation((_: any, _2: any, cb: () => any) => cb()); + (logger.warn as jest.Mock).mockClear(); mockStoryIndex.mockReset().mockReturnValue(storyIndex); addons.setChannel(mockChannel as any); @@ -126,7 +149,7 @@ describe('PreviewWeb', () => { ).rejects.toThrow(err); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.CONFIG_ERROR, err); + expect(mockChannel.emit).toHaveBeenCalledWith(CONFIG_ERROR, err); }); it('shows an error if the stories.json endpoint 500s', async () => { @@ -139,7 +162,7 @@ describe('PreviewWeb', () => { ); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.CONFIG_ERROR, expect.any(Error)); + expect(mockChannel.emit).toHaveBeenCalledWith(CONFIG_ERROR, expect.any(Error)); }); it('sets globals from the URL', async () => { @@ -153,7 +176,7 @@ describe('PreviewWeb', () => { it('emits the SET_GLOBALS event', async () => { await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.SET_GLOBALS, { + expect(mockChannel.emit).toHaveBeenCalledWith(SET_GLOBALS, { globals: { a: 'b' }, globalTypes: {}, }); @@ -162,7 +185,7 @@ describe('PreviewWeb', () => { it('SET_GLOBALS sets globals and types even when undefined', async () => { await createAndRenderPreview({ getProjectAnnotations: () => ({ renderToDOM: jest.fn() }) }); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.SET_GLOBALS, { + expect(mockChannel.emit).toHaveBeenCalledWith(SET_GLOBALS, { globals: {}, globalTypes: {}, }); @@ -173,7 +196,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.SET_GLOBALS, { + expect(mockChannel.emit).toHaveBeenCalledWith(SET_GLOBALS, { globals: { a: 'c' }, globalTypes: {}, }); @@ -193,7 +216,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'url' }, }); @@ -234,7 +257,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_SPECIFIED, { + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_SPECIFIED, { storyId: 'component-one--a', viewMode: 'story', }); @@ -245,7 +268,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.CURRENT_STORY_WAS_SET, { + expect(mockChannel.emit).toHaveBeenCalledWith(CURRENT_STORY_WAS_SET, { storyId: 'component-one--a', viewMode: 'story', }); @@ -258,7 +281,7 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_MISSING, 'random'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_MISSING, 'random'); }); it('tries again with a specifier if CSF file changes', async () => { @@ -267,7 +290,7 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_MISSING, 'component-one--d'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_MISSING, 'component-one--d'); mockChannel.emit.mockClear(); const newComponentOneExports = merge({}, componentOneExports, { @@ -295,7 +318,7 @@ describe('PreviewWeb', () => { }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_SPECIFIED, { + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_SPECIFIED, { storyId: 'component-one--d', viewMode: 'story', }); @@ -311,9 +334,9 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_MISSING, 'component-one--d'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_MISSING, 'component-one--d'); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -343,7 +366,7 @@ describe('PreviewWeb', () => { }, }, }); - expect(mockChannel.emit).not.toHaveBeenCalledWith(Events.STORY_SPECIFIED, { + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_SPECIFIED, { storyId: 'component-one--d', viewMode: 'story', }); @@ -355,7 +378,7 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); expect(preview.view.showNoPreview).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_MISSING); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_MISSING); }); describe('in story viewMode', () => { @@ -375,7 +398,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_PREPARED, { + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_PREPARED, { id: 'component-one--a', parameters: { __isArgsStory: false, @@ -441,7 +464,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -454,7 +477,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -490,7 +513,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -503,7 +526,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ERRORED, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ERRORED, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith({ message: error.title, stack: error.description, @@ -519,7 +542,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -534,7 +557,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); it('does not show error display if the render function throws IGNORED_EXCEPTION', async () => { @@ -548,10 +571,7 @@ describe('PreviewWeb', () => { await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith( - Events.STORY_THREW_EXCEPTION, - IGNORED_EXCEPTION - ); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, IGNORED_EXCEPTION); expect(preview.view.showErrorDisplay).not.toHaveBeenCalled(); }); }); @@ -568,7 +588,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a&viewMode=docs'; await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_PREPARED, { + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_PREPARED, { id: 'component-one--a', parameters: { __isArgsStory: false, @@ -607,7 +627,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.DOCS_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(DOCS_RENDERED, 'component-one--a'); }); }); }); @@ -617,10 +637,10 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; await createAndRenderPreview(); - emitter.emit(Events.UPDATE_GLOBALS, { globals: { foo: 'bar' } }); + emitter.emit(UPDATE_GLOBALS, { globals: { foo: 'bar' } }); - await waitForEvents([Events.GLOBALS_UPDATED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.GLOBALS_UPDATED, { + await waitForEvents([GLOBALS_UPDATED]); + expect(mockChannel.emit).toHaveBeenCalledWith(GLOBALS_UPDATED, { globals: { a: 'b', foo: 'bar' }, initialGlobals: { a: 'b' }, }); @@ -630,7 +650,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - emitter.emit(Events.UPDATE_GLOBALS, { globals: { foo: 'bar' } }); + emitter.emit(UPDATE_GLOBALS, { globals: { foo: 'bar' } }); expect(preview.storyStore.globals.get()).toEqual({ a: 'b', foo: 'bar' }); }); @@ -641,7 +661,7 @@ describe('PreviewWeb', () => { mockChannel.emit.mockClear(); projectAnnotations.renderToDOM.mockClear(); - emitter.emit(Events.UPDATE_GLOBALS, { globals: { foo: 'bar' } }); + emitter.emit(UPDATE_GLOBALS, { globals: { foo: 'bar' } }); await waitForRender(); expect(projectAnnotations.renderToDOM).toHaveBeenCalledWith( @@ -660,10 +680,10 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_GLOBALS, { globals: { foo: 'bar' } }); + emitter.emit(UPDATE_GLOBALS, { globals: { foo: 'bar' } }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); describe('in docs mode', () => { @@ -673,7 +693,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_GLOBALS, { globals: { foo: 'bar' } }); + emitter.emit(UPDATE_GLOBALS, { globals: { foo: 'bar' } }); await waitForRender(); expect(ReactDOM.render).toHaveBeenCalledTimes(2); @@ -686,13 +706,13 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; await createAndRenderPreview(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); - await waitForEvents([Events.STORY_ARGS_UPDATED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + await waitForEvents([STORY_ARGS_UPDATED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'a', new: 'arg' }, }); @@ -702,7 +722,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); @@ -719,7 +739,7 @@ describe('PreviewWeb', () => { mockChannel.emit.mockClear(); projectAnnotations.renderToDOM.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); @@ -742,13 +762,13 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); describe('while story is still rendering', () => { @@ -768,7 +788,7 @@ describe('PreviewWeb', () => { ); componentOneExports.default.loaders[0].mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); @@ -822,7 +842,7 @@ describe('PreviewWeb', () => { await new PreviewWeb().initialize({ importFn, getProjectAnnotations }); await waitForRenderPhase('rendering'); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); @@ -857,7 +877,7 @@ describe('PreviewWeb', () => { it('works if it is called directly from inside non async renderToDOM', async () => { document.location.search = '?id=component-one--a'; projectAnnotations.renderToDOM.mockImplementationOnce(() => { - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); @@ -914,7 +934,7 @@ describe('PreviewWeb', () => { undefined // this is coming from view.prepareForStory, not super important ); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); @@ -947,7 +967,7 @@ describe('PreviewWeb', () => { (ReactDOM.render as jest.MockedFunction).mockClear(); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); @@ -971,11 +991,11 @@ describe('PreviewWeb', () => { (ReactDOM.render as jest.MockedFunction).mockClear(); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); - await waitForEvents([Events.STORY_ARGS_UPDATED]); + await waitForEvents([STORY_ARGS_UPDATED]); expect(ReactDOM.render).not.toHaveBeenCalled(); }); @@ -1003,7 +1023,7 @@ describe('PreviewWeb', () => { (ReactDOM.render as jest.MockedFunction).mockClear(); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { new: 'arg' }, }); @@ -1040,26 +1060,26 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { foo: 'new' }, }); - await waitForEvents([Events.STORY_ARGS_UPDATED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + await waitForEvents([STORY_ARGS_UPDATED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'new' }, }); mockChannel.emit.mockClear(); - emitter.emit(Events.RESET_STORY_ARGS, { + emitter.emit(RESET_STORY_ARGS, { storyId: 'component-one--a', argNames: ['foo'], }); - await waitForEvents([Events.STORY_ARGS_UPDATED]); + await waitForEvents([STORY_ARGS_UPDATED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'a' }, }); @@ -1071,14 +1091,14 @@ describe('PreviewWeb', () => { const onUpdateArgsSpy = jest.spyOn(preview, 'onUpdateArgs'); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { foo: 'new', new: 'value' }, }); - await waitForEvents([Events.STORY_ARGS_UPDATED]); + await waitForEvents([STORY_ARGS_UPDATED]); mockChannel.emit.mockClear(); - emitter.emit(Events.RESET_STORY_ARGS, { + emitter.emit(RESET_STORY_ARGS, { storyId: 'component-one--a', argNames: ['foo'], }); @@ -1096,8 +1116,8 @@ describe('PreviewWeb', () => { undefined // this is coming from view.prepareForStory, not super important ); - await waitForEvents([Events.STORY_ARGS_UPDATED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + await waitForEvents([STORY_ARGS_UPDATED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'a', new: 'value' }, }); @@ -1113,14 +1133,14 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); const onUpdateArgsSpy = jest.spyOn(preview, 'onUpdateArgs'); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { foo: 'new' }, }); - await waitForEvents([Events.STORY_ARGS_UPDATED]); + await waitForEvents([STORY_ARGS_UPDATED]); mockChannel.emit.mockClear(); - emitter.emit(Events.RESET_STORY_ARGS, { + emitter.emit(RESET_STORY_ARGS, { storyId: 'component-one--a', }); @@ -1137,8 +1157,8 @@ describe('PreviewWeb', () => { undefined // this is coming from view.prepareForStory, not super important ); - await waitForEvents([Events.STORY_ARGS_UPDATED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + await waitForEvents([STORY_ARGS_UPDATED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'a' }, }); @@ -1154,14 +1174,14 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); const onUpdateArgsSpy = jest.spyOn(preview, 'onUpdateArgs'); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { foo: 'new', new: 'value' }, }); - await waitForEvents([Events.STORY_ARGS_UPDATED]); + await waitForEvents([STORY_ARGS_UPDATED]); mockChannel.emit.mockClear(); - emitter.emit(Events.RESET_STORY_ARGS, { + emitter.emit(RESET_STORY_ARGS, { storyId: 'component-one--a', }); @@ -1178,8 +1198,8 @@ describe('PreviewWeb', () => { undefined // this is coming from view.prepareForStory, not super important ); - await waitForEvents([Events.STORY_ARGS_UPDATED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + await waitForEvents([STORY_ARGS_UPDATED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'a' }, }); @@ -1195,14 +1215,14 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); const onUpdateArgsSpy = jest.spyOn(preview, 'onUpdateArgs'); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { foo: undefined }, }); - await waitForEvents([Events.STORY_ARGS_UPDATED]); + await waitForEvents([STORY_ARGS_UPDATED]); mockChannel.emit.mockClear(); - emitter.emit(Events.RESET_STORY_ARGS, { + emitter.emit(RESET_STORY_ARGS, { storyId: 'component-one--a', }); @@ -1219,8 +1239,8 @@ describe('PreviewWeb', () => { undefined // this is coming from view.prepareForStory, not super important ); - await waitForEvents([Events.STORY_ARGS_UPDATED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + await waitForEvents([STORY_ARGS_UPDATED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'a' }, }); @@ -1239,7 +1259,7 @@ describe('PreviewWeb', () => { mockChannel.emit.mockClear(); projectAnnotations.renderToDOM.mockClear(); - emitter.emit(Events.FORCE_RE_RENDER); + emitter.emit(FORCE_RE_RENDER); await waitForRender(); expect(projectAnnotations.renderToDOM).toHaveBeenCalledWith( @@ -1263,7 +1283,7 @@ describe('PreviewWeb', () => { mockChannel.emit.mockClear(); projectAnnotations.renderToDOM.mockClear(); - emitter.emit(Events.FORCE_REMOUNT, { storyId: 'component-one--a' }); + emitter.emit(FORCE_REMOUNT, { storyId: 'component-one--a' }); await waitForRender(); expect(projectAnnotations.renderToDOM).toHaveBeenCalledWith( @@ -1292,7 +1312,7 @@ describe('PreviewWeb', () => { ); mockChannel.emit.mockClear(); - emitter.emit(Events.FORCE_REMOUNT, { storyId: 'component-one--a' }); + emitter.emit(FORCE_REMOUNT, { storyId: 'component-one--a' }); await waitForSetCurrentStory(); // Now let the renderToDOM call resolve @@ -1307,7 +1327,7 @@ describe('PreviewWeb', () => { expect(componentOneExports.a.play).toHaveBeenCalledTimes(1); await waitForRenderPhase('completed'); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); await waitForQuiescence(); }); @@ -1325,7 +1345,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; await createAndRenderPreview(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1342,13 +1362,13 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; await createAndRenderPreview(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); await waitForSetCurrentStory(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.CURRENT_STORY_WAS_SET, { + expect(mockChannel.emit).toHaveBeenCalledWith(CURRENT_STORY_WAS_SET, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1358,15 +1378,15 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'random', viewMode: 'story', }); await waitForSetCurrentStory(); - await waitForEvents([Events.STORY_MISSING]); + await waitForEvents([STORY_MISSING]); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_MISSING, 'random'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_MISSING, 'random'); }); describe('if called before the preview is initialized', () => { @@ -1375,13 +1395,13 @@ describe('PreviewWeb', () => { // We intentionally are *not* awaiting here new PreviewWeb().initialize({ importFn, getProjectAnnotations }); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); - await waitForEvents([Events.STORY_RENDERED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.CURRENT_STORY_WAS_SET, { + await waitForEvents([STORY_RENDERED]); + expect(mockChannel.emit).toHaveBeenCalledWith(CURRENT_STORY_WAS_SET, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1390,8 +1410,8 @@ describe('PreviewWeb', () => { '', 'pathname?id=component-one--b&viewMode=story' ); - expect(mockChannel.emit).not.toHaveBeenCalledWith(Events.STORY_MISSING, 'component-one--b'); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--b'); + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_MISSING, 'component-one--b'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--b'); }); }); @@ -1400,14 +1420,14 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; await createAndRenderPreview(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); await waitForSetCurrentStory(); - await waitForEvents([Events.STORY_UNCHANGED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_UNCHANGED, 'component-one--a'); + await waitForEvents([STORY_UNCHANGED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_UNCHANGED, 'component-one--a'); }); it('does NOT call renderToDOM', async () => { @@ -1415,7 +1435,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); projectAnnotations.renderToDOM.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -1448,11 +1468,11 @@ describe('PreviewWeb', () => { // We can't wait for the initialize function, as it waits for `renderSelection()` // which prepares, but it does emit `CURRENT_STORY_WAS_SET` right before that preview.initialize({ importFn, getProjectAnnotations }); - await waitForEvents([Events.CURRENT_STORY_WAS_SET]); + await waitForEvents([CURRENT_STORY_WAS_SET]); mockChannel.emit.mockClear(); projectAnnotations.renderToDOM.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -1477,7 +1497,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; await createAndRenderPreview(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1494,7 +1514,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1508,14 +1528,14 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); await waitForSetCurrentStory(); - await waitForEvents([Events.STORY_CHANGED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_CHANGED, 'component-one--b'); + await waitForEvents([STORY_CHANGED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_CHANGED, 'component-one--b'); }); it('emits STORY_PREPARED', async () => { @@ -1523,14 +1543,14 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); await waitForSetCurrentStory(); - await waitForEvents([Events.STORY_PREPARED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_PREPARED, { + await waitForEvents([STORY_PREPARED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_PREPARED, { id: 'component-one--b', parameters: { __isArgsStory: false, @@ -1548,7 +1568,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1575,7 +1595,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1613,14 +1633,14 @@ describe('PreviewWeb', () => { }); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); await waitForSetCurrentStory(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -1634,14 +1654,14 @@ describe('PreviewWeb', () => { ); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); await waitForSetCurrentStory(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ERRORED, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ERRORED, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith({ message: error.title, stack: error.description, @@ -1658,14 +1678,14 @@ describe('PreviewWeb', () => { ); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); await waitForSetCurrentStory(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -1674,7 +1694,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1689,14 +1709,14 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); await waitForSetCurrentStory(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--b'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--b'); }); it('retains any arg changes', async () => { @@ -1704,7 +1724,7 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { foo: 'updated' }, }); @@ -1714,7 +1734,7 @@ describe('PreviewWeb', () => { }); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1725,7 +1745,7 @@ describe('PreviewWeb', () => { }); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -1745,7 +1765,7 @@ describe('PreviewWeb', () => { await new PreviewWeb().initialize({ importFn, getProjectAnnotations }); await waitForRenderPhase('loading'); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1779,7 +1799,7 @@ describe('PreviewWeb', () => { await waitForRenderPhase('rendering'); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1798,11 +1818,8 @@ describe('PreviewWeb', () => { expect(componentOneExports.b.play).toHaveBeenCalled(); await waitForRenderPhase('completed'); - expect(mockChannel.emit).not.toHaveBeenCalledWith( - Events.STORY_RENDERED, - 'component-one--a' - ); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--b'); + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--b'); await waitForQuiescence(); }); @@ -1827,7 +1844,7 @@ describe('PreviewWeb', () => { ); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1839,7 +1856,7 @@ describe('PreviewWeb', () => { await waitForSetCurrentStory(); await waitForRenderPhase('rendering'); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_CHANGED, 'component-one--b'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_CHANGED, 'component-one--b'); expect(projectAnnotations.renderToDOM).toHaveBeenCalledWith( expect.objectContaining({ forceRemount: true, @@ -1853,14 +1870,11 @@ describe('PreviewWeb', () => { await waitForRenderPhase('playing'); await waitForRenderPhase('completed'); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--b'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--b'); // Final story rendered is not emitted for the first story await waitForQuiescence(); - expect(mockChannel.emit).not.toHaveBeenCalledWith( - Events.STORY_RENDERED, - 'component-one--a' - ); + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); it('reloads page if playFunction fails to abort in time', async () => { @@ -1883,7 +1897,7 @@ describe('PreviewWeb', () => { ); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -1894,10 +1908,7 @@ describe('PreviewWeb', () => { await waitForSetCurrentStory(); expect(global.window.location.reload).toHaveBeenCalled(); - expect(mockChannel.emit).not.toHaveBeenCalledWith( - Events.STORY_CHANGED, - 'component-one--b' - ); + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_CHANGED, 'component-one--b'); expect(projectAnnotations.renderToDOM).not.toHaveBeenCalledWith( expect.objectContaining({ storyContext: expect.objectContaining({ id: 'component-one--b' }), @@ -1913,7 +1924,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; await createAndRenderPreview(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'docs', }); @@ -1930,7 +1941,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a'; const preview = await createAndRenderPreview(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'docs', }); @@ -1944,14 +1955,14 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'docs', }); await waitForSetCurrentStory(); - await waitForEvents([Events.STORY_CHANGED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_CHANGED, 'component-one--a'); + await waitForEvents([STORY_CHANGED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_CHANGED, 'component-one--a'); }); it('calls view.prepareForDocs', async () => { @@ -1959,7 +1970,7 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'docs', }); @@ -1974,7 +1985,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'docs', }); @@ -2002,14 +2013,14 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'docs', }); await waitForSetCurrentStory(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.DOCS_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(DOCS_RENDERED, 'component-one--a'); }); }); @@ -2018,7 +2029,7 @@ describe('PreviewWeb', () => { document.location.search = '?id=component-one--a&viewMode=docs'; await createAndRenderPreview(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -2036,7 +2047,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -2052,14 +2063,14 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); await waitForSetCurrentStory(); - await waitForEvents([Events.STORY_CHANGED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_CHANGED, 'component-one--a'); + await waitForEvents([STORY_CHANGED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_CHANGED, 'component-one--a'); }); it('calls view.prepareForStory', async () => { @@ -2067,7 +2078,7 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -2086,14 +2097,14 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); await waitForSetCurrentStory(); - await waitForEvents([Events.STORY_PREPARED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_PREPARED, { + await waitForEvents([STORY_PREPARED]); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_PREPARED, { id: 'component-one--a', parameters: { __isArgsStory: false, @@ -2111,7 +2122,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -2138,7 +2149,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -2176,14 +2187,14 @@ describe('PreviewWeb', () => { }); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); await waitForSetCurrentStory(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -2197,14 +2208,14 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); await waitForSetCurrentStory(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ERRORED, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ERRORED, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith({ message: error.title, stack: error.description, @@ -2221,14 +2232,14 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); await waitForSetCurrentStory(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -2237,7 +2248,7 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -2252,14 +2263,14 @@ describe('PreviewWeb', () => { await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); await waitForSetCurrentStory(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); }); }); @@ -2277,13 +2288,13 @@ describe('PreviewWeb', () => { ); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.CONFIG_ERROR, expect.any(Error)); + expect(mockChannel.emit).toHaveBeenCalledWith(CONFIG_ERROR, expect.any(Error)); mockChannel.emit.mockClear(); mockFetchResult = { status: 200, json: mockStoryIndex, text: () => 'error text' }; preview.onStoryIndexChanged(); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); it('sets story args from the URL', async () => { @@ -2297,7 +2308,7 @@ describe('PreviewWeb', () => { ); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.CONFIG_ERROR, expect.any(Error)); + expect(mockChannel.emit).toHaveBeenCalledWith(CONFIG_ERROR, expect.any(Error)); mockChannel.emit.mockClear(); mockFetchResult = { status: 200, json: mockStoryIndex, text: () => 'error text' }; @@ -2327,10 +2338,7 @@ describe('PreviewWeb', () => { preview.onStoriesChanged({ importFn: newImportFn }); await waitForRender(); - expect(mockChannel.emit).not.toHaveBeenCalledWith( - Events.STORY_UNCHANGED, - 'component-one--a' - ); + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_UNCHANGED, 'component-one--a'); }); it('does not emit STORY_CHANGED', async () => { @@ -2341,7 +2349,7 @@ describe('PreviewWeb', () => { preview.onStoriesChanged({ importFn: newImportFn }); await waitForRender(); - expect(mockChannel.emit).not.toHaveBeenCalledWith(Events.STORY_CHANGED, 'component-one--a'); + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_CHANGED, 'component-one--a'); }); it('emits STORY_PREPARED with new annotations', async () => { @@ -2352,7 +2360,7 @@ describe('PreviewWeb', () => { preview.onStoriesChanged({ importFn: newImportFn }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_PREPARED, { + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_PREPARED, { id: 'component-one--a', parameters: { __isArgsStory: false, @@ -2373,7 +2381,7 @@ describe('PreviewWeb', () => { preview.onStoriesChanged({ importFn: newImportFn }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'edited' }, }); @@ -2438,7 +2446,7 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { foo: 'updated' }, }); @@ -2474,7 +2482,7 @@ describe('PreviewWeb', () => { preview.onStoriesChanged({ importFn: newImportFn }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -2491,7 +2499,7 @@ describe('PreviewWeb', () => { preview.onStoriesChanged({ importFn: newImportFn }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ERRORED, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ERRORED, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith({ message: error.title, stack: error.description, @@ -2511,7 +2519,7 @@ describe('PreviewWeb', () => { preview.onStoriesChanged({ importFn: newImportFn }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, error); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_THREW_EXCEPTION, error); expect(preview.view.showErrorDisplay).toHaveBeenCalledWith(error); }); @@ -2535,7 +2543,7 @@ describe('PreviewWeb', () => { preview.onStoriesChanged({ importFn: newImportFn }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); }); @@ -2575,7 +2583,7 @@ describe('PreviewWeb', () => { const preview = await createAndRenderPreview(); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -2585,7 +2593,7 @@ describe('PreviewWeb', () => { preview.onStoriesChanged({ importFn: newImportFn, storyIndex: newStoryIndex }); mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -2610,10 +2618,10 @@ describe('PreviewWeb', () => { mockChannel.emit.mockClear(); preview.onStoriesChanged({ importFn: newImportFn }); - await waitForEvents([Events.STORY_UNCHANGED]); + await waitForEvents([STORY_UNCHANGED]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_UNCHANGED, 'component-one--a'); - expect(mockChannel.emit).not.toHaveBeenCalledWith(Events.STORY_CHANGED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_UNCHANGED, 'component-one--a'); + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_CHANGED, 'component-one--a'); }); it('clears preparing state', async () => { @@ -2623,7 +2631,7 @@ describe('PreviewWeb', () => { (preview.view.showMain as jest.Mock).mockClear(); mockChannel.emit.mockClear(); preview.onStoriesChanged({ importFn: newImportFn }); - await waitForEvents([Events.STORY_UNCHANGED]); + await waitForEvents([STORY_UNCHANGED]); expect(preview.view.showMain).toHaveBeenCalled(); }); @@ -2638,10 +2646,7 @@ describe('PreviewWeb', () => { await waitForQuiescence(); expect(projectAnnotations.renderToDOM).not.toHaveBeenCalled(); - expect(mockChannel.emit).not.toHaveBeenCalledWith( - Events.STORY_RENDERED, - 'component-one--a' - ); + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); }); @@ -2667,7 +2672,7 @@ describe('PreviewWeb', () => { // Change A's args mockChannel.emit.mockClear(); - emitter.emit(Events.UPDATE_STORY_ARGS, { + emitter.emit(UPDATE_STORY_ARGS, { storyId: 'component-one--a', updatedArgs: { foo: 'updated' }, }); @@ -2675,7 +2680,7 @@ describe('PreviewWeb', () => { // Change to story B mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--b', viewMode: 'story', }); @@ -2693,7 +2698,7 @@ describe('PreviewWeb', () => { // Change back to Story A mockChannel.emit.mockClear(); - emitter.emit(Events.SET_CURRENT_STORY, { + emitter.emit(SET_CURRENT_STORY, { storyId: 'component-one--a', viewMode: 'story', }); @@ -2738,10 +2743,10 @@ describe('PreviewWeb', () => { mockChannel.emit.mockClear(); preview.onStoriesChanged({ importFn: newImportFn, storyIndex: newStoryIndex }); - await waitForEvents([Events.STORY_MISSING]); + await waitForEvents([STORY_MISSING]); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_MISSING, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_MISSING, 'component-one--a'); }); it('does not re-render the story', async () => { @@ -2754,10 +2759,7 @@ describe('PreviewWeb', () => { await waitForQuiescence(); expect(projectAnnotations.renderToDOM).not.toHaveBeenCalled(); - expect(mockChannel.emit).not.toHaveBeenCalledWith( - Events.STORY_RENDERED, - 'component-one--a' - ); + expect(mockChannel.emit).not.toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); it('re-renders the story if it is readded', async () => { @@ -2766,12 +2768,12 @@ describe('PreviewWeb', () => { mockChannel.emit.mockClear(); preview.onStoriesChanged({ importFn: newImportFn, storyIndex: newStoryIndex }); - await waitForEvents([Events.STORY_MISSING]); + await waitForEvents([STORY_MISSING]); mockChannel.emit.mockClear(); preview.onStoriesChanged({ importFn, storyIndex }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); }); }); @@ -2795,7 +2797,7 @@ describe('PreviewWeb', () => { preview.onGetProjectAnnotationsChanged({ getProjectAnnotations }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_RENDERED, 'component-one--a'); + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_RENDERED, 'component-one--a'); }); it('sets globals from the URL', async () => { @@ -2834,7 +2836,7 @@ describe('PreviewWeb', () => { ).rejects.toThrow(err); expect(preview.view.showErrorDisplay).toHaveBeenCalled(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.CONFIG_ERROR, err); + expect(mockChannel.emit).toHaveBeenCalledWith(CONFIG_ERROR, err); }); const newGlobalDecorator = jest.fn((s) => s()); @@ -2866,8 +2868,8 @@ describe('PreviewWeb', () => { preview.onGetProjectAnnotationsChanged({ getProjectAnnotations: newGetProjectAnnotations }); await waitForRender(); - await waitForEvents([Events.SET_GLOBALS]); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.SET_GLOBALS, { + await waitForEvents([SET_GLOBALS]); + expect(mockChannel.emit).toHaveBeenCalledWith(SET_GLOBALS, { globals: { a: 'edited' }, globalTypes: {}, }); @@ -2895,7 +2897,7 @@ describe('PreviewWeb', () => { preview.onGetProjectAnnotationsChanged({ getProjectAnnotations: newGetProjectAnnotations }); await waitForRender(); - expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, { + expect(mockChannel.emit).toHaveBeenCalledWith(STORY_ARGS_UPDATED, { storyId: 'component-one--a', args: { foo: 'a', global: 'added' }, }); @@ -2931,10 +2933,7 @@ describe('PreviewWeb', () => { target: { tagName: 'div', getAttribute: jest.fn().mockReturnValue(null) }, } as any); - expect(mockChannel.emit).toHaveBeenCalledWith( - Events.PREVIEW_KEYDOWN, - expect.objectContaining({}) - ); + expect(mockChannel.emit).toHaveBeenCalledWith(PREVIEW_KEYDOWN, expect.objectContaining({})); }); it('does not emit PREVIEW_KEYDOWN for input elements', async () => { @@ -2946,7 +2945,7 @@ describe('PreviewWeb', () => { } as any); expect(mockChannel.emit).not.toHaveBeenCalledWith( - Events.PREVIEW_KEYDOWN, + PREVIEW_KEYDOWN, expect.objectContaining({}) ); }); diff --git a/lib/preview-web/src/PreviewWeb.tsx b/lib/preview-web/src/PreviewWeb.tsx index 417730034bd3..e4fdb3c2d574 100644 --- a/lib/preview-web/src/PreviewWeb.tsx +++ b/lib/preview-web/src/PreviewWeb.tsx @@ -1,7 +1,24 @@ import deprecate from 'util-deprecate'; import dedent from 'ts-dedent'; import global from 'global'; -import Events, { IGNORED_EXCEPTION } from '@storybook/core-events'; +import { + CURRENT_STORY_WAS_SET, + IGNORED_EXCEPTION, + PRELOAD_STORIES, + PREVIEW_KEYDOWN, + SET_CURRENT_STORY, + SET_STORIES, + STORY_ARGS_UPDATED, + STORY_CHANGED, + STORY_ERRORED, + STORY_MISSING, + STORY_PREPARED, + STORY_RENDER_PHASE_CHANGED, + STORY_SPECIFIED, + STORY_THREW_EXCEPTION, + STORY_UNCHANGED, + UPDATE_QUERY_PARAMS, +} from '@storybook/core-events'; import { logger } from '@storybook/client-logger'; import { AnyFramework, StoryId, ProjectAnnotations, Args, Globals } from '@storybook/csf'; import type { @@ -64,9 +81,9 @@ export class PreviewWeb extends Preview) { @@ -87,7 +104,7 @@ export class PreviewWeb extends Preview { return super.initializeWithStoryIndex(storyIndex).then(() => { if (!global.FEATURES?.storyStoreV7) { - this.channel.emit(Events.SET_STORIES, this.storyStore.getSetStoriesPayload()); + this.channel.emit(SET_STORIES, this.storyStore.getSetStoriesPayload()); } return this.selectSpecifiedStory(); @@ -130,9 +147,9 @@ export class PreviewWeb extends Preview extends Preview extends Preview extends Preview extends Preview extends Preview extends Preview extends Preview extends Preview