Skip to content

Commit

Permalink
Changed validation behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Semigradsky committed Jun 3, 2015
1 parent d7e7dfc commit 0395f61
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 475 deletions.
54 changes: 24 additions & 30 deletions specs/Rules-equals-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ var Formsy = require('./../src/main.js');
describe('Rules: equals', function() {
var TestInput, isValid, form, input;

beforeEach(function() {
isValid = jasmine.createSpy('valid');
function pass(value) {
return pass.length ? function () {
TestUtils.Simulate.change(input, {target: {value: value}});
expect(isValid).toBe(true);
} : function () { expect(isValid).toBe(true); };
}

function fail(value) {
return fail.length ? function () {
TestUtils.Simulate.change(input, {target: {value: value}});
expect(isValid).toBe(false);
} : function () { expect(isValid).toBe(false); };
}

beforeEach(function() {
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
},
render: function () {
if (this.isValid()) {
isValid();
}
isValid = this.isValid();
return <input value={this.getValue()} onChange={this.updateValue}/>
}
});
Expand All @@ -35,34 +45,18 @@ describe('Rules: equals', function() {
TestInput = isValid = isInvalid = form = null;
});

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 with a default value', pass());

it('should pass when the value is equal', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
expect(isValid).toHaveBeenCalled();
});
it('should fail when the value is not equal', fail('foo'));

it('should fail with an undefined', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: undefined}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass when the value is equal', pass('myValue'));

it('should fail with a null', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: null}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with an empty string', pass(''));

it('should fail with a number', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 42}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with an undefined', pass(undefined));

it('should pass with a null', pass(null));

it('should fail with a number', fail(42));

});
58 changes: 23 additions & 35 deletions specs/Rules-isAlpha-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ var Formsy = require('./../src/main.js');
describe('Rules: isAlpha', function() {
var TestInput, isValid, form, input;

beforeEach(function() {
isValid = jasmine.createSpy('valid');
function pass(value) {
return pass.length ? function () {
TestUtils.Simulate.change(input, {target: {value: value}});
expect(isValid).toBe(true);
} : function () { expect(isValid).toBe(true); };
}

function fail(value) {
return fail.length ? function () {
TestUtils.Simulate.change(input, {target: {value: value}});
expect(isValid).toBe(false);
} : function () { expect(isValid).toBe(false); };
}

beforeEach(function() {
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
},
render: function () {
if (this.isValid()) {
isValid();
}
isValid = this.isValid();
return <input value={this.getValue()} onChange={this.updateValue}/>
}
});
Expand All @@ -35,40 +45,18 @@ describe('Rules: isAlpha', function() {
TestInput = isValid = isInvalid = form = null;
});

it('should pass with a string is only latin letters', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 'myValue'}});
expect(isValid).toHaveBeenCalled();
});
it('should pass with a default value', pass());

it('should fail with a string with numbers', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 'myValue 42'}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with a string is only latin letters', pass('myValue'));

it('should fail with an undefined', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: undefined}});
expect(isValid).not.toHaveBeenCalled();
});
it('should fail with a string with numbers', fail('myValue 42'));

it('should fail with a null', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: null}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with an undefined', pass(undefined));

it('should fail with a number', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 42}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with a null', pass(null));

it('should fail with an empty string', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: ''}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with an empty string', pass(''));

it('should fail with a number', fail(42));

});
58 changes: 23 additions & 35 deletions specs/Rules-isEmail-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ var Formsy = require('./../src/main.js');
describe('Rules: isEmail', function() {
var TestInput, isValid, form, input;

beforeEach(function() {
isValid = jasmine.createSpy('valid');
function pass(value) {
return pass.length ? function () {
TestUtils.Simulate.change(input, {target: {value: value}});
expect(isValid).toBe(true);
} : function () { expect(isValid).toBe(true); };
}

function fail(value) {
return fail.length ? function () {
TestUtils.Simulate.change(input, {target: {value: value}});
expect(isValid).toBe(false);
} : function () { expect(isValid).toBe(false); };
}

beforeEach(function() {
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
},
render: function () {
if (this.isValid()) {
isValid();
}
isValid = this.isValid();
return <input value={this.getValue()} onChange={this.updateValue}/>
}
});
Expand All @@ -35,40 +45,18 @@ describe('Rules: isEmail', 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 a default value', pass());

it('should pass with "[email protected]"', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: '[email protected]'}});
expect(isValid).toHaveBeenCalled();
});
it('should fail with "foo"', fail('foo'));

it('should fail with an undefined', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: undefined}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with "[email protected]"', pass('[email protected]'));

it('should fail with a null', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: null}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with an undefined', pass(undefined));

it('should fail with a number', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 42}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with a null', pass(null));

it('should fail with an empty string', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: ''}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with an empty string', pass(''));

it('should fail with a number', fail(42));

});
58 changes: 23 additions & 35 deletions specs/Rules-isEmptyString-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ var Formsy = require('./../src/main.js');
describe('Rules: isEmptyString', function() {
var TestInput, isValid, form, input;

beforeEach(function() {
isValid = jasmine.createSpy('valid');
function pass(value) {
return pass.length ? function () {
TestUtils.Simulate.change(input, {target: {value: value}});
expect(isValid).toBe(true);
} : function () { expect(isValid).toBe(true); };
}

function fail(value) {
return fail.length ? function () {
TestUtils.Simulate.change(input, {target: {value: value}});
expect(isValid).toBe(false);
} : function () { expect(isValid).toBe(false); };
}

beforeEach(function() {
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
},
render: function () {
if (this.isValid()) {
isValid();
}
isValid = this.isValid();
return <input value={this.getValue()} onChange={this.updateValue}/>
}
});
Expand All @@ -35,40 +45,18 @@ describe('Rules: isEmptyString', function() {
TestInput = isValid = isInvalid = form = null;
});

it('should fail with non-empty string', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 'asd'}});
expect(isValid).not.toHaveBeenCalled();
});
it('should fail with a default value', fail());

it('should pass with an empty string', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: ''}});
expect(isValid).toHaveBeenCalled();
});
it('should fail with non-empty string', fail('asd'));

it('should fail with a undefined', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: undefined}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with an empty string', pass(''));

it('should fail with a null', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: null}});
expect(isValid).not.toHaveBeenCalled();
});
it('should fail with a undefined', fail(undefined));

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 null', fail(null));

it('should fail with a zero', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 0}});
expect(isValid).not.toHaveBeenCalled();
});
it('should fail with a number', fail(123));

it('should fail with a zero', fail(0));

});
Loading

0 comments on commit 0395f61

Please sign in to comment.