2010-01-06 00:36:58 +00:00
|
|
|
ValidatorTest = TestCase('ValidatorTest');
|
|
|
|
|
|
2010-04-08 20:43:40 +00:00
|
|
|
ValidatorTest.prototype.testItShouldHaveThisSet = function() {
|
|
|
|
|
var validator = {};
|
2010-02-12 22:16:33 +00:00
|
|
|
angular.validator.myValidator = function(first, last){
|
2010-04-08 20:43:40 +00:00
|
|
|
validator.first = first;
|
|
|
|
|
validator.last = last;
|
|
|
|
|
validator._this = this;
|
2010-02-12 22:16:33 +00:00
|
|
|
};
|
2010-07-13 18:20:11 +00:00
|
|
|
var scope = compile('<input name="name" ng:validate="myValidator:\'hevery\'"/>');
|
2010-04-08 20:43:40 +00:00
|
|
|
scope.name = 'misko';
|
|
|
|
|
scope.$init();
|
|
|
|
|
assertEquals('misko', validator.first);
|
|
|
|
|
assertEquals('hevery', validator.last);
|
2010-04-21 19:50:05 +00:00
|
|
|
expect(validator._this.$id).toEqual(scope.$id);
|
2010-04-08 20:43:40 +00:00
|
|
|
delete angular.validator.myValidator;
|
|
|
|
|
scope.$element.remove();
|
2010-02-12 22:16:33 +00:00
|
|
|
};
|
|
|
|
|
|
2010-01-06 00:36:58 +00:00
|
|
|
ValidatorTest.prototype.testRegexp = function() {
|
|
|
|
|
assertEquals(angular.validator.regexp("abc", /x/, "E1"), "E1");
|
|
|
|
|
assertEquals(angular.validator.regexp("abc", '/x/'),
|
|
|
|
|
"Value does not match expected format /x/.");
|
|
|
|
|
assertEquals(angular.validator.regexp("ab", '^ab$'), null);
|
|
|
|
|
assertEquals(angular.validator.regexp("ab", '^axb$', "E3"), "E3");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ValidatorTest.prototype.testNumber = function() {
|
2010-03-24 23:13:42 +00:00
|
|
|
assertEquals(angular.validator.number("ab"), "Not a number");
|
2010-01-06 00:36:58 +00:00
|
|
|
assertEquals(angular.validator.number("-0.1",0), "Value can not be less than 0.");
|
|
|
|
|
assertEquals(angular.validator.number("10.1",0,10), "Value can not be greater than 10.");
|
|
|
|
|
assertEquals(angular.validator.number("1.2"), null);
|
|
|
|
|
assertEquals(angular.validator.number(" 1 ", 1, 1), null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ValidatorTest.prototype.testInteger = function() {
|
2010-03-24 23:13:42 +00:00
|
|
|
assertEquals(angular.validator.integer("ab"), "Not a number");
|
|
|
|
|
assertEquals(angular.validator.integer("1.1"), "Not a whole number");
|
|
|
|
|
assertEquals(angular.validator.integer("1.0"), "Not a whole number");
|
|
|
|
|
assertEquals(angular.validator.integer("1."), "Not a whole number");
|
2010-01-06 00:36:58 +00:00
|
|
|
assertEquals(angular.validator.integer("-1",0), "Value can not be less than 0.");
|
|
|
|
|
assertEquals(angular.validator.integer("11",0,10), "Value can not be greater than 10.");
|
|
|
|
|
assertEquals(angular.validator.integer("1"), null);
|
|
|
|
|
assertEquals(angular.validator.integer(" 1 ", 1, 1), null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ValidatorTest.prototype.testDate = function() {
|
|
|
|
|
var error = "Value is not a date. (Expecting format: 12/31/2009).";
|
2010-09-23 11:48:17 +00:00
|
|
|
expect(angular.validator.date("ab")).toEqual(error);
|
|
|
|
|
expect(angular.validator.date("12/31/2009")).toEqual(null);
|
|
|
|
|
expect(angular.validator.date("1/1/1000")).toEqual(null);
|
|
|
|
|
expect(angular.validator.date("12/31/9999")).toEqual(null);
|
|
|
|
|
expect(angular.validator.date("2/29/2004")).toEqual(null);
|
|
|
|
|
expect(angular.validator.date("2/29/2000")).toEqual(null);
|
|
|
|
|
expect(angular.validator.date("2/29/2100")).toEqual(error);
|
|
|
|
|
expect(angular.validator.date("2/29/2003")).toEqual(error);
|
|
|
|
|
expect(angular.validator.date("41/1/2009")).toEqual(error);
|
|
|
|
|
expect(angular.validator.date("13/1/2009")).toEqual(error);
|
|
|
|
|
expect(angular.validator.date("1/1/209")).toEqual(error);
|
|
|
|
|
expect(angular.validator.date("1/32/2010")).toEqual(error);
|
|
|
|
|
expect(angular.validator.date("001/031/2009")).toEqual(error);
|
2010-01-06 00:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ValidatorTest.prototype.testPhone = function() {
|
|
|
|
|
var error = "Phone number needs to be in 1(987)654-3210 format in North America or +999 (123) 45678 906 internationaly.";
|
|
|
|
|
assertEquals(angular.validator.phone("ab"), error);
|
|
|
|
|
assertEquals(null, angular.validator.phone("1(408)757-3023"));
|
|
|
|
|
assertEquals(null, angular.validator.phone("+421 (0905) 933 297"));
|
|
|
|
|
assertEquals(null, angular.validator.phone("+421 0905 933 297"));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ValidatorTest.prototype.testSSN = function() {
|
|
|
|
|
var error = "SSN needs to be in 999-99-9999 format.";
|
|
|
|
|
assertEquals(angular.validator.ssn("ab"), error);
|
|
|
|
|
assertEquals(angular.validator.ssn("123-45-6789"), null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ValidatorTest.prototype.testURL = function() {
|
|
|
|
|
var error = "URL needs to be in http://server[:port]/path format.";
|
|
|
|
|
assertEquals(angular.validator.url("ab"), error);
|
|
|
|
|
assertEquals(angular.validator.url("http://server:123/path"), null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ValidatorTest.prototype.testEmail = function() {
|
|
|
|
|
var error = "Email needs to be in username@host.com format.";
|
|
|
|
|
assertEquals(error, angular.validator.email("ab"));
|
|
|
|
|
assertEquals(null, angular.validator.email("misko@hevery.com"));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ValidatorTest.prototype.testJson = function() {
|
|
|
|
|
assertNotNull(angular.validator.json("'"));
|
|
|
|
|
assertNotNull(angular.validator.json("''X"));
|
|
|
|
|
assertNull(angular.validator.json("{}"));
|
|
|
|
|
};
|
2010-02-13 03:39:01 +00:00
|
|
|
|
|
|
|
|
describe('Validator:asynchronous', function(){
|
|
|
|
|
var asynchronous = angular.validator.asynchronous;
|
|
|
|
|
var self;
|
|
|
|
|
var value, fn;
|
2010-03-24 23:13:42 +00:00
|
|
|
|
2010-02-13 03:39:01 +00:00
|
|
|
beforeEach(function(){
|
|
|
|
|
value = null;
|
|
|
|
|
fn = null;
|
2010-10-11 21:36:38 +00:00
|
|
|
self = compile('<input />');
|
|
|
|
|
jqLite(document.body).append(self.$element);
|
2010-04-16 21:01:29 +00:00
|
|
|
self.$element.data('$validate', noop);
|
|
|
|
|
self.$root = self;
|
2010-10-11 21:36:38 +00:00
|
|
|
self.$init();
|
2010-02-13 03:39:01 +00:00
|
|
|
});
|
2010-03-24 23:13:42 +00:00
|
|
|
|
2010-03-30 04:36:34 +00:00
|
|
|
afterEach(function(){
|
|
|
|
|
if (self.$element) self.$element.remove();
|
|
|
|
|
var oldCache = jqCache;
|
|
|
|
|
jqCache = {};
|
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject
- inject method can return, instance, or call function which have $inject
property
- initialize services with $creation=[eager|eager-publish] this means that
only some of the services are now globally accessible
- upgraded $become on scope to use injector hence respect the $inject property
for injection
- $become should not be run multiple times and will most likely be removed
in future version
- added $new on scope to create a child scope
- $inject is respected on constructor function
- simplified scopes so that they no longer have separate __proto__ for
parent, api, behavior and instance this should speed up execution since
scope will now create one __proto__ chain per scope (not three).
BACKWARD COMPATIBILITY WARNING:
- services now need to have $inject instead of inject property for proper
injection this breaks backward compatibility
- not all services are now published into root scope
(only: $location, $cookie, $window)
- if you have widget/directive which uses services on scope
(such as this.$xhr), you will now have to inject that service in
(as it is not published on the root scope anymore)
2010-10-09 00:30:13 +00:00
|
|
|
if (size(oldCache)) {
|
|
|
|
|
dump(oldCache);
|
|
|
|
|
}
|
2010-03-30 04:36:34 +00:00
|
|
|
expect(size(oldCache)).toEqual(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should make a request and show spinner', function(){
|
|
|
|
|
var value, fn;
|
2010-07-13 18:20:11 +00:00
|
|
|
var scope = compile('<input type="text" name="name" ng:validate="asynchronous:asyncFn"/>');
|
2010-10-11 21:36:38 +00:00
|
|
|
jqLite(document.body).append(scope.$element);
|
2010-03-30 04:36:34 +00:00
|
|
|
scope.$init();
|
|
|
|
|
var input = scope.$element;
|
|
|
|
|
scope.asyncFn = function(v,f){
|
|
|
|
|
value=v; fn=f;
|
|
|
|
|
};
|
|
|
|
|
scope.name = "misko";
|
|
|
|
|
scope.$eval();
|
2010-02-13 03:39:01 +00:00
|
|
|
expect(value).toEqual('misko');
|
|
|
|
|
expect(input.hasClass('ng-input-indicator-wait')).toBeTruthy();
|
|
|
|
|
fn("myError");
|
|
|
|
|
expect(input.hasClass('ng-input-indicator-wait')).toBeFalsy();
|
2010-04-16 21:01:29 +00:00
|
|
|
expect(input.attr(NG_VALIDATION_ERROR)).toEqual("myError");
|
2010-03-30 04:36:34 +00:00
|
|
|
scope.$element.remove();
|
2010-02-13 03:39:01 +00:00
|
|
|
});
|
2010-03-24 23:13:42 +00:00
|
|
|
|
2010-02-13 03:39:01 +00:00
|
|
|
it("should not make second request to same value", function(){
|
|
|
|
|
asynchronous.call(self, "kai", function(v,f){value=v; fn=f;});
|
|
|
|
|
expect(value).toEqual('kai');
|
2010-04-16 21:01:29 +00:00
|
|
|
expect(self.$invalidWidgets[0]).toEqual(self.$element);
|
2010-03-24 23:13:42 +00:00
|
|
|
|
2010-02-13 03:39:01 +00:00
|
|
|
var spy = jasmine.createSpy();
|
|
|
|
|
asynchronous.call(self, "kai", spy);
|
|
|
|
|
expect(spy).wasNotCalled();
|
2010-03-24 23:13:42 +00:00
|
|
|
|
2010-02-13 03:39:01 +00:00
|
|
|
asynchronous.call(self, "misko", spy);
|
2010-03-24 23:13:42 +00:00
|
|
|
expect(spy).wasCalled();
|
2010-02-13 03:39:01 +00:00
|
|
|
});
|
2010-03-24 23:13:42 +00:00
|
|
|
|
2010-02-13 03:39:01 +00:00
|
|
|
it("should ignore old callbacks, and not remove spinner", function(){
|
|
|
|
|
var firstCb, secondCb;
|
|
|
|
|
asynchronous.call(self, "first", function(v,f){value=v; firstCb=f;});
|
|
|
|
|
asynchronous.call(self, "second", function(v,f){value=v; secondCb=f;});
|
2010-03-24 23:13:42 +00:00
|
|
|
|
2010-02-13 03:39:01 +00:00
|
|
|
firstCb();
|
2010-04-16 21:01:29 +00:00
|
|
|
expect(self.$element.hasClass('ng-input-indicator-wait')).toBeTruthy();
|
2010-03-24 23:13:42 +00:00
|
|
|
|
2010-02-13 03:39:01 +00:00
|
|
|
secondCb();
|
2010-04-16 21:01:29 +00:00
|
|
|
expect(self.$element.hasClass('ng-input-indicator-wait')).toBeFalsy();
|
2010-02-13 03:39:01 +00:00
|
|
|
});
|
2010-04-16 21:01:29 +00:00
|
|
|
|
|
|
|
|
it("should handle update function", function(){
|
2010-07-13 18:20:11 +00:00
|
|
|
var scope = angular.compile('<input name="name" ng:validate="asynchronous:asyncFn:updateFn"/>');
|
2010-04-16 21:01:29 +00:00
|
|
|
scope.asyncFn = jasmine.createSpy();
|
|
|
|
|
scope.updateFn = jasmine.createSpy();
|
|
|
|
|
scope.name = 'misko';
|
|
|
|
|
scope.$init();
|
|
|
|
|
scope.$eval();
|
|
|
|
|
expect(scope.asyncFn).wasCalledWith('misko', scope.asyncFn.mostRecentCall.args[1]);
|
|
|
|
|
assertTrue(scope.$element.hasClass('ng-input-indicator-wait'));
|
|
|
|
|
scope.asyncFn.mostRecentCall.args[1]('myError', {id: 1234, data:'data'});
|
|
|
|
|
assertFalse(scope.$element.hasClass('ng-input-indicator-wait'));
|
|
|
|
|
assertEquals('myError', scope.$element.attr('ng-validation-error'));
|
|
|
|
|
expect(scope.updateFn.mostRecentCall.args[0]).toEqual({id: 1234, data:'data'});
|
|
|
|
|
scope.$element.remove();
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-24 23:13:42 +00:00
|
|
|
});
|