Skip to content

Commit

Permalink
Updating jest and fixing async tests regarding es6-promise jest fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jcperez-ch committed May 30, 2016
1 parent edbcb7e commit fde9fed
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 173 deletions.
4 changes: 4 additions & 0 deletions .babelrc
@@ -0,0 +1,4 @@
{
"presets": ["es2015", "react", "stage-0"]
}

11 changes: 0 additions & 11 deletions jest-preprocessor.js

This file was deleted.

21 changes: 8 additions & 13 deletions lib/Action.js
Expand Up @@ -10,20 +10,15 @@ var _Dispatcher = require('./Dispatcher');

var _Dispatcher2 = _interopRequireDefault(_Dispatcher);

var _es6Promise = require('es6-promise');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function reThrow(reject, error) {
setTimeout(function () {
if (error && error.stack) {
console.error(error.stack);
}
throw error;
}, 0);
return reject();
if (error && error.stack) {
console.error(error.stack);
}
return reject(error);
}

/**
Expand Down Expand Up @@ -56,10 +51,10 @@ var Action = function () {
_createClass(Action, [{
key: 'dispatch',
value: function dispatch() {
return _es6Promise.Promise.resolve(this.callback.apply(this, arguments)).then(function (payload) {
return new _es6Promise.Promise(function (resolve, reject) {
if (!payload) return reject();
if (!payload.actionType) return reThrow(reject, "Payload object requires an actionType property");
return Promise.resolve(this.callback.apply(this, arguments)).then(function (payload) {
return new Promise(function (resolve, reject) {
if (!payload) return reThrow(reject, 'Payload needs to be an object');
if (!payload.actionType) return reThrow(reject, 'Payload object requires an actionType property');

try {
_Dispatcher2.default.dispatch(payload);
Expand Down
23 changes: 20 additions & 3 deletions package.json
Expand Up @@ -26,15 +26,32 @@
},
"jest": {
"rootDir": "src",
"scriptPreprocessor": "../jest-preprocessor.js"
"scriptPreprocessor": "<rootDir>/../node_modules/babel-jest/src/index.js",
"unmockedModulePathPatterns": [
"node_modules(/|\\\\)object-assign",
"node_modules(/|\\\\)flux",
"node_modules(/|\\\\)invariant"
],
"moduleFileExtensions": [
"js",
"jsx"
],
"testFileExtensions": [
"js",
"jsx"
],
"globals": {
"__DEV__": true
}
},
"dependencies": {
"es6-promise": "^3.2.1",
"flux": "^2.1.1",
"invariant": "^2.2.1",
"object-assign": "^4.1.0"
},
"devDependencies": {
"babel-jest": "^12.1.0",
"babel-polyfill": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
Expand All @@ -43,7 +60,7 @@
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-replace": "^0.5.4",
"jest-cli": "^0.1.18",
"jest-cli": "^12.1.1",
"vinyl-source-stream": "^0.1.1"
}
}
23 changes: 8 additions & 15 deletions src/Action.js
@@ -1,16 +1,11 @@
'use strict';
import Dispatcher from './Dispatcher';
import { Promise } from 'es6-promise';


function reThrow(reject, error) {
setTimeout(() => {
if (error && error.stack) {
console.error(error.stack);
}
throw error;
}, 0);
return reject();
if (error && error.stack) {
console.error(error.stack);
}
return reject(error);
}

/**
Expand All @@ -36,12 +31,10 @@ class Action {
*/
dispatch() {
return Promise.resolve(this.callback.apply(this, arguments))
.then(function(payload){
return new Promise(function(resolve, reject){
if (!payload) return reject();
if (!payload.actionType) return reThrow(reject,
"Payload object requires an actionType property"
);
.then((payload) => {
return new Promise((resolve, reject) => {
if (!payload) return reThrow(reject, 'Payload needs to be an object');
if (!payload.actionType) return reThrow(reject, 'Payload object requires an actionType property');

try {
Dispatcher.dispatch(payload);
Expand Down
85 changes: 47 additions & 38 deletions src/__tests__/Action-test.js
Expand Up @@ -5,41 +5,41 @@ jest.dontMock('invariant');

describe('Action', function() {

var Action = require('../Action');
var Dispatcher = require('../Dispatcher');
var mockAction;
var callback;
const Action = require('../Action').default;
const Dispatcher = require('../Dispatcher').default;
let mockAction;
let callback;

it('should attach the callback argument to the instance', function() {

callback = function() {
return;
};
callback = () => false;

mockAction = new Action(callback);

expect(mockAction.callback).toBe(callback);

});

it('should throw if actionType isn\'t supplied', function(){
pit('should reject if actionType isn\'t supplied', async () => {

callback = function(argument) {
return{
test: argument
};
};
callback = (argument) => ({ test: argument });

mockAction = new Action(callback);
let success = true;
let error;

expect(function() {
mockAction.dispatch("test");
jest.runAllTimers();
}).toThrow();
try {
success = await mockAction.dispatch('test');
} catch (caughtError) {
error = caughtError;
}

expect(success).toBe(true);
expect(error).toEqual('Payload object requires an actionType property');

});

it('should not throw if actionType IS supplied', function(){
it('should not throw if actionType IS supplied', () => {

callback = function(argument) {
return{
Expand All @@ -57,39 +57,48 @@ describe('Action', function() {

});

pit('should reject if returns falsy value', function(){
pit('should reject if returns falsy value', async () => {

callback = function(argument) {
return false;
};
mockAction = new Action(callback);
let success = true;
let error;

return (new Action(callback)).dispatch("test")["catch"](
function(error){
expect(error).toBeUndefined();
});
try {
success = await mockAction.dispatch('test');
} catch (caughtError) {
error = caughtError;
}

expect(success).toBe(true);
expect(error).toEqual('Payload needs to be an object');
});

pit('should resolve if actionType IS supplied', function(){
pit('should resolve if actionType IS supplied', async () => {

callback = function(argument) {
return{
actionType: 'TEST_ACTION',
test: argument
};
};
callback = (argument) => ({
actionType: 'TEST_ACTION',
test: argument,
});
mockAction = new Action(callback);
let success = true;
let error;

return (new Action(callback)).dispatch("test").then(
function(success){
expect(success).toBeUndefined();
});
try {
success = await mockAction.dispatch("test");
} catch (caughtError) {
error = caughtError;
}

});
expect(success).toBeUndefined();
expect(error).toBeUndefined();

it('should have dispatched the supplied payload', function(){
});

it('should have dispatched the supplied payload', () => {
expect(Dispatcher.dispatch.mock.calls.length).toEqual(2);

});

});
});
28 changes: 12 additions & 16 deletions src/__tests__/ActionsFactory-test.js
Expand Up @@ -4,31 +4,27 @@ jest.dontMock('../ActionsFactory');
jest.dontMock('../Action');
jest.dontMock('object-assign');

describe('ActionsFactory', function() {
describe('ActionsFactory', () => {

var ActionsFactory = require('../ActionsFactory');
var mockActionsFactory;
const ActionsFactory = require('../ActionsFactory').default;
let mockActionsFactory;

it('create new Actions and return an object with the supplied method names as callers', function() {
it('create new Actions and return an object with the supplied method names as callers', () => {

mockActionsFactory = new ActionsFactory({
testMethodA: function(){
return {
testMethodA: () => ({
actionType: 'TEST_ACTION_A',
data: arguments
}
},
testMethodB: function(){
return {
actionType: 'TEST_ACTION_B',
data: arguments
}
}
data: arguments,
}),
testMethodB: () => ({
actionType: 'TEST_ACTION_B',
data: arguments
}),
});

expect(mockActionsFactory.testMethodA).toBeDefined();
expect(mockActionsFactory.testMethodB).toBeDefined();

});

});
});

0 comments on commit fde9fed

Please sign in to comment.