Merged pull, fixed equality bug and added tests

This commit is contained in:
christianalfoni 2015-04-29 14:57:51 +02:00
parent f568ac5970
commit 37018b10fa
9 changed files with 84 additions and 748 deletions

View File

@ -1,6 +1,6 @@
{
"name": "formsy-react",
"version": "0.13.0",
"version": "0.13.1",
"description": "A form input builder and validator for React JS",
"repository": {
"type": "git",

View File

@ -1,6 +1,6 @@
{
"name": "formsy-react",
"version": "0.13.0",
"version": "0.13.1",
"description": "A form input builder and validator for React JS",
"repository": {
"type": "git",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -518,4 +518,44 @@ it('should allow an undefined value to be updated to a value', function (done) {
});
it('should handle isFormDisabled with dynamic inputs', function () {
var TestInput = React.createClass({
mixins: [Formsy.Mixin],
render: function () {
return <input disabled={this.isFormDisabled()} />
}
});
var TestForm = React.createClass({
getInitialState: function () {
return {
bool: true
};
},
flip: function () {
this.setState({
bool: !this.state.bool
});
},
render: function () {
return (
<Formsy.Form disabled={this.state.bool}>
{this.state.bool ?
<TestInput name="foo" /> :
<TestInput name="bar" />
}
</Formsy.Form>
);
}
});
var form = TestUtils.renderIntoDocument(<TestForm/>);
var input = TestUtils.findRenderedComponentWithType(form, TestInput);
expect(input.isFormDisabled()).toBe(true);
form.flip();
expect(input.isFormDisabled()).toBe(false);
});
});

20
specs/Utils-spec.jsx Normal file
View File

@ -0,0 +1,20 @@
var utils = require('./../src/utils.js');
describe('Utils', function() {
it('should check equality of objects and arrays', function () {
var objA = {foo: 'bar'};
var objB = {foo: 'bar'};
var objC = [{
foo: ['bar']
}];
var objD = [{
foo: ['bar']
}];
expect(utils.isSame(objA, objB)).toBe(true);
expect(utils.isSame(objC, objD)).toBe(true);
expect(utils.isSame(objA, objD)).toBe(false);
});
});

View File

@ -76,17 +76,11 @@ module.exports = {
this.setValidations(nextProps.validations, nextProps.required);
},
componentDidUpdate: function (prevProps, prevState) {
componentDidUpdate: function (prevProps) {
var isValueChanged = function () {
return !utils.isSame(this.props.value, prevProps.value) && utils.isSame(this.state._value, prevProps.value);
}.bind(this);
// If validations has changed or something outside changes
// the value, set the value again running a validation
if (isValueChanged()) {
// If the value passed has changed, set it. If value is not passed it will
// internally update, and this will never run
if (!utils.isSame(this.props.value, prevProps.value)) {
this.setValue(this.props.value);
}
},

View File

@ -50,28 +50,24 @@ Formsy.Form = React.createClass({
},
componentWillUpdate: function () {
var inputKeys = Object.keys(this.inputs);
// Keep a reference to input keys before form updates,
// to check if inputs has changed after render
this.prevInputKeys = Object.keys(this.inputs);
// The updated children array is not available here for some reason,
// we need to wait for next event loop
setTimeout(function () {
},
// The component might have been unmounted on an
// update
if (this.isMounted()) {
componentDidUpdate: function () {
if (this.props.validationErrors) {
this.setInputValidationErrors(this.props.validationErrors);
}
if (this.props.validationErrors) {
this.setInputValidationErrors(this.props.validationErrors);
}
var newInputKeys = Object.keys(this.inputs);
if (utils.arraysDiffer(this.prevInputKeys, newInputKeys)) {
this.validateForm();
}
var newInputKeys = Object.keys(this.inputs);
if (utils.arraysDiffer(inputKeys, newInputKeys)) {
this.validateForm();
}
}
}.bind(this), 0);
},
reset: function () {

View File

@ -26,10 +26,6 @@ module.exports = {
return isDifferent;
},
isSame: function (a, b) {
if (a !== b) {
return false;
}
if (Array.isArray(a)) {
return !this.arraysDiffer(a, b);
@ -37,6 +33,6 @@ module.exports = {
return !this.objectsDiffer(a, b);
}
return true;
return a === b;
}
};