UNPKG

redux-formo

Version:

An alternate forms framework for Redux+React.

122 lines (95 loc) 3.89 kB
'use strict'; var _constants = require('../constants'); var _filter = require('../actions/filter'); var _filter2 = _interopRequireDefault(_filter); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var KEY = 'form'; var FORM = 'personal-details'; var FIELD = 'firstName'; describe('filter()', function () { describe('=> synchronous', function () { it('should call the fn', function () { var fn = sinon.stub().returns('John'); var dispatch = sinon.spy(); var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) }); return (0, _filter2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).then(function () { expect(fn).to.be.calledOnce; }); }); it('should return an action with a filtered value', function () { var fn = sinon.stub().returns('John'); var dispatch = sinon.spy(); var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) }); return (0, _filter2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).then(function () { expect(dispatch).to.be.calledOnce; expect(dispatch).to.be.calledWith({ type: _constants.FILTER, status: 'finish', payload: 'John', meta: { form: FORM, field: FIELD } }); }); }); }); describe('=> asynchronous', function () { it('should call the fn', function () { var fn = sinon.stub().returns(new Promise(function (resolve) { setTimeout(function () { return resolve('John'); }, 100); })); var dispatch = sinon.spy(); var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) }); return (0, _filter2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).then(function () { expect(fn).to.be.calledOnce; }); }); it('should resolve an action with a filtered value', function () { var fn = sinon.stub().returns(new Promise(function (resolve) { setTimeout(function () { return resolve('John'); }, 100); })); var dispatch = sinon.spy(); var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) }); return (0, _filter2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).then(function () { expect(dispatch).to.be.calledTwice; expect(dispatch).to.be.calledWith({ type: _constants.FILTER, status: 'finish', payload: 'John', meta: { form: FORM, field: FIELD } }); }); }); it('should resolve an action with an error', function () { var fn = sinon.stub().returns(new Promise(function (resolve, reject) { setTimeout(function () { return reject(new Error('Error!')); }, 100); })); var dispatch = sinon.spy(); var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) }); return (0, _filter2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).catch(function () { expect(dispatch).to.be.calledTwice; expect(dispatch).to.be.calledWith({ type: _constants.FILTER, status: 'error', payload: new Error('Error!'), meta: { form: FORM, field: FIELD } }); }); }); }); }); //# sourceMappingURL=filter.test.js.map