/* * Copyright (C) 2008 - OQube / Arnaud Bailly */ function doTest(){ $ = jQuery; with (jqUnit) { module('base validators'); test('we have base validators available', function() { var regexp = new ValidateRegex("^\\d+\\w+$"); var check = regexp.isOk("113dfsf121"); var range = new ValidateRange().from(12).to(25); ok(range.isOk(14).result ,"'14' is ok"); ok(beANumber().isOk(14).result ,"14 is a number"); var not = new Not(new ValidateRange(10,13)); ok(not.isOk(13).result,"13 is ok"); ok(!not.isOk(10).result,"10 is ko"); var complex = new And(new Not(new ValidateRange(10,13)) ,new Or(new ValidateRange(7,10) ,new ValidateRange(13,18))); ok(complex.isOk(13).result,"13 is ok"); }); module('combinators'); test('we can use .and() to link base validators', function() { var range = new ValidateRange().from(12).to(25); var lb = new ValidateRange().from(12); var ub = new ValidateRange().to(25); equals(range.isOk(13).result, lb.and(ub).isOk(13).result, "13 is Ok with combinators"); }); test('we can use .or() to link base validators', function() { var range = new Not(new ValidateRange().from(12).to(25)); var lb = new ValidateRange().from(25); var ub = new ValidateRange().to(12); equals(range.isOk(12).result, lb.or(ub).isOk(12).result, "12 is KO with or combinators"); equals(range.isOk(11).result, lb.or(ub).isOk(11).result, "11 is OK with or combinators"); }); test('we can combine 2 different base combinators', function() { var lb = new ValidateRange().from(25); var ub = new ValidateRange().to(12); var checkNum = beANumber().and(lb.or(ub)); ok(!checkNum.isOk("toto").result, "'toto' is not a number"); ok(checkNum.isOk(11).result, "11 is OK with or combinators"); }); test('we can combine arbitrary expressions', function() { var complex = new Not(new ValidateRange(10,13)).and(new ValidateRange(7,10).or(new ValidateRange(13,18))); ok(complex.isOk(13).result,"13 is ok"); }); test('we can nest or/ands/nots in expressions (A and B) or (C and D)', function() { var complex = new ValidateRange().from(10).and(new ValidateRange().to(12)) .or(new ValidateRange().from(14).and(new ValidateRange().to(16))); ok(!complex.isOk(13).result,"13 is ko"); ok(complex.isOk(15).result,"15 is ok"); ok(complex.isOk(10).result,"10 is ok"); } ); test('we can use factory methods', function() { var complex = new ValidateRange().from(10).and(new ValidateRange().to(12)) .or(new Not(new ValidateRange().from(14).and(new ValidateRange().to(16)))); var sameComplex = range().from(10).and(range().to(12)).or(not(range().from(14).and(range().to(16)))); ok(complex.isOk(13).result,"13 is ok"); ok(!complex.isOk(15).result,"15 is ko"); ok(complex.isOk(10).result,"10 is ok"); } ); module("validate form"); test('we can check rules with jQuery and be passed a form', function() { expect(2); var rules = function(form) { equals(form.age.value,23,"age is 23"); return false; }; $('#testform').checkWith(rules).find('input[name=age]').val("23").end().submit(); equals($('#testform input[name=age]').val(),"23"); }); test('we can create a field with default rule', function() { var age = new Field("age"); ok(age.validate({age: 10}).result,"age of 10 is valid with default"); ok(age.validate({}).result,"age validate anything"); } ); test('we can attach a rule to a field', function() { var age = new Field("age"); age.must(beANumber().and(range().from(10).to(12))); ok(age.validate({age: 10}).result,"age of 10 is valid with default"); ok(!age.validate({}).result,"age is undefined"); ok(!age.validate({age: "toto"}).result,"age is not anumber"); } ); test("a field has a value", function() { var values = {age: 23}; var age = new Field("age",values); equals(age.value, 23,"age is 23"); }); test("a field has an undefined value", function() { var age = new Field("age"); equals(age.value, undefined,"age is undefined"); }); var rules = function(form) { with(form) { age.must(beANumber().and(range().from(18))); name.must(match("^[A-Z-]+$")); nbYearDL.must(beANumber().and(range().from(0).to(function() { return age.value - 18; }))); } }; test('checkWith invoke rules and validate form with errors', function() { $('#testform').checkWith(rules,'#errorslist'); $('#testform') .find('input[name=nbYearDL]').val("6").end() .find('input[name=age]').val("23").end() .find('input[name=name]').val("Dupont"); $('#testform').submit(); ok($('#errorslist>li').size() == 2,'two errors expected'); }); test('validate a form', function() { $('#testform').checkWith(rules,'#errorslist'); $('#testform') .find('input[name=nbYearDL]').val("6").end() .find('input[name=age]').val("27").end() .find('input[name=name]').val("DUPONT"); $('#testform').submit(); ok($('#errorslist>li').size() == 0,'no error expected'); }); } } jQuery(document).ready( function() { function waitForShow() { if(jqValidator) doTest(); else setTimeout(waitForShow,200); } setTimeout(waitForShow,200); } );