'use strict';
var _this = this;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _ = require('../');
var _2 = _interopRequireDefault(_);
var _sinon = require('sinon');
function noop() {}
var GIVE_ME_META = 'GIVE_ME_META';
function metaMiddleware() {
return function (next) {
return function (action) {
return action.type === GIVE_ME_META ? next(_extends({}, action, { meta: 'here you go' })) : next(action);
};
};
}
describe('promiseMiddleware', function () {
var baseDispatch = undefined;
var dispatch = undefined;
var foobar = undefined;
var err = undefined;
beforeEach(function () {
baseDispatch = _sinon.spy();
dispatch = function d(action) {
var methods = { dispatch: d, getState: noop };
return metaMiddleware()(_2['default'](methods)(baseDispatch))(action);
};
foobar = { foo: 'bar' };
err = new Error();
});
it('handles Flux standard actions', function callee$1$0() {
var meta, action1, action2, action3, action4;
return regeneratorRuntime.async(function callee$1$0$(context$2$0) {
while (1) switch (context$2$0.prev = context$2$0.next) {
case 0:
meta = { 'do': 'wop' };
context$2$0.next = 3;
return regeneratorRuntime.awrap(dispatch({
type: 'ACTION_TYPE',
payload: Promise.resolve(foobar),
meta: meta
}));
case 3:
expect(baseDispatch.calledTwice).to.be['true'];
action1 = baseDispatch.firstCall.args[0];
expect(action1.type).to.equal('ACTION_TYPE');
expect(action1.payload).to.be.undefined;
expect(action1.meta).to.eql(meta);
expect(action1.sequence.type).to.equal('start');
action2 = baseDispatch.secondCall.args[0];
expect(action2.type).to.equal('ACTION_TYPE');
expect(action2.payload).to.eql(foobar);
expect(action2.meta).to.eql(meta);
expect(action2.sequence.type).to.equal('next');
expect(action1.sequence.id).to.equal(action2.sequence.id);
context$2$0.next = 17;
return regeneratorRuntime.awrap(dispatch({
type: 'ACTION_TYPE',
payload: Promise.reject(err),
meta: meta
})['catch'](noop));
case 17:
expect(baseDispatch.callCount).to.equal(4);
action3 = baseDispatch.args[2][0];
expect(action3.type).to.equal('ACTION_TYPE');
expect(action3.payload).to.be.undefined;
expect(action3.meta).to.eql(meta);
expect(action3.sequence.type).to.equal('start');
action4 = baseDispatch.args[3][0];
expect(action4.type).to.equal('ACTION_TYPE');
expect(action4.payload).to.eql(err);
expect(action4.error).to.be['true'];
expect(action4.meta).to.eql(meta);
expect(action4.sequence.type).to.equal('next');
expect(action3.sequence.id).to.equal(action4.sequence.id);
case 30:
case 'end':
return context$2$0.stop();
}
}, null, _this);
});
it('handles promises', function callee$1$0() {
return regeneratorRuntime.async(function callee$1$0$(context$2$0) {
while (1) switch (context$2$0.prev = context$2$0.next) {
case 0:
context$2$0.next = 2;
return regeneratorRuntime.awrap(dispatch(Promise.resolve(foobar)));
case 2:
expect(baseDispatch.calledOnce).to.be['true'];
expect(baseDispatch.firstCall.args[0]).to.equal(foobar);
context$2$0.next = 6;
return regeneratorRuntime.awrap(expect(dispatch(Promise.reject(err))).to.eventually.be.rejectedWith(err));
case 6:
case 'end':
return context$2$0.stop();
}
}, null, _this);
});
it('ignores non-promises', function callee$1$0() {
return regeneratorRuntime.async(function callee$1$0$(context$2$0) {
while (1) switch (context$2$0.prev = context$2$0.next) {
case 0:
dispatch(foobar);
expect(baseDispatch.calledOnce).to.be['true'];
expect(baseDispatch.firstCall.args[0]).to.equal(foobar);
dispatch({ type: 'ACTION_TYPE', payload: foobar });
expect(baseDispatch.calledTwice).to.be['true'];
expect(baseDispatch.secondCall.args[0]).to.deep.equal({
type: 'ACTION_TYPE',
payload: foobar
});
case 6:
case 'end':
return context$2$0.stop();
}
}, null, _this);
});
it('starts async dispatches from beginning of middleware chain', function callee$1$0() {
return regeneratorRuntime.async(function callee$1$0$(context$2$0) {
while (1) switch (context$2$0.prev = context$2$0.next) {
case 0:
context$2$0.next = 2;
return regeneratorRuntime.awrap(dispatch(Promise.resolve({ type: GIVE_ME_META })));
case 2:
dispatch({ type: GIVE_ME_META });
expect(baseDispatch.args.map(function (args) {
return args[0].meta;
})).to.eql(['here you go', 'here you go']);
case 4:
case 'end':
return context$2$0.stop();
}
}, null, _this);
});
});