redux-formo
Version:
An alternate forms framework for Redux+React.
171 lines (136 loc) • 5.39 kB
JavaScript
'use strict';
var _constants = require('../constants');
var _validate = require('../actions/validate');
var _validate2 = _interopRequireDefault(_validate);
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('validate()', function () {
describe('=> synchronous', function () {
it('should call the fn', function () {
var fn = sinon.stub().returns(true);
var dispatch = sinon.spy();
var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) });
return (0, _validate2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).then(function () {
expect(fn).to.be.calledOnce;
});
});
it('should return an action with a valid result', function () {
var dispatch = sinon.spy();
var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) });
return (0, _validate2.default)(KEY, FORM, FIELD, function () {
return true;
})(dispatch, getState).then(function () {
expect(dispatch).to.be.calledOnce;
expect(dispatch).to.be.calledWith({
type: _constants.VALIDATE,
status: 'finish',
payload: true,
meta: {
form: FORM,
field: FIELD
}
});
});
});
it('should return an action with an invalid result', function () {
var fn = function fn() {
return 'Invalid!';
};
var dispatch = sinon.spy();
var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) });
return (0, _validate2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).then(function () {
expect(dispatch).to.be.calledOnce;
expect(dispatch).to.be.calledWith({
type: _constants.VALIDATE,
status: 'finish',
payload: 'Invalid!',
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(true);
}, 100);
}));
var dispatch = sinon.spy();
var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) });
return (0, _validate2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).then(function () {
expect(fn).to.be.calledOnce;
});
});
it('should resolve an action with a valid result', function () {
var fn = sinon.stub().returns(new Promise(function (resolve) {
setTimeout(function () {
return resolve(true);
}, 100);
}));
var dispatch = sinon.spy();
var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) });
return (0, _validate2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).then(function () {
expect(dispatch).to.be.calledTwice;
expect(dispatch).to.be.calledWith({
type: _constants.VALIDATE,
status: 'finish',
payload: true,
meta: {
form: FORM,
field: FIELD
}
});
});
});
it('should resolve an action with an invalid result', function () {
var fn = sinon.stub().returns(new Promise(function (resolve) {
setTimeout(function () {
return resolve('Invalid!');
}, 100);
}));
var dispatch = sinon.spy();
var getState = sinon.stub().returns({ form: _defineProperty({}, FORM, { fields: { firstName: { value: 'John' } } }) });
return (0, _validate2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).then(function () {
expect(dispatch).to.be.calledTwice;
expect(dispatch).to.be.calledWith({
type: _constants.VALIDATE,
status: 'finish',
payload: 'Invalid!',
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, _validate2.default)(KEY, FORM, FIELD, fn)(dispatch, getState).catch(function (err) {
expect(dispatch).to.be.calledTwice;
expect(dispatch).to.be.calledWith({
type: _constants.VALIDATE,
status: 'error',
payload: new Error('Error!'),
meta: {
form: FORM,
field: FIELD
}
});
});
});
});
});
//# sourceMappingURL=validate.test.js.map