redux-testkit
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -134,3 +134,3 @@ # API Examples | ||
it('should clear all posts', () => { | ||
const dispatches = await Thunk(uut.clearPosts).execute(); | ||
const dispatches = Thunk(uut.clearPosts).execute(); | ||
expect(dispatches.length).toBe(1); | ||
@@ -155,3 +155,3 @@ expect(dispatches[0].getType()).toEqual('POSTS_UPDATED'); | ||
const state = { loading: false, posts: ['funny1', 'scary2', 'funny3'] }; | ||
const dispatches = await Thunk(uut.filterPosts).withState(state).execute('funny'); | ||
const dispatches = Thunk(uut.filterPosts).withState(state).execute('funny'); | ||
expect(dispatches.length).toBe(1); | ||
@@ -158,0 +158,0 @@ expect(dispatches[0].getAction()).toEqual({ type: 'POSTS_UPDATED', posts: ['funny1', 'funny3'] }); |
@@ -9,119 +9,74 @@ 'use strict'; | ||
var dispatch = function () { | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(action) { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!_lodash2.default.isFunction(action) && !_lodash2.default.isPlainObject(action)) { | ||
error = new Error('unsupported ' + action + ' action type sent to dispatch'); | ||
} | ||
exports.default = function (thunkFunction) { | ||
var dispatch = function () { | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(action) { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!_lodash2.default.isFunction(action) && !_lodash2.default.isPlainObject(action)) { | ||
error = new Error('unsupported ' + action + ' action type sent to dispatch'); | ||
} | ||
dispatches.push(createDispatchedObject(action)); | ||
dispatches.push(createDispatchedObject(action)); | ||
case 2: | ||
case 'end': | ||
return _context.stop(); | ||
case 2: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
} | ||
}, _callee, this); | ||
})); | ||
}, _callee, this); | ||
})); | ||
return function dispatch(_x) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
return function dispatch(_x) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
var executeDispatch = function () { | ||
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(action) { | ||
var result; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
if (!_lodash2.default.isFunction(action)) { | ||
_context2.next = 5; | ||
break; | ||
} | ||
var dispatches = []; | ||
var state = void 0; | ||
var originalState = void 0; | ||
var error = void 0; | ||
_context2.next = 3; | ||
return action(dispatch, getState); | ||
function getState() { | ||
return state; | ||
} | ||
case 3: | ||
result = _context2.sent; | ||
return _context2.abrupt('return', Promise.resolve(result)); | ||
function executeDispatch(action) { | ||
if (_lodash2.default.isFunction(action)) { | ||
return action(dispatch, getState); | ||
} | ||
error = new Error('provided action is not a thunk function'); | ||
return null; | ||
} | ||
case 5: | ||
function checkForStateMutation() { | ||
var mutated = !utils.deepEqual(state, originalState); | ||
if (mutated) { | ||
error = new Error('state mutated after running the thunk'); | ||
} | ||
} | ||
error = new Error('provided action is not a thunk function'); | ||
return _context2.abrupt('return', null); | ||
case 7: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
return function executeDispatch(_x2) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
exports.default = function (thunkFunction) { | ||
dispatches = []; | ||
error = undefined; | ||
state = undefined; | ||
originalState = undefined; | ||
function internalThunkCommands() { | ||
var _this = this; | ||
return { | ||
execute: function () { | ||
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3() { | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (!_lodash2.default.isFunction(thunkFunction)) { | ||
_context3.next = 6; | ||
break; | ||
} | ||
_context3.next = 3; | ||
return executeDispatch(thunkFunction()); | ||
case 3: | ||
checkForStateMutation(); | ||
_context3.next = 7; | ||
break; | ||
case 6: | ||
error = new Error('you must pass a thunk function to Thunk()'); | ||
case 7: | ||
if (!error) { | ||
_context3.next = 9; | ||
break; | ||
} | ||
throw error; | ||
case 9: | ||
return _context3.abrupt('return', dispatches); | ||
case 10: | ||
case 'end': | ||
return _context3.stop(); | ||
execute: function execute() { | ||
if (_lodash2.default.isFunction(thunkFunction)) { | ||
var dispatchResult = executeDispatch(thunkFunction()); | ||
if (!utils.isPromise(dispatchResult)) { | ||
checkForStateMutation(); | ||
if (error) { | ||
throw error; | ||
} | ||
return dispatches; | ||
} else { | ||
return dispatchResult.then(function () { | ||
checkForStateMutation(); | ||
if (error) { | ||
throw error; | ||
} | ||
} | ||
}, _callee3, _this); | ||
})); | ||
return function execute() { | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}() | ||
return dispatches; | ||
}); | ||
} | ||
} | ||
throw new Error('you must pass a thunk function to Thunk()'); | ||
} | ||
}; | ||
@@ -153,7 +108,2 @@ } | ||
var dispatches = []; | ||
var state = void 0; | ||
var originalState = void 0; | ||
var error = void 0; | ||
function createDispatchedObject(action) { | ||
@@ -177,14 +127,2 @@ return { | ||
}; | ||
} | ||
function getState() { | ||
return state; | ||
} | ||
function checkForStateMutation() { | ||
var mutated = !utils.deepEqual(state, originalState); | ||
if (mutated) { | ||
error = new Error('state mutated after running the thunk'); | ||
} | ||
} |
@@ -10,2 +10,3 @@ "use strict"; | ||
exports.deepEqual = deepEqual; | ||
exports.isPromise = isPromise; | ||
function deepEqual(x, y) { | ||
@@ -33,2 +34,6 @@ if ((typeof x === "undefined" ? "undefined" : _typeof(x)) === "object" && x !== null && (typeof y === "undefined" ? "undefined" : _typeof(y)) === "object" && y !== null) { | ||
} | ||
} | ||
function isPromise(obj) { | ||
return Promise.resolve(obj) === obj; | ||
} |
{ | ||
"name": "redux-testkit", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"publishConfig": { | ||
@@ -5,0 +5,0 @@ "registry": "https://registry.npmjs.org/" |
@@ -154,3 +154,3 @@ # Redux Testkit | ||
it('should clear all posts', () => { | ||
const dispatches = await Thunk(uut.clearPosts).execute(); | ||
const dispatches = Thunk(uut.clearPosts).execute(); | ||
expect(dispatches.length).toBe(1); | ||
@@ -174,3 +174,3 @@ expect(dispatches[0].getAction()).toEqual({ type: 'POSTS_UPDATED', posts: [] }); | ||
const state = { loading: false, posts: ['funny1', 'scary2', 'funny3'] }; | ||
const dispatches = await Thunk(uut.filterPosts).withState(state).execute('funny'); | ||
const dispatches = Thunk(uut.filterPosts).withState(state).execute('funny'); | ||
expect(dispatches.length).toBe(1); | ||
@@ -190,3 +190,3 @@ expect(dispatches[0].getAction()).toEqual({ type: 'POSTS_UPDATED', posts: ['funny1', 'funny3'] }); | ||
* Returns an awaitable array of dispatches performed by the thunk (shallow, these dispatches are not executed). You can run expectations over them manually. Always `await` on the result to get the actual dispatches array. | ||
* Returns an array of dispatches performed by the thunk (shallow, these dispatches are not executed). You can run expectations over them manually. If the tested thunk is asynchronous, `await` on the result to get the actual dispatches array. If it's synchronous, you can use the return value directly without `await`. | ||
@@ -193,0 +193,0 @@ * Also verifies that `state` did not mutate. [Why is this important? see example bug](BUG-EXAMPLES.md#thunk) |
import _ from 'lodash'; | ||
import * as utils from './utils'; | ||
let dispatches = []; | ||
let state; | ||
let originalState; | ||
let error; | ||
function createDispatchedObject(action) { | ||
@@ -19,52 +14,56 @@ return { | ||
function getState() { | ||
return state; | ||
} | ||
export default function(thunkFunction) { | ||
const dispatches = []; | ||
let state; | ||
let originalState; | ||
let error; | ||
async function dispatch(action) { | ||
if (!_.isFunction(action) && !_.isPlainObject(action)) { | ||
error = new Error(`unsupported ${action} action type sent to dispatch`); | ||
function getState() { | ||
return state; | ||
} | ||
dispatches.push(createDispatchedObject(action)); | ||
} | ||
async function dispatch(action) { | ||
if (!_.isFunction(action) && !_.isPlainObject(action)) { | ||
error = new Error(`unsupported ${action} action type sent to dispatch`); | ||
} | ||
dispatches.push(createDispatchedObject(action)); | ||
} | ||
async function executeDispatch(action) { | ||
if (_.isFunction(action)) { | ||
const result = await action(dispatch, getState); | ||
return Promise.resolve(result); | ||
function executeDispatch(action) { | ||
if (_.isFunction(action)) { | ||
return action(dispatch, getState); | ||
} | ||
error = new Error('provided action is not a thunk function'); | ||
return null; | ||
} | ||
error = new Error('provided action is not a thunk function'); | ||
return null; | ||
} | ||
function checkForStateMutation() { | ||
const mutated = !utils.deepEqual(state, originalState); | ||
if (mutated) { | ||
error = new Error('state mutated after running the thunk'); | ||
function checkForStateMutation() { | ||
const mutated = !utils.deepEqual(state, originalState); | ||
if (mutated) { | ||
error = new Error('state mutated after running the thunk'); | ||
} | ||
} | ||
} | ||
export default function(thunkFunction) { | ||
dispatches = []; | ||
error = undefined; | ||
state = undefined; | ||
originalState = undefined; | ||
function internalThunkCommands() { | ||
return { | ||
execute: async () => { | ||
execute: () => { | ||
if (_.isFunction(thunkFunction)) { | ||
await executeDispatch(thunkFunction()); | ||
checkForStateMutation(); | ||
} else { | ||
error = new Error('you must pass a thunk function to Thunk()'); | ||
const dispatchResult = executeDispatch(thunkFunction()); | ||
if (!utils.isPromise(dispatchResult)) { | ||
checkForStateMutation(); | ||
if (error) { | ||
throw error; | ||
} | ||
return dispatches; | ||
} else { | ||
return dispatchResult.then(() => { | ||
checkForStateMutation(); | ||
if (error) { | ||
throw error; | ||
} | ||
return dispatches; | ||
}); | ||
} | ||
} | ||
if (error) { | ||
throw error; | ||
} | ||
return dispatches; | ||
throw new Error('you must pass a thunk function to Thunk()'); | ||
} | ||
@@ -71,0 +70,0 @@ }; |
@@ -24,1 +24,5 @@ export function deepEqual(x, y) { | ||
} | ||
export function isPromise(obj) { | ||
return Promise.resolve(obj) === obj; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
193342
568