Skip to content

Instantly share code, notes, and snippets.

@ilkka
Created November 4, 2014 11:44
Show Gist options
  • Select an option

  • Save ilkka/23b0aadde2240e642798 to your computer and use it in GitHub Desktop.

Select an option

Save ilkka/23b0aadde2240e642798 to your computer and use it in GitHub Desktop.
Jest + React + Gulp
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']
}));
});
// 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);
});
});
// 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