Skip to content

Instantly share code, notes, and snippets.

@magalhini
Last active October 27, 2015 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magalhini/5fd462b6f6977f2f25a9 to your computer and use it in GitHub Desktop.
Save magalhini/5fd462b6f6977f2f25a9 to your computer and use it in GitHub Desktop.
Testing a Flux Store using Mocha and Sinon. It uses rewire to fetch a new copy of the Store every time, as to maintain a clean state before each test. Also includes a sample for the Gulp task to run it in proper TDD fashion.
module.exports = function(markup) {
if (typeof document !== 'undefined') return;
var jsdom = require('jsdom').jsdom;
global.document = jsdom(markup || '');
global.window = document.defaultView;
global.navigator = {
userAgent: 'node.js'
};
};
var gulp = require('gulp');
var babel = require('babel/register')
var config = require('../config');
var mocha = require('gulp-mocha');
// "config" is a configuration file which contains the paths
// to your src and test folders. Feel free to hardcode them if you wish instead.
gulp.task('mocha', function (done) {
return gulp.src(config.test.src, {read: false})
.pipe(mocha({
reporter: 'spec',
compilers: {
js: babel
}
}))
});
gulp.task('watch-mocha', function() {
gulp.watch([config.test.src, config.watch.src], ['mocha']);
});
// Mock our global environment using JSDOM
require('../dom-mock')('<html><body></body></html>');
let jsdom = require('mocha-jsdom');
import {
React,
sinon,
assert,
expect,
TestUtils
} from '../test-helper';
// Rewire allows for a "fresh" Store to be retrieved everytime. No need to keep erasing its contents.
import rewire from 'rewire';
describe('TemplateStore', () => {
var Constants = require('../../src/js/constants/Constants');
var registerSpy, unregisterSpy, dispatchSpy, TemplateStore, callback;
let AppDispatcher = require('../../src/js/dispatcher/Dispatcher');
// Action generator
let actionGetTemplates = (id) => {
return {
action: {
type: Constants.ActionTypes.TEMPLATES_FETCHED,
templates: [{name: 'gemuse', description: 'is vegetables', id: id}]
}
};
};
beforeEach(function() {
TemplateStore = rewire('../../src/js/stores/TemplateStore');
registerSpy = sinon.stub(AppDispatcher, 'register');
dispatchSpy = sinon.spy(AppDispatcher, 'dispatch');
AppDispatcher.register(TemplateStore);
callback = registerSpy.lastCall.args[0];
});
afterEach(function() {
registerSpy.restore();
dispatchSpy.restore();
});
it('should register a callback with the dispatcher', () => {
expect(registerSpy.callCount).to.equal(1);
});
it('should receive one template with a name and description', () => {
AppDispatcher.dispatch(actionGetTemplates(1))
var all = TemplateStore.getAll();
var keys = Object.keys(all.templates[0]);
expect(keys.length).to.equal(3);
expect(all.templates[0][keys[0]]).to.equal('gemuse');
expect(all.templates[0][keys[1]]).to.equal('is vegetables');
});
});
import chai from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import React from 'react/addons';
let { assert, expect } = chai,
{ TestUtils } = React.addons;
chai.should();
chai.use(sinonChai);
export {
React,
chai,
sinon,
sinonChai,
assert,
expect,
TestUtils
}
// from http://reactjsnews.com/testing-in-react/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment