Skip to content

Commit

Permalink
Added a snapshotWithOptions test body and example
Browse files Browse the repository at this point in the history
See for instance #896 -- it's a common problem to need mocked nodes. 

This solution is probably a bit of a bandaid, as large projects would probably want to set options per-story, this is a lot better than nothing in the meantime.
  • Loading branch information
tmeasday authored and ndelangen committed May 24, 2017
1 parent 9222864 commit 941d808
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion addons/storyshots/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import createChannel from './storybook-channel-mock';
import { snapshot } from './test-bodies';
const { describe, it, expect } = global;

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

let storybook;
let configPath;
Expand Down
8 changes: 5 additions & 3 deletions addons/storyshots/src/test-bodies.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import renderer from 'react-test-renderer';
import shallow from 'react-test-renderer/shallow';

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

export const snapshot = snapshotWithOptions({});

export function renderOnly({ story, context }) {
const storyElement = story.render(context);
Expand Down
2 changes: 2 additions & 0 deletions examples/test-cra/src/__snapshots__/storyshots.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ exports[`Storyshots Button with text 1`] = `
</button>
`;

exports[`Storyshots ComponentWithRef basic 1`] = `<div />`;

exports[`Storyshots Welcome to Storybook 1`] = `
<div
style={
Expand Down
13 changes: 13 additions & 0 deletions examples/test-cra/src/stories/ComponentWithRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { Component } from 'react';

class ComponentWithRef extends Component {
componentDidMount() {
// Read the scroll width off the DOM element
console.log(this.ref.scrollWidth);
}
render() {
return <div ref={r => (this.ref = r)} />;
}
}

export default ComponentWithRef;
3 changes: 3 additions & 0 deletions examples/test-cra/src/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { linkTo } from '@storybook/addon-links';

import Button from './Button';
import Welcome from './Welcome';
import ComponentWithRef from './ComponentWithRef';

storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);

storiesOf('Button', module)
.add('with text', () => <Button onClick={action('clicked')}>Hello Button</Button>)
.add('with some emoji', () => <Button onClick={action('clicked')}>😀 😎 👍 💯</Button>);

storiesOf('ComponentWithRef', module).add('basic', () => <ComponentWithRef />);
12 changes: 11 additions & 1 deletion examples/test-cra/src/storyshots.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import initStoryshots from '@storybook/addon-storyshots';
import initStoryshots, { snapshotWithOptions } from '@storybook/addon-storyshots';
import path from 'path';

function createNodeMock(element) {
if (element.type === 'div') {
return { scrollWidth: 123 };
}
return null;
}

initStoryshots({
framework: 'react',
configPath: path.join(__dirname, '..', '.storybook'),
test: snapshotWithOptions({
createNodeMock,
}),
});

0 comments on commit 941d808

Please sign in to comment.