Skip to content

Commit

Permalink
Adding example of snapshot (jestjs#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
kentaromiura authored and cpojer committed Jun 29, 2016
1 parent 6f581c5 commit f36c684
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/snapshot/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["react"]
}
45 changes: 45 additions & 0 deletions examples/snapshot/Link.react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2004-present Facebook. All Rights Reserved.

'use strict';
const React = require('react');

const STATUS = {
NORMAL: 'normal',
HOVERED: 'hovered',
};

class Link extends React.Component {

constructor() {
super();
this.state = {
class: STATUS.NORMAL,
};
this._onMouseEnter = this._onMouseEnter.bind(this);
this._onMouseLeave = this._onMouseLeave.bind(this);
}

_onMouseEnter() {
this.setState({class: STATUS.HOVERED});
}

_onMouseLeave() {
this.setState({class: STATUS.NORMAL});
}

render() {
return (
<a
className={this.state.class}
href={this.props.page || '#'}
onMouseEnter={this._onMouseEnter}
onMouseLeave={this._onMouseLeave}
>
{this.props.children}
</a>
);
}

}

module.exports = Link;
47 changes: 47 additions & 0 deletions examples/snapshot/__tests__/Link.react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2004-present Facebook. All Rights Reserved.
/* eslint-disable no-unused-vars */

'use strict';

const React = require('react');
const Link = require('../Link.react');
const renderer = require('react/lib/ReactTestRenderer');

describe('Link', () => {

it('renders correctly', () => {
const tree = renderer.create(
<Link page="http://www.example.com">HI</Link>
).toJSON();
expect(tree).toMatchSnapshot();
});

it('renders as an anchor when no page is set', () => {
const tree = renderer.create(
<Link>HI</Link>
).toJSON();
expect(tree).toMatchSnapshot();
});

it('changes the class when hovered', () => {
const component = renderer.create(
<Link page="http://www.example.com">HI</Link>
);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();

// manually trigger the callback
tree.props.onMouseEnter();
// re-rendering
tree = component.toJSON();
expect(tree).toMatchSnapshot();

// manually trigger the callback
tree.props.onMouseLeave();
// re-rendering
tree = component.toJSON();
expect(tree).toMatchSnapshot();

});

});
49 changes: 49 additions & 0 deletions examples/snapshot/__tests__/__snapshots__/Link.react.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
exports[`Link renders correctly 0`] = `
<a
className="normal"
href="http://www.example.com"
onMouseEnter={[Function bound _onMouseEnter]}
onMouseLeave={[Function bound _onMouseLeave]}>
HI
</a>
`;

exports[`Link renders as an anchor when no page is set 0`] = `
<a
className="normal"
href="#"
onMouseEnter={[Function bound _onMouseEnter]}
onMouseLeave={[Function bound _onMouseLeave]}>
HI
</a>
`;

exports[`Link changes the class when hovered 0`] = `
<a
className="normal"
href="http://www.example.com"
onMouseEnter={[Function bound _onMouseEnter]}
onMouseLeave={[Function bound _onMouseLeave]}>
HI
</a>
`;

exports[`Link changes the class when hovered 1`] = `
<a
className="hovered"
href="http://www.example.com"
onMouseEnter={[Function bound _onMouseEnter]}
onMouseLeave={[Function bound _onMouseLeave]}>
HI
</a>
`;

exports[`Link changes the class when hovered 2`] = `
<a
className="normal"
href="http://www.example.com"
onMouseEnter={[Function bound _onMouseEnter]}
onMouseLeave={[Function bound _onMouseLeave]}>
HI
</a>
`;
15 changes: 15 additions & 0 deletions examples/snapshot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"dependencies": {
"react": "15.2.0-rc.1"
},
"devDependencies": {
"jest": "*",
"babel": "*",
"babel-preset-react": "*",
"babel-jest": "*"
},
"jest": {
"automock": false,
"testEnvironment": "node"
}
}

0 comments on commit f36c684

Please sign in to comment.