formsy-react/specs/Rules-minLength-spec.jsx

96 lines
2.4 KiB
JavaScript

var React = require('react/addons');
var TestUtils = React.addons.TestUtils;
var Formsy = require('./../src/main.js');
describe('Rules: minLength', function() {
var TestInput, isValid, form, input;
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 () {
isValid = this.isValid();
return <input value={this.getValue()} onChange={this.updateValue}/>
}
});
});
afterEach(function() {
TestInput = isValid = form = null;
});
describe('minLength:3', function() {
beforeEach(function() {
form = TestUtils.renderIntoDocument(
<Formsy.Form>
<TestInput name="foo" validations="minLength:3"/>
</Formsy.Form>
);
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
});
it('should pass with a default value', pass());
it('should fail when a string\'s length is smaller', fail('hi'));
it('should pass when a string\'s length is equal', pass('bar'));
it('should pass when a string\'s length is bigger', pass('myValue'));
it('should pass with an empty string', pass(''));
it('should pass with an undefined', pass(undefined));
it('should pass with a null', pass(null));
it('should fail with a number', fail(123));
});
describe('minLength:0', function() {
beforeEach(function() {
form = TestUtils.renderIntoDocument(
<Formsy.Form>
<TestInput name="foo" validations="minLength:0"/>
</Formsy.Form>
);
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
});
it('should pass with a default value', pass());
it('should pass when a string\'s length is bigger', pass('myValue'));
it('should pass with empty string', pass(''));
it('should pass with undefined', pass(undefined));
it('should pass with null', pass(null));
it('should fail with a number', fail(123));
});
});