From 128de1a552044e5fa43d1dba9daf461fb36ad508 Mon Sep 17 00:00:00 2001 From: Tom Crockett Date: Thu, 31 May 2018 10:25:30 -0700 Subject: [PATCH 1/2] [@storybook/addon-storyshots] Fill out missing initStoryshots options --- types/storybook__addon-storyshots/index.d.ts | 22 +++++++++++---- .../storybook__addon-storyshots-tests.ts | 28 ++++++++++++++++++- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/types/storybook__addon-storyshots/index.d.ts b/types/storybook__addon-storyshots/index.d.ts index cbece6e354..289efcf33c 100644 --- a/types/storybook__addon-storyshots/index.d.ts +++ b/types/storybook__addon-storyshots/index.d.ts @@ -11,11 +11,15 @@ import { Page, NavigationOptions, ScreenshotOptions } from "puppeteer"; export type Test = (options: { story: StoryObject; context: StoryContext; - renderShallowTree: any; - renderTree: any; + renderShallowTree: RenderTree; + renderTree: RenderTree; snapshotFileName: string; }) => void | undefined | Promise; +export interface RenderTree { + (story: StoryObject, context: StoryContext, options?: SnapshotOptions): void | undefined | Promise; +} + export interface SnapshotOptions { createNodeMock?: (element: any) => any; } @@ -54,10 +58,16 @@ export const renderOnly: Test; export function getSnapshotFileName(context: StoryContext): string; -export default function initStoryshots(options: { +export default function initStoryshots(options: InitOptions): void; + +export interface InitOptions { configPath?: string; - framework?: string; - integrityOptions?: {}; suite?: string; + storyKindRegex?: RegExp; + storyNameRegex?: RegExp; + framework?: string; test?: Test; -}): void; + renderer?: (node: React.ReactElement) => Rendered; + serializer?: (rendered: Rendered) => any; + integrityOptions?: {}; +} diff --git a/types/storybook__addon-storyshots/storybook__addon-storyshots-tests.ts b/types/storybook__addon-storyshots/storybook__addon-storyshots-tests.ts index a2bafa6a14..3d5eb2743a 100644 --- a/types/storybook__addon-storyshots/storybook__addon-storyshots-tests.ts +++ b/types/storybook__addon-storyshots/storybook__addon-storyshots-tests.ts @@ -1,5 +1,5 @@ import initStoryshots, { multiSnapshotWithOptions, snapshotWithOptions, getSnapshotFileName, renderOnly, imageSnapshot } from "@storybook/addon-storyshots"; -import { shallow } from 'enzyme'; +import { shallow, ShallowWrapper } from 'enzyme'; import toJson from 'enzyme-to-json'; import 'jest'; import 'jest-specific-snapshot'; @@ -55,3 +55,29 @@ initStoryshots({ } }) }); + +initStoryshots({ + configPath: 'config/storybook', + suite: 'storybook snapshots', + storyNameRegex: /^(?!NOTEST )/, + test: function renderWithoutSnapshotting({ story, context, renderTree }) { + const result = renderTree(story, context, { createNodeMock }); + const unmount = (tree: any) => typeof tree.unmount === 'function' && tree.unmount(); + return result instanceof Promise ? result.then(unmount) : unmount(result); + + function createNodeMock() { + return { + addEventListener: () => undefined, + getClientRects: () => [], + getBoundingClientRect: () => ({}), + getElementsByClassName: () => [], + }; + } + }, +}); + +// Ensure renderer is compatible with serializer +initStoryshots({ + renderer: shallow, + serializer: toJson, +}); From 74b29ce480d3cf430407456f49013e1cc850abd6 Mon Sep 17 00:00:00 2001 From: Tom Crockett Date: Thu, 31 May 2018 10:28:42 -0700 Subject: [PATCH 2/2] Fix lint --- types/storybook__addon-storyshots/index.d.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/types/storybook__addon-storyshots/index.d.ts b/types/storybook__addon-storyshots/index.d.ts index 289efcf33c..e703f374da 100644 --- a/types/storybook__addon-storyshots/index.d.ts +++ b/types/storybook__addon-storyshots/index.d.ts @@ -16,9 +16,11 @@ export type Test = (options: { snapshotFileName: string; }) => void | undefined | Promise; -export interface RenderTree { - (story: StoryObject, context: StoryContext, options?: SnapshotOptions): void | undefined | Promise; -} +export type RenderTree = ( + story: StoryObject, + context: StoryContext, + options?: SnapshotOptions +) => void | undefined | Promise; export interface SnapshotOptions { createNodeMock?: (element: any) => any; @@ -58,6 +60,7 @@ export const renderOnly: Test; export function getSnapshotFileName(context: StoryContext): string; +// tslint:disable-next-line no-unnecessary-generics export default function initStoryshots(options: InitOptions): void; export interface InitOptions {