Skip to content

Commit cd37020

Browse files
Merge pull request christianalfoni#105 from erwanjegouzo/master
improving validation rules + unit tests
2 parents e4b5dbf + 8f4e256 commit cd37020

11 files changed

+599
-182
lines changed

specs/Rules-equals-spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var Formsy = require('./../src/main.js');
2+
3+
describe('Rules: equals', function() {
4+
var TestInput, isValid, form, input;
5+
6+
beforeEach(function() {
7+
isValid = jasmine.createSpy('valid');
8+
9+
TestInput = React.createClass({
10+
mixins: [Formsy.Mixin],
11+
updateValue: function (event) {
12+
this.setValue(event.target.value);
13+
},
14+
render: function () {
15+
if (this.isValid()) {
16+
isValid();
17+
}
18+
return <input value={this.getValue()} onChange={this.updateValue}/>
19+
}
20+
});
21+
22+
form = TestUtils.renderIntoDocument(
23+
<Formsy.Form>
24+
<TestInput name="foo" validations="equals:myValue"/>
25+
</Formsy.Form>
26+
);
27+
28+
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
29+
30+
});
31+
32+
afterEach(function() {
33+
TestInput = isValid = isInvalid = form = null;
34+
});
35+
36+
it('should fail with undefined', function () {
37+
expect(isValid).not.toHaveBeenCalled();
38+
TestUtils.Simulate.change(input, {target: {value: undefined}});
39+
expect(isValid).not.toHaveBeenCalled();
40+
});
41+
42+
it('should fail with null', function () {
43+
expect(isValid).not.toHaveBeenCalled();
44+
TestUtils.Simulate.change(input, {target: {value: null}});
45+
expect(isValid).not.toHaveBeenCalled();
46+
});
47+
48+
it('should fail when the value is not equal', function () {
49+
expect(isValid).not.toHaveBeenCalled();
50+
TestUtils.Simulate.change(input, {target: {value: 'foo'}});
51+
expect(isValid).not.toHaveBeenCalled();
52+
});
53+
54+
it('should pass when the value is equal', function () {
55+
expect(isValid).not.toHaveBeenCalled();
56+
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
57+
expect(isValid).toHaveBeenCalled();
58+
});
59+
60+
});

specs/Rules-hasValue-spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var Formsy = require('./../src/main.js');
2+
3+
describe('Rules: hasValue', function() {
4+
var TestInput, isValid, form, input;
5+
6+
beforeEach(function() {
7+
isValid = jasmine.createSpy('valid');
8+
9+
TestInput = React.createClass({
10+
mixins: [Formsy.Mixin],
11+
updateValue: function (event) {
12+
this.setValue(event.target.value);
13+
},
14+
render: function () {
15+
if (this.isValid()) {
16+
isValid();
17+
}
18+
return <input value={this.getValue()} onChange={this.updateValue}/>
19+
}
20+
});
21+
22+
form = TestUtils.renderIntoDocument(
23+
<Formsy.Form>
24+
<TestInput name="foo" validations="hasValue"/>
25+
</Formsy.Form>
26+
);
27+
28+
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
29+
30+
});
31+
32+
afterEach(function() {
33+
TestInput = isValid = isInvalid = form = null;
34+
});
35+
36+
it('should fail with undefined', function () {
37+
expect(isValid).not.toHaveBeenCalled();
38+
TestUtils.Simulate.change(input, {target: {value: undefined}});
39+
expect(isValid).not.toHaveBeenCalled();
40+
});
41+
42+
it('should fail with null', function () {
43+
expect(isValid).not.toHaveBeenCalled();
44+
TestUtils.Simulate.change(input, {target: {value: null}});
45+
expect(isValid).not.toHaveBeenCalled();
46+
});
47+
48+
it('should pass with a string', function () {
49+
expect(isValid).not.toHaveBeenCalled();
50+
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
51+
expect(isValid).toHaveBeenCalled();
52+
});
53+
54+
});

specs/Rules-isAlpha-spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var Formsy = require('./../src/main.js');
2+
3+
describe('Rules: isAlpha', function() {
4+
var TestInput, isValid, form, input;
5+
6+
beforeEach(function() {
7+
isValid = jasmine.createSpy('valid');
8+
9+
TestInput = React.createClass({
10+
mixins: [Formsy.Mixin],
11+
updateValue: function (event) {
12+
this.setValue(event.target.value);
13+
},
14+
render: function () {
15+
if (this.isValid()) {
16+
isValid();
17+
}
18+
return <input value={this.getValue()} onChange={this.updateValue}/>
19+
}
20+
});
21+
22+
form = TestUtils.renderIntoDocument(
23+
<Formsy.Form>
24+
<TestInput name="foo" validations="isAlpha"/>
25+
</Formsy.Form>
26+
);
27+
28+
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
29+
30+
});
31+
32+
afterEach(function() {
33+
TestInput = isValid = isInvalid = form = null;
34+
});
35+
36+
it('should fail with undefined', function () {
37+
expect(isValid).not.toHaveBeenCalled();
38+
TestUtils.Simulate.change(input, {target: {value: undefined}});
39+
expect(isValid).not.toHaveBeenCalled();
40+
});
41+
42+
it('should fail with null', function () {
43+
expect(isValid).not.toHaveBeenCalled();
44+
TestUtils.Simulate.change(input, {target: {value: null}});
45+
expect(isValid).not.toHaveBeenCalled();
46+
});
47+
48+
it('should fail with a number', function () {
49+
expect(isValid).not.toHaveBeenCalled();
50+
TestUtils.Simulate.change(input, {target: {value: 123}});
51+
expect(isValid).not.toHaveBeenCalled();
52+
});
53+
54+
it('should pass with a string', function () {
55+
expect(isValid).not.toHaveBeenCalled();
56+
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
57+
expect(isValid).toHaveBeenCalled();
58+
});
59+
60+
});

specs/Rules-isEmail-spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var Formsy = require('./../src/main.js');
2+
3+
describe('Rules: isEmail', function() {
4+
var TestInput, isValid, form, input;
5+
6+
beforeEach(function() {
7+
isValid = jasmine.createSpy('valid');
8+
9+
TestInput = React.createClass({
10+
mixins: [Formsy.Mixin],
11+
updateValue: function (event) {
12+
this.setValue(event.target.value);
13+
},
14+
render: function () {
15+
if (this.isValid()) {
16+
isValid();
17+
}
18+
return <input value={this.getValue()} onChange={this.updateValue}/>
19+
}
20+
});
21+
22+
form = TestUtils.renderIntoDocument(
23+
<Formsy.Form>
24+
<TestInput name="foo" value="foo" validations="isEmail"/>
25+
</Formsy.Form>
26+
);
27+
28+
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
29+
30+
});
31+
32+
afterEach(function() {
33+
TestInput = isValid = isInvalid = form = null;
34+
});
35+
36+
it('should fail with "foo"', function () {
37+
expect(isValid).not.toHaveBeenCalled();
38+
TestUtils.Simulate.change(input, {target: {value: 'foo'}});
39+
expect(isValid).not.toHaveBeenCalled();
40+
});
41+
42+
it('should pass with "foo@foo.com"', function () {
43+
expect(isValid).not.toHaveBeenCalled();
44+
TestUtils.Simulate.change(input, {target: {value: 'foo@foo.com'}});
45+
expect(isValid).toHaveBeenCalled();
46+
});
47+
48+
});

specs/Rules-isLength-spec.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var Formsy = require('./../src/main.js');
2+
3+
describe('Rules: isLength', function() {
4+
var TestInput, isValid, form, input;
5+
6+
beforeEach(function() {
7+
isValid = jasmine.createSpy('valid');
8+
9+
TestInput = React.createClass({
10+
mixins: [Formsy.Mixin],
11+
updateValue: function (event) {
12+
this.setValue(event.target.value);
13+
},
14+
render: function () {
15+
if (this.isValid()) {
16+
isValid();
17+
}
18+
return <input value={this.getValue()} onChange={this.updateValue}/>
19+
}
20+
});
21+
22+
form = TestUtils.renderIntoDocument(
23+
<Formsy.Form>
24+
<TestInput name="foo" validations="isLength:3"/>
25+
</Formsy.Form>
26+
);
27+
28+
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
29+
30+
});
31+
32+
afterEach(function() {
33+
TestInput = isValid = isInvalid = form = null;
34+
});
35+
36+
it('should fail with undefined', function () {
37+
expect(isValid).not.toHaveBeenCalled();
38+
TestUtils.Simulate.change(input, {target: {value: undefined}});
39+
expect(isValid).not.toHaveBeenCalled();
40+
});
41+
42+
it('should fail with null', function () {
43+
expect(isValid).not.toHaveBeenCalled();
44+
TestUtils.Simulate.change(input, {target: {value: null}});
45+
expect(isValid).not.toHaveBeenCalled();
46+
});
47+
48+
it('should fail with a number', function () {
49+
expect(isValid).not.toHaveBeenCalled();
50+
TestUtils.Simulate.change(input, {target: {value: 123}});
51+
expect(isValid).not.toHaveBeenCalled();
52+
});
53+
54+
it('should fail with a string too small', function () {
55+
expect(isValid).not.toHaveBeenCalled();
56+
TestUtils.Simulate.change(input, {target: {value: "hi"}});
57+
expect(isValid).not.toHaveBeenCalled();
58+
});
59+
60+
it('should fail with a string too long', function () {
61+
expect(isValid).not.toHaveBeenCalled();
62+
TestUtils.Simulate.change(input, {target: {value: "foo bar"}});
63+
expect(isValid).not.toHaveBeenCalled();
64+
});
65+
66+
it('should pass with the right length', function () {
67+
expect(isValid).not.toHaveBeenCalled();
68+
TestUtils.Simulate.change(input, {target: {value: 'sup'}});
69+
expect(isValid).toHaveBeenCalled();
70+
});
71+
72+
});

specs/Rules-isNumeric-spec.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var Formsy = require('./../src/main.js');
2+
3+
describe('Rules: isNumeric', function() {
4+
var TestInput, isValid, form, input;
5+
6+
beforeEach(function() {
7+
isValid = jasmine.createSpy('valid');
8+
9+
TestInput = React.createClass({
10+
mixins: [Formsy.Mixin],
11+
updateValue: function (event) {
12+
this.setValue(event.target.value);
13+
},
14+
render: function () {
15+
if (this.isValid()) {
16+
isValid();
17+
}
18+
return <input value={this.getValue()} onChange={this.updateValue}/>
19+
}
20+
});
21+
22+
form = TestUtils.renderIntoDocument(
23+
<Formsy.Form>
24+
<TestInput name="foo" validations="isNumeric"/>
25+
</Formsy.Form>
26+
);
27+
28+
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
29+
30+
});
31+
32+
afterEach(function() {
33+
TestInput = isValid = isInvalid = form = null;
34+
});
35+
36+
it('should fail with undefined', function () {
37+
expect(isValid).not.toHaveBeenCalled();
38+
TestUtils.Simulate.change(input, {target: {value: undefined}});
39+
expect(isValid).not.toHaveBeenCalled();
40+
});
41+
42+
it('should fail with null', function () {
43+
expect(isValid).not.toHaveBeenCalled();
44+
TestUtils.Simulate.change(input, {target: {value: null}});
45+
expect(isValid).not.toHaveBeenCalled();
46+
});
47+
48+
it('should fail with a string', function () {
49+
expect(isValid).not.toHaveBeenCalled();
50+
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
51+
expect(isValid).not.toHaveBeenCalled();
52+
});
53+
54+
it('should pass with a number as string', function () {
55+
expect(isValid).not.toHaveBeenCalled();
56+
TestUtils.Simulate.change(input, {target: {value: '123'}});
57+
expect(isValid).toHaveBeenCalled();
58+
});
59+
60+
it('should pass with an int', function () {
61+
expect(isValid).not.toHaveBeenCalled();
62+
TestUtils.Simulate.change(input, {target: {value: 123}});
63+
expect(isValid).toHaveBeenCalled();
64+
});
65+
66+
it('should pass with a float', function () {
67+
expect(isValid).not.toHaveBeenCalled();
68+
TestUtils.Simulate.change(input, {target: {value: 1.23}});
69+
expect(isValid).toHaveBeenCalled();
70+
});
71+
72+
});

0 commit comments

Comments
 (0)