Skip to content

Commit

Permalink
Allow passing a test function to storyshots
Browse files Browse the repository at this point in the history
See #1034
  • Loading branch information
tmeasday committed May 15, 2017
1 parent b88fd53 commit 5df15fa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
5 changes: 4 additions & 1 deletion examples/test-cra/src/storyshots.test.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import initStoryshots from 'storyshots';
import initStoryshots, { justRender } from 'storyshots';

initStoryshots();

// initStoryshots({ test: justRender });
10 changes: 6 additions & 4 deletions packages/storyshots/src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import renderer from 'react-test-renderer';
import path from 'path';
import readPkgUp from 'read-pkg-up';
import addons from '@kadira/storybook-addons';
import runWithRequireContext from './require_context';
import createChannel from './storybook-channel-mock';
import { snapshot } from './test-bodies';
const { describe, it, expect } = global;

export { snapshot, justRender } from './test-bodies';

let storybook;
let configPath;

Expand Down Expand Up @@ -60,6 +62,8 @@ export default function testStorySnapshots(options = {}) {
// Added not to break existing storyshots configs (can be removed in a future major release)
options.storyNameRegex = options.storyNameRegex || options.storyRegex;

options.test = options.test || snapshot;

for (const group of stories) {
if (options.storyKindRegex && !group.kind.match(options.storyKindRegex)) {
continue;
Expand All @@ -74,9 +78,7 @@ export default function testStorySnapshots(options = {}) {

it(story.name, () => {
const context = { kind: group.kind, story: story.name };
const renderedStory = story.render(context);
const tree = renderer.create(renderedStory).toJSON();
expect(tree).toMatchSnapshot();
options.test({ story, context });
});
}
});
Expand Down
13 changes: 13 additions & 0 deletions packages/storyshots/src/test-bodies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import renderer from 'react-test-renderer';
import shallow from 'react-test-renderer/shallow';

export function snapshot({ story, context }) {
const storyElement = story.render(context);
const tree = renderer.create(storyElement).toJSON();
expect(tree).toMatchSnapshot();
}

export function justRender({ story, context }) {
const storyElement = story.render(context);
const tree = renderer.create(storyElement);
}

0 comments on commit 5df15fa

Please sign in to comment.