Skip to content

Commit

Permalink
Fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed May 6, 2022
1 parent 958f7be commit d96e1a1
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 32 deletions.
33 changes: 21 additions & 12 deletions addons/docs/src/blocks/DocsRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import React, { ComponentType, ReactElement } from 'react';
import ReactDOM from 'react-dom';
import { AnyFramework, Parameters } from '@storybook/csf';
import { DocsRenderFunction } from '@storybook/preview-web';

import { DocsContainer } from './DocsContainer';
import { DocsPage } from './DocsPage';

import { DocsContext, DocsContextProps } from './DocsContext';

export function renderDocs<TFramework extends AnyFramework>(
docsContext: DocsContextProps<TFramework>,
docsParameters: Parameters,
element: HTMLElement,
callback: () => void
): void {
renderDocsAsync(docsContext, docsParameters, element).then(callback);
export class DocsRenderer<TFramework extends AnyFramework> {
public render: DocsRenderFunction<TFramework>;

public unmount: (element: HTMLElement) => void;

constructor() {
this.render = (
docsContext: DocsContextProps<TFramework>,
docsParameters: Parameters,
element: HTMLElement,
callback: () => void
): void => {
renderDocsAsync(docsContext, docsParameters, element).then(callback);
};

this.unmount = (element: HTMLElement) => {
ReactDOM.unmountComponentAtNode(element);
};
}
}

async function renderDocsAsync<TFramework extends AnyFramework>(
Expand Down Expand Up @@ -43,7 +56,3 @@ async function renderDocsAsync<TFramework extends AnyFramework>(
ReactDOM.render(docsElement, element, resolve);
});
}

export function unmountDocs(element: HTMLElement) {
ReactDOM.unmountComponentAtNode(element);
}
1 change: 1 addition & 0 deletions addons/docs/src/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './Description';
export * from './DocsContext';
export * from './DocsPage';
export * from './DocsContainer';
export * from './DocsRenderer'; // For testing
export * from './DocsStory';
export * from './Heading';
export * from './Meta';
Expand Down
5 changes: 2 additions & 3 deletions addons/docs/src/preview.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
export const parameters = {
docs: {
renderer: async () => {
const x = await import('./blocks/DocsRenderer');
console.log(x);
return x;
const { DocsRenderer } = await import('./blocks/DocsRenderer');
return new DocsRenderer();
},
},
};
5 changes: 2 additions & 3 deletions lib/preview-web/src/DocsRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class DocsRender<TFramework extends AnyFramework> implements Render<TFram
}

const renderer = await docs.renderer();
(renderer.renderDocs as DocsRenderFunction<TFramework>)(
(renderer.render as DocsRenderFunction<TFramework>)(
this.docsContext,
{
...docs,
Expand All @@ -155,8 +155,7 @@ export class DocsRender<TFramework extends AnyFramework> implements Render<TFram
);
this.teardown = async ({ viewModeChanged }: { viewModeChanged?: boolean } = {}) => {
if (!viewModeChanged || !this.canvasElement) return;
// TODO type
renderer.unmountDocs(this.canvasElement);
renderer.unmount(this.canvasElement);
};
}

Expand Down
3 changes: 3 additions & 0 deletions lib/preview-web/src/PreviewWeb.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import global from 'global';
import { RenderContext } from '@storybook/store';
import addons, { mockChannel as createMockChannel } from '@storybook/addons';
import { DocsRenderer } from '@storybook/addon-docs';

import { PreviewWeb } from './PreviewWeb';
import {
Expand All @@ -13,6 +14,7 @@ import {
mockChannel,
waitForRender,
storyIndex as mockStoryIndex,
docsRenderer,
} from './PreviewWeb.mockdata';

// PreviewWeb.test mocks out all rendering
Expand Down Expand Up @@ -51,6 +53,7 @@ beforeEach(() => {
projectAnnotations.renderToDOM.mockReset();
projectAnnotations.render.mockClear();
projectAnnotations.decorators[0].mockClear();
projectAnnotations.parameters.docs.renderer = () => new DocsRenderer();

addons.setChannel(mockChannel as any);
addons.setServerChannel(createMockChannel());
Expand Down
4 changes: 2 additions & 2 deletions lib/preview-web/src/PreviewWeb.mockdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const importFn = jest.fn(
);

export const docsRenderer = {
renderDocs: jest.fn().mockImplementation((context, parameters, element, cb) => cb()),
unmountDocs: jest.fn(),
render: jest.fn().mockImplementation((context, parameters, element, cb) => cb()),
unmount: jest.fn(),
};
export const projectAnnotations = {
globals: { a: 'b' },
Expand Down
24 changes: 12 additions & 12 deletions lib/preview-web/src/PreviewWeb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ beforeEach(() => {
projectAnnotations.renderToDOM.mockReset();
projectAnnotations.render.mockClear();
projectAnnotations.decorators[0].mockClear();
docsRenderer.renderDocs.mockClear();
docsRenderer.render.mockClear();
// @ts-ignore
logger.warn.mockClear();
mockStoryIndex.mockReset().mockReturnValue(storyIndex);
Expand Down Expand Up @@ -588,7 +588,7 @@ describe('PreviewWeb', () => {

await createAndRenderPreview();

expect(docsRenderer.renderDocs).toHaveBeenCalledWith(
expect(docsRenderer.render).toHaveBeenCalledWith(
expect.objectContaining({
id: 'component-one--a',
title: 'Component One',
Expand Down Expand Up @@ -621,7 +621,7 @@ describe('PreviewWeb', () => {
document.location.search = '?id=legacy--docs&viewMode=docs';
await createAndRenderPreview();

expect(docsRenderer.renderDocs).toHaveBeenCalledWith(
expect(docsRenderer.render).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
page: legacyDocsExports.Docs.parameters.docs.page,
Expand All @@ -644,7 +644,7 @@ describe('PreviewWeb', () => {
document.location.search = '?id=introduction--docs&viewMode=docs';
await createAndRenderPreview();

expect(docsRenderer.renderDocs).toHaveBeenCalledWith(
expect(docsRenderer.render).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
page: modernDocsExports.default,
Expand Down Expand Up @@ -728,7 +728,7 @@ describe('PreviewWeb', () => {
emitter.emit(Events.UPDATE_GLOBALS, { globals: { foo: 'bar' } });
await waitForRender();

expect(docsRenderer.renderDocs).toHaveBeenCalledTimes(2);
expect(docsRenderer.render).toHaveBeenCalledTimes(2);
});
});
});
Expand Down Expand Up @@ -997,15 +997,15 @@ describe('PreviewWeb', () => {

await createAndRenderPreview();

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

expect(docsRenderer.renderDocs).toHaveBeenCalledTimes(1);
expect(docsRenderer.render).toHaveBeenCalledTimes(1);
});
});

Expand All @@ -1021,15 +1021,15 @@ describe('PreviewWeb', () => {

await createAndRenderPreview();

docsRenderer.renderDocs.mockClear();
docsRenderer.render.mockClear();
mockChannel.emit.mockClear();
emitter.emit(Events.UPDATE_STORY_ARGS, {
storyId: 'component-one--a',
updatedArgs: { new: 'arg' },
});
await waitForEvents([Events.STORY_ARGS_UPDATED]);

expect(docsRenderer.renderDocs).not.toHaveBeenCalled();
expect(docsRenderer.render).not.toHaveBeenCalled();
});

describe('when renderStoryToElement was called', () => {
Expand All @@ -1053,7 +1053,7 @@ describe('PreviewWeb', () => {
'story-element'
);

docsRenderer.renderDocs.mockClear();
docsRenderer.render.mockClear();
mockChannel.emit.mockClear();
emitter.emit(Events.UPDATE_STORY_ARGS, {
storyId: 'component-one--a',
Expand Down Expand Up @@ -1968,7 +1968,7 @@ describe('PreviewWeb', () => {
await waitForSetCurrentStory();
await waitForRender();

expect(docsRenderer.renderDocs).toHaveBeenCalledWith(
expect(docsRenderer.render).toHaveBeenCalledWith(
expect.objectContaining({
id: 'component-one--a',
title: 'Component One',
Expand Down Expand Up @@ -2026,7 +2026,7 @@ describe('PreviewWeb', () => {
await waitForSetCurrentStory();
await waitForRender();

expect(docsRenderer.unmountDocs).toHaveBeenCalled();
expect(docsRenderer.unmount).toHaveBeenCalled();
});

// NOTE: I am not sure this entirely makes sense but this is the behaviour from 6.3
Expand Down

0 comments on commit d96e1a1

Please sign in to comment.