| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- function betweenCompareMin() {
- var compareTo = $('#betweenForm').find('[name="minAge"]').val();
- $('#msgMin').html('betweenCompareMin() called; compare to ' + compareTo);
- return compareTo;
- };
- function betweenCompareMax() {
- var compareTo = $('#betweenForm').find('[name="maxAge"]').val();
- $('#msgMax').html('betweenCompareMax() called; compare to ' + compareTo);
- return compareTo;
- };
- TestSuite = $.extend({}, TestSuite, {
- between: {
- compareToMin: function(value, validator, $field) {
- var compareTo = $('#betweenForm').find('[name="minAge"]').val();
- $('#msgMin').html('TestSuite.between.compareToMin() called; compare to ' + compareTo);
- return compareTo;
- },
- compareToMax: function(value, validator, $field) {
- var compareTo = $('#betweenForm').find('[name="maxAge"]').val();
- $('#msgMax').html('TestSuite.between.compareToMax() called; compare to ' + compareTo);
- return compareTo;
- }
- }
- });
- describe('between', function() {
- beforeEach(function() {
- $([
- '<form class="form-horizontal" id="betweenForm">',
- '<div id="msgMin"></div>',
- '<div id="msgMax"></div>',
- '<div class="form-group">',
- '<input type="text" name="minAge" />',
- '</div>',
- '<div class="form-group">',
- '<input type="text" name="maxAge" />',
- '</div>',
- '<div class="form-group">',
- '<input type="text" name="age" data-bv-between data-bv-between-min="18" data-bv-between-max="100" />',
- '</div>',
- '</form>'
- ].join('\n')).appendTo('body');
- $('#betweenForm').bootstrapValidator();
- this.bv = $('#betweenForm').data('bootstrapValidator');
- this.$minAge = this.bv.getFieldElements('minAge');
- this.$maxAge = this.bv.getFieldElements('maxAge');
- this.$age = this.bv.getFieldElements('age');
- });
- afterEach(function() {
- $('#betweenForm').bootstrapValidator('destroy').remove();
- });
- it('not a number', function() {
- this.$age.val('50abc');
- this.bv.validate();
- expect(this.bv.isValid()).toEqual(false);
- });
- it('compare to value', function() {
- this.$age.val(10);
- this.bv.validate();
- expect(this.bv.isValid()).toEqual(false);
- this.bv.resetForm();
- this.$age.val(120);
- this.bv.validate();
- expect(this.bv.isValid()).toEqual(false);
- this.bv.resetForm();
- this.$age.val(30);
- this.bv.validate();
- expect(this.bv.isValid()).toBeTruthy();
- });
- it('compare to other field', function() {
- this.bv.updateOption('age', 'between', 'min', 'minAge');
- this.bv.updateOption('age', 'between', 'max', 'maxAge');
- this.$minAge.val(2);
- this.$maxAge.val(10);
- this.$age.val(5);
- this.bv.validate();
- expect(this.bv.isValid()).toBeTruthy();
- this.bv.resetForm();
- this.$minAge.val(20);
- this.$maxAge.val(40);
- this.$age.val(50);
- this.bv.validate();
- expect(this.bv.isValid()).toEqual(false);
- expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
- });
- // #1048
- it('compare to other field that value has comma', function() {
- this.bv.updateOption('age', 'between', 'min', 'minAge');
- this.bv.updateOption('age', 'between', 'max', 'maxAge');
- this.$minAge.val('2,5');
- this.$maxAge.val('10,5');
- this.$age.val(5);
- this.bv.validate();
- expect(this.bv.isValid()).toBeTruthy();
- this.bv.resetForm();
- this.$minAge.val('20,5');
- this.$maxAge.val('40,5');
- this.$age.val(50);
- this.bv.validate();
- expect(this.bv.isValid()).toEqual(false);
- expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
- });
- it('compare to return value of a function', function() {
- this.bv.updateOption('age', 'between', 'min', 'betweenCompareMin');
- this.bv.updateOption('age', 'between', 'max', 'betweenCompareMax');
- this.$minAge.val(20);
- this.$maxAge.val(30);
- this.$age.val(18);
- this.bv.validate();
- expect($('#msgMin').html()).toEqual('betweenCompareMin() called; compare to 20');
- expect($('#msgMax').html()).toEqual('betweenCompareMax() called; compare to 30');
- expect(this.bv.isValid()).toEqual(false);
- expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
- this.bv.resetForm();
- this.$minAge.val(2);
- this.$maxAge.val(10);
- this.$age.val(6);
- this.bv.validate();
- expect($('#msgMin').html()).toEqual('betweenCompareMin() called; compare to 2');
- expect($('#msgMax').html()).toEqual('betweenCompareMax() called; compare to 10');
- expect(this.bv.isValid()).toBeTruthy();
- });
- it('compare to return value of a namespace function', function() {
- this.bv.updateOption('age', 'between', 'min', 'TestSuite.between.compareToMin');
- this.bv.updateOption('age', 'between', 'max', 'TestSuite.between.compareToMax');
- this.$minAge.val(20);
- this.$maxAge.val(30);
- this.$age.val(40);
- this.bv.validate();
- expect($('#msgMin').html()).toEqual('TestSuite.between.compareToMin() called; compare to 20');
- expect($('#msgMax').html()).toEqual('TestSuite.between.compareToMax() called; compare to 30');
- expect(this.bv.isValid()).toEqual(false);
- expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
- this.bv.resetForm();
- this.$minAge.val(2);
- this.$maxAge.val(10);
- this.$age.val(5);
- this.bv.validate();
- expect($('#msgMin').html()).toEqual('TestSuite.between.compareToMin() called; compare to 2');
- expect($('#msgMax').html()).toEqual('TestSuite.between.compareToMax() called; compare to 10');
- expect(this.bv.isValid()).toBeTruthy();
- });
- });
|