Created
November 4, 2014 11:44
-
-
Save ilkka/23b0aadde2240e642798 to your computer and use it in GitHub Desktop.
Jest + React + Gulp
This file contains hidden or 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
| gulp.task('jest', function () { | |
| var nodeModules = path.resolve('./node_modules'); | |
| return gulp.src('app/scripts/**/__tests__') | |
| .pipe($.jest({ | |
| scriptPreprocessor: nodeModules + '/gulp-jest/preprocessor.js', | |
| unmockedModulePathPatterns: [nodeModules + '/react'] | |
| })); | |
| }); |
This file contains hidden or 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
| // app/scripts/ui/__tests__/Timer-test.js | |
| /** @jsx React.DOM */ | |
| jest.dontMock('../Timer'); | |
| describe('Timer', function () { | |
| var Timer = require('../Timer'); | |
| var React = require('react/addons'); | |
| var TestUtils = React.addons.TestUtils; | |
| var timer = null; | |
| beforeEach(function () { | |
| timer = TestUtils.renderIntoDocument(<Timer/>); | |
| }); | |
| it('increments seconds elapsed with each tick', function () { | |
| expect(timer.state.secondsElapsed).toBe(0); | |
| timer.tick(); | |
| expect(timer.state.secondsElapsed).toBe(1); | |
| }); | |
| it('registers tick to run once each second', function () { | |
| expect(setInterval.mock.calls.length).toBe(1); | |
| expect(setInterval.mock.calls[0][0]).toBe(timer.tick); | |
| expect(setInterval.mock.calls[0][1]).toBe(1000); | |
| }); | |
| }); | |
This file contains hidden or 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
| // app/scripts/ui/Timer.js | |
| /** @jsx React.DOM */ | |
| var React = require('react'); | |
| var Timer = React.createClass({ | |
| getInitialState: function() { | |
| return {secondsElapsed: 0}; | |
| }, | |
| tick: function() { | |
| this.setState({secondsElapsed: this.state.secondsElapsed + 1}); | |
| }, | |
| componentDidMount: function() { | |
| this.interval = setInterval(this.tick, 1000); | |
| }, | |
| componentWillUnmount: function() { | |
| clearInterval(this.interval); | |
| }, | |
| render: function() { | |
| return ( | |
| <div>Seconds Elapsed: {this.state.secondsElapsed}</div> | |
| ); | |
| } | |
| }); | |
| module.exports = Timer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment