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

Storyshots: Fix typings of "test"-method #12389

Merged
merged 2 commits into from Oct 5, 2020
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
20 changes: 19 additions & 1 deletion addons/storyshots/storyshots-core/src/api/StoryshotsOptions.ts
Expand Up @@ -3,6 +3,24 @@ import { Stories2SnapsConverter } from '../Stories2SnapsConverter';
import { SupportedFramework } from '../frameworks';
import { RenderTree } from '../frameworks/Loader';

export interface TestMethodOptions {
story: any;
context: any;
renderTree: RenderTree;
renderShallowTree: RenderTree;
stories2snapsConverter: Stories2SnapsConverter;
snapshotFileName: string;
options: any;
}

export interface StoryshotsTestMethod {
(args: TestMethodOptions): any;
beforeAll?: () => void | Promise<void>;
beforeEach?: () => void | Promise<void>;
afterAll?: () => void | Promise<void>;
afterEach?: () => void | Promise<void>;
}

export interface StoryshotsOptions {
asyncJest?: boolean;
config?: (options: any) => void;
Expand All @@ -12,7 +30,7 @@ export interface StoryshotsOptions {
storyKindRegex?: RegExp | string;
storyNameRegex?: RegExp | string;
framework?: SupportedFramework;
test?: (story: any, context: any, renderTree: RenderTree, options?: any) => any;
test?: StoryshotsTestMethod;
renderer?: Function;
snapshotSerializers?: jest.SnapshotSerializerPlugin[];
/**
Expand Down
128 changes: 50 additions & 78 deletions addons/storyshots/storyshots-core/src/test-bodies.ts
@@ -1,104 +1,76 @@
import 'jest-specific-snapshot';
import { RenderTree } from './frameworks/Loader';
import { Stories2SnapsConverter } from './Stories2SnapsConverter';
import { StoryshotsTestMethod, TestMethodOptions } from './api/StoryshotsOptions';

const isFunction = (obj: any) => !!(obj && obj.constructor && obj.call && obj.apply);
const optionsOrCallOptions = (opts: any, story: any) => (isFunction(opts) ? opts(story) : opts);

export const snapshotWithOptions = (
type SnapshotsWithOptionsReturnType = (
options: Pick<TestMethodOptions, 'story' | 'context' | 'renderTree' | 'snapshotFileName'>
) => any;

export function snapshotWithOptions(
options: { renderer?: any; serializer?: any } | Function = {}
) => ({
story,
context,
renderTree,
snapshotFileName,
}: {
story: any;
context: any;
renderTree: RenderTree;
snapshotFileName: string;
}): Promise<void> | void => {
const result = renderTree(story, context, optionsOrCallOptions(options, story));
): SnapshotsWithOptionsReturnType {
return ({ story, context, renderTree, snapshotFileName }) => {
const result = renderTree(story, context, optionsOrCallOptions(options, story));

function match(tree: any) {
let target = tree;
const isReact = story.parameters.framework === 'react';
function match(tree: any) {
let target = tree;
const isReact = story.parameters.framework === 'react';

if (isReact && typeof tree.childAt === 'function') {
target = tree.childAt(0);
}
if (isReact && Array.isArray(tree.children)) {
[target] = tree.children;
}
if (isReact && typeof tree.childAt === 'function') {
target = tree.childAt(0);
}
if (isReact && Array.isArray(tree.children)) {
[target] = tree.children;
}

if (snapshotFileName) {
expect(target).toMatchSpecificSnapshot(snapshotFileName);
} else {
expect(target).toMatchSnapshot();
}
if (snapshotFileName) {
expect(target).toMatchSpecificSnapshot(snapshotFileName);
} else {
expect(target).toMatchSnapshot();
}

if (typeof tree.unmount === 'function') {
tree.unmount();
if (typeof tree.unmount === 'function') {
tree.unmount();
}
}
}

if (typeof result.then === 'function') {
return result.then(match);
}
if (typeof result.then === 'function') {
return result.then(match);
}

return match(result);
};
return match(result);
};
}

export const multiSnapshotWithOptions = (options = {}) => ({
story,
context,
renderTree,
stories2snapsConverter,
}: {
story: any;
context: any;
renderTree: RenderTree;
stories2snapsConverter: Stories2SnapsConverter;
}) =>
snapshotWithOptions(options)({
story,
context,
renderTree,
snapshotFileName: stories2snapsConverter.getSnapshotFileName(context),
});
export function multiSnapshotWithOptions(options = {}): StoryshotsTestMethod {
return ({ story, context, renderTree, stories2snapsConverter }) => {
const snapshotFileName = stories2snapsConverter.getSnapshotFileName(context);
return snapshotWithOptions(options)({ story, context, renderTree, snapshotFileName });
};
}

export function shallowSnapshot({
export const shallowSnapshot: StoryshotsTestMethod = ({
story,
context,
renderShallowTree,
options = {},
}: {
story: any;
context: any;
renderShallowTree: RenderTree;
options: any;
}) {
}) => {
const result = renderShallowTree(story, context, options);
expect(result).toMatchSnapshot();
}

export const renderWithOptions = (options = {}) => ({
story,
context,
renderTree,
}: {
story: any;
context: any;
renderTree: RenderTree;
}) => {
const result = renderTree(story, context, options);
};

if (typeof result.then === 'function') {
return result;
}
export function renderWithOptions(options = {}): StoryshotsTestMethod {
return ({ story, context, renderTree }) => {
const result = renderTree(story, context, options);
if (typeof result.then === 'function') {
return result;
}

return undefined;
};
return undefined;
};
}

export const renderOnly = renderWithOptions();

Expand Down