Created
November 30, 2016 05:29
-
-
Save DonnieWest/211bdcee68e32f081081778ea6e65ea0 to your computer and use it in GitHub Desktop.
stolen-jest-sample-code
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
// __tests__/Link.react-test.js | |
import React from 'react'; | |
import Link from '../Link.react'; | |
import renderer from 'react-test-renderer'; | |
test('Link changes the class when hovered', () => { | |
const component = renderer.create( | |
<Link page="http://www.facebook.com">Facebook</Link> | |
); | |
let tree = component.toJSON(); | |
expect(tree).toMatchSnapshot(); | |
// Manually trigger the callback defined on the <a> | |
tree.props.onMouseEnter(); | |
// Since the state has changed, retrieve the JSON again | |
tree = component.toJSON(); | |
expect(tree).toMatchSnapshot(); | |
// manually trigger the callback | |
tree.props.onMouseLeave(); | |
// re-rendering | |
tree = component.toJSON(); | |
expect(tree).toMatchSnapshot(); | |
}); |
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
//__tests__/__snapshots__/Link.react-test.js.snap | |
exports[`Link changes the class when hovered 1`] = ` | |
<a | |
className="normal" | |
href="http://www.facebook.com" | |
onMouseEnter={[Function]} | |
onMouseLeave={[Function]}> | |
</a> | |
`; | |
exports[`Link changes the class when hovered 2`] = ` | |
<a | |
className="hovered" | |
href="http://www.facebook.com" | |
onMouseEnter={[Function]} | |
onMouseLeave={[Function]}> | |
</a> | |
`; | |
exports[`Link changes the class when hovered 3`] = ` | |
<a | |
className="normal" | |
href="http://www.facebook.com" | |
onMouseEnter={[Function]} | |
onMouseLeave={[Function]}> | |
</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
// Link.react.js | |
import React from 'react'; | |
const STATUS = { | |
NORMAL: 'normal', | |
HOVERED: 'hovered', | |
}; | |
export default class Link extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
class: STATUS.NORMAL, | |
}; | |
} | |
_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> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment