forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding example of snapshot (jestjs#1209)
- Loading branch information
1 parent
6f581c5
commit f36c684
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["react"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
examples/snapshot/__tests__/__snapshots__/Link.react.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |