improving validation rules + unit tests
This commit is contained in:
parent
e4b5dbf8f9
commit
8f4e2566d5
|
|
@ -0,0 +1,60 @@
|
|||
var Formsy = require('./../src/main.js');
|
||||
|
||||
describe('Rules: equals', function() {
|
||||
var TestInput, isValid, form, input;
|
||||
|
||||
beforeEach(function() {
|
||||
isValid = jasmine.createSpy('valid');
|
||||
|
||||
TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" validations="equals:myValue"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
TestInput = isValid = isInvalid = form = null;
|
||||
});
|
||||
|
||||
it('should fail with undefined', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: undefined}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with null', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: null}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail when the value is not equal', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'foo'}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass when the value is equal', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
var Formsy = require('./../src/main.js');
|
||||
|
||||
describe('Rules: hasValue', function() {
|
||||
var TestInput, isValid, form, input;
|
||||
|
||||
beforeEach(function() {
|
||||
isValid = jasmine.createSpy('valid');
|
||||
|
||||
TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" validations="hasValue"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
TestInput = isValid = isInvalid = form = null;
|
||||
});
|
||||
|
||||
it('should fail with undefined', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: undefined}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with null', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: null}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass with a string', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
var Formsy = require('./../src/main.js');
|
||||
|
||||
describe('Rules: isAlpha', function() {
|
||||
var TestInput, isValid, form, input;
|
||||
|
||||
beforeEach(function() {
|
||||
isValid = jasmine.createSpy('valid');
|
||||
|
||||
TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" validations="isAlpha"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
TestInput = isValid = isInvalid = form = null;
|
||||
});
|
||||
|
||||
it('should fail with undefined', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: undefined}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with null', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: null}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with a number', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 123}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass with a string', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
var Formsy = require('./../src/main.js');
|
||||
|
||||
describe('Rules: isEmail', function() {
|
||||
var TestInput, isValid, form, input;
|
||||
|
||||
beforeEach(function() {
|
||||
isValid = jasmine.createSpy('valid');
|
||||
|
||||
TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" value="foo" validations="isEmail"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
TestInput = isValid = isInvalid = form = null;
|
||||
});
|
||||
|
||||
it('should fail with "foo"', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'foo'}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass with "foo@foo.com"', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'foo@foo.com'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
var Formsy = require('./../src/main.js');
|
||||
|
||||
describe('Rules: isLength', function() {
|
||||
var TestInput, isValid, form, input;
|
||||
|
||||
beforeEach(function() {
|
||||
isValid = jasmine.createSpy('valid');
|
||||
|
||||
TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" validations="isLength:3"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
TestInput = isValid = isInvalid = form = null;
|
||||
});
|
||||
|
||||
it('should fail with undefined', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: undefined}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with null', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: null}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with a number', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 123}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with a string too small', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: "hi"}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with a string too long', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: "foo bar"}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass with the right length', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'sup'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
var Formsy = require('./../src/main.js');
|
||||
|
||||
describe('Rules: isNumeric', function() {
|
||||
var TestInput, isValid, form, input;
|
||||
|
||||
beforeEach(function() {
|
||||
isValid = jasmine.createSpy('valid');
|
||||
|
||||
TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" validations="isNumeric"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
TestInput = isValid = isInvalid = form = null;
|
||||
});
|
||||
|
||||
it('should fail with undefined', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: undefined}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with null', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: null}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with a string', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass with a number as string', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: '123'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass with an int', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 123}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass with a float', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 1.23}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
var Formsy = require('./../src/main.js');
|
||||
|
||||
describe('Rules: isWords', function() {
|
||||
var TestInput, isValid, form, input;
|
||||
|
||||
beforeEach(function() {
|
||||
isValid = jasmine.createSpy('valid');
|
||||
|
||||
TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" validations="isWords"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
TestInput = isValid = isInvalid = form = null;
|
||||
});
|
||||
|
||||
it('should fail with undefined', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: undefined}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with null', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: null}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with a number', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 123}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass with a 1 word', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'sup'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass with 2 words', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'sup dude'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
var Formsy = require('./../src/main.js');
|
||||
|
||||
describe('Rules: maxLength', function() {
|
||||
var TestInput, isValid, form, input;
|
||||
|
||||
beforeEach(function() {
|
||||
isValid = jasmine.createSpy('valid');
|
||||
|
||||
TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" validations="maxLength:3"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
TestInput = isValid = isInvalid = form = null;
|
||||
});
|
||||
|
||||
it('should fail with undefined', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: undefined}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with null', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: null}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with a number', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 123}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass when a string\'s length is smaller', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'hi'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass when a string\'s length is equal', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'bar'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail when a string\'s length is bigger', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
var Formsy = require('./../src/main.js');
|
||||
|
||||
describe('Rules: minLength', function() {
|
||||
var TestInput, isValid, form, input;
|
||||
|
||||
beforeEach(function() {
|
||||
isValid = jasmine.createSpy('valid');
|
||||
|
||||
TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" validations="minLength:3"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
TestInput = isValid = isInvalid = form = null;
|
||||
});
|
||||
|
||||
it('should fail with undefined', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: undefined}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with null', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: null}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail with a number', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 123}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail when a string\'s length is smaller', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'hi'}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass when a string\'s length is equal', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'bar'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass when a string\'s length is bigger', function () {
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -215,166 +215,4 @@ describe('Validation', function() {
|
|||
|
||||
});
|
||||
|
||||
it('RULE: isEmail', function () {
|
||||
|
||||
var isValid = jasmine.createSpy('valid');
|
||||
var TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
var form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" value="foo" validations="isEmail"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: 'foo@foo.com'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
it('RULE: isNumeric', function () {
|
||||
|
||||
var isValid = jasmine.createSpy('valid');
|
||||
var TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
var form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" value="foo" validations="isNumeric"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: '123'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
it('RULE: isNumeric (actual number)', function () {
|
||||
|
||||
var isValid = jasmine.createSpy('valid');
|
||||
var TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(Number(event.target.value));
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
var form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" value="foo" validations="isNumeric"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: '123'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
it('RULE: isNumeric (string representation of a float)', function () {
|
||||
|
||||
var isValid = jasmine.createSpy('valid');
|
||||
var TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
var form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" value="foo" validations="isNumeric"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: '1.5'}});
|
||||
expect(isValid).toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
it('RULE: isNumeric is false (string representation of an invalid float)', function () {
|
||||
|
||||
var isValid = jasmine.createSpy('valid');
|
||||
var TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
updateValue: function (event) {
|
||||
this.setValue(event.target.value);
|
||||
},
|
||||
render: function () {
|
||||
if (this.isValid()) {
|
||||
isValid();
|
||||
}
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>
|
||||
}
|
||||
});
|
||||
var form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" value="foo" validations="isNumeric"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
TestUtils.Simulate.change(input, {target: {value: '1.'}});
|
||||
expect(isValid).not.toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
it('RULE: equalsField', function () {
|
||||
|
||||
var TestInput = React.createClass({
|
||||
mixins: [Formsy.Mixin],
|
||||
render: function () {
|
||||
return <input value={this.getValue()}/>
|
||||
}
|
||||
});
|
||||
var form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<TestInput name="foo" value="foo" validations="equalsField:bar"/>
|
||||
<TestInput name="bar" value="foo" validations="equalsField:foobar"/>
|
||||
<TestInput name="foobar" value="bar"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
var input = TestUtils.scryRenderedComponentsWithType(form, TestInput);
|
||||
expect(input[0].isValid()).toBe(true);
|
||||
expect(input[1].isValid()).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,33 +1,36 @@
|
|||
module.exports = {
|
||||
'isDefaultRequiredValue': function (values, value) {
|
||||
isDefaultRequiredValue: function (values, value) {
|
||||
return value === undefined || value === '';
|
||||
},
|
||||
'hasValue': function (values, value) {
|
||||
return value !== undefined;
|
||||
hasValue: function (values, value) {
|
||||
return !!value;
|
||||
},
|
||||
'matchRegexp': function (values, value, regexp) {
|
||||
return value !== undefined && !!value.match(regexp);
|
||||
matchRegexp: function (values, value, regexp) {
|
||||
return !!value && !!value.match(regexp);
|
||||
},
|
||||
'isUndefined': function (values, value) {
|
||||
isUndefined: function (values, value) {
|
||||
return value === undefined;
|
||||
},
|
||||
'isEmptyString': function (values, value) {
|
||||
isEmptyString: function (values, value) {
|
||||
return value === '';
|
||||
},
|
||||
'isEmail': function (values, value) {
|
||||
isEmail: function (values, value) {
|
||||
return !value || value.match(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i);
|
||||
},
|
||||
'isTrue': function (values, value) {
|
||||
isTrue: function (values, value) {
|
||||
return value === true;
|
||||
},
|
||||
'isFalse': function (values, value) {
|
||||
isFalse: function (values, value) {
|
||||
return value === false;
|
||||
},
|
||||
'isNumeric': function (values, value) {
|
||||
isNumeric: function (values, value) {
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
return true;
|
||||
} else {
|
||||
var matchResults = value !== undefined && value.match(/[-+]?(\d*[.])?\d+/);
|
||||
var matchResults = value.match(/[-+]?(\d*[.])?\d+/);
|
||||
if (!!matchResults) {
|
||||
return matchResults[0] == value;
|
||||
} else {
|
||||
|
|
@ -35,17 +38,17 @@ module.exports = {
|
|||
}
|
||||
}
|
||||
},
|
||||
'isAlpha': function (values, value) {
|
||||
return !value || value.match(/^[a-zA-Z]+$/);
|
||||
isAlpha: function (values, value) {
|
||||
return value && /^[a-zA-Z]+$/.test(value);
|
||||
},
|
||||
'isWords': function (values, value) {
|
||||
return !value || value.match(/^[a-zA-Z\s]+$/);
|
||||
isWords: function (values, value) {
|
||||
return value && /^[a-zA-Z\s]+$/.test(value);
|
||||
},
|
||||
'isSpecialWords': function (values, value) {
|
||||
isSpecialWords: function (values, value) {
|
||||
return !value || value.match(/^[a-zA-Z\s\u00C0-\u017F]+$/);
|
||||
},
|
||||
isLength: function (values, value, length) {
|
||||
return value !== undefined && value.length === length;
|
||||
return value && value.length === length;
|
||||
},
|
||||
equals: function (values, value, eql) {
|
||||
return value == eql;
|
||||
|
|
@ -54,9 +57,9 @@ module.exports = {
|
|||
return value == values[field];
|
||||
},
|
||||
maxLength: function (values, value, length) {
|
||||
return value !== undefined && value.length <= length;
|
||||
return value && value.length && value.length <= length;
|
||||
},
|
||||
minLength: function (values, value, length) {
|
||||
return value !== undefined && value.length >= length;
|
||||
return value && value.length && value.length >= length;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue