From 1990cbbf2817e04657ccd616da1d9d6b78cc2949 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 25 Mar 2010 22:07:36 -0700 Subject: added few extra tests --- test/ScopeSpec.js | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 test/ScopeSpec.js (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js new file mode 100644 index 00000000..0a0b4241 --- /dev/null +++ b/test/ScopeSpec.js @@ -0,0 +1,90 @@ +describe('scope/model', function(){ + + it('should create a scope with parent', function(){ + var model = scope({name:'Misko'}); + expect(model.name).toEqual('Misko'); + }); + + it('should have $get/set$/parent$', function(){ + var parent = {}; + var model = scope(parent); + model.$set('name', 'adam'); + expect(model.name).toEqual('adam'); + expect(model.$get('name')).toEqual('adam'); + expect(model.$parent).toEqual(parent); + }); + + //$eval + it('should eval function with correct this and pass arguments', function(){ + var model = scope(); + model.$eval(function(name){ + this.name = name; + }, 'works'); + expect(model.name).toEqual('works'); + }); + + it('should eval expression with correct this', function(){ + var model = scope(); + model.$eval('name="works"'); + expect(model.name).toEqual('works'); + }); + + //$onEval + it('should watch an expression for change', function(){ + var model = scope(); + model.oldValue = ""; + var count = 0; + model.name = 'adam'; + model.$watch('name', function(){ count ++; }); + model.$watch(function(){return model.name;}, function(newValue, oldValue){ + this.newValue = newValue; + this.oldValue = oldValue; + }); + model.name = 'misko'; + model.$eval(); + expect(count).toEqual(1); + expect(model.newValue).toEqual('misko'); + expect(model.oldValue).toEqual('adam'); + }); + + it('should eval with no arguments', function(){ + var model = scope(); + var count = 0; + model.$onEval(function(){count++;}); + model.$eval(); + expect(count).toEqual(1); + }); + + //$bind + it('should curry a function with respect to scope', function(){ + var model = scope(); + model.name = 'misko'; + expect(model.$bind(function(){return this.name;})()).toEqual('misko'); + }); + + //$behavior + it('should behave as class', function(){ + function Printer(brand){ + this.brand = brand; + }; + Printer.prototype.print = function(){ + this.printed = true; + }; + var model = scope({ name: 'parent' }, Printer, 'hp'); + expect(model.brand).toEqual('hp'); + model.print(); + expect(model.printed).toEqual(true); + }); + + + + //$tryEval + it('should report error on element', function(){ + + }); + + it('should report error on visible element', function(){ + + }); + +}); -- cgit v1.2.3 From 258ca5f16581f0e8befa493644225a02ae2fc002 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 26 Mar 2010 16:27:18 -0700 Subject: moved all uneeded files out, widgets.html works, tests horribly broken --- test/ScopeSpec.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index 0a0b4241..cfae42a8 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -1,13 +1,13 @@ describe('scope/model', function(){ it('should create a scope with parent', function(){ - var model = scope({name:'Misko'}); + var model = createScope({name:'Misko'}); expect(model.name).toEqual('Misko'); }); it('should have $get/set$/parent$', function(){ var parent = {}; - var model = scope(parent); + var model = createScope(parent); model.$set('name', 'adam'); expect(model.name).toEqual('adam'); expect(model.$get('name')).toEqual('adam'); @@ -16,7 +16,7 @@ describe('scope/model', function(){ //$eval it('should eval function with correct this and pass arguments', function(){ - var model = scope(); + var model = createScope(); model.$eval(function(name){ this.name = name; }, 'works'); @@ -24,14 +24,14 @@ describe('scope/model', function(){ }); it('should eval expression with correct this', function(){ - var model = scope(); + var model = createScope(); model.$eval('name="works"'); expect(model.name).toEqual('works'); }); //$onEval it('should watch an expression for change', function(){ - var model = scope(); + var model = createScope(); model.oldValue = ""; var count = 0; model.name = 'adam'; @@ -48,7 +48,7 @@ describe('scope/model', function(){ }); it('should eval with no arguments', function(){ - var model = scope(); + var model = createScope(); var count = 0; model.$onEval(function(){count++;}); model.$eval(); @@ -57,7 +57,7 @@ describe('scope/model', function(){ //$bind it('should curry a function with respect to scope', function(){ - var model = scope(); + var model = createScope(); model.name = 'misko'; expect(model.$bind(function(){return this.name;})()).toEqual('misko'); }); @@ -70,7 +70,7 @@ describe('scope/model', function(){ Printer.prototype.print = function(){ this.printed = true; }; - var model = scope({ name: 'parent' }, Printer, 'hp'); + var model = createScope({ name: 'parent' }, Printer, 'hp'); expect(model.brand).toEqual('hp'); model.print(); expect(model.printed).toEqual(true); -- cgit v1.2.3 From d2d356918bd1c0c76673d22ff85c617fbd85d40e Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Mon, 29 Mar 2010 21:49:12 -0700 Subject: reenabled more tests --- test/ScopeSpec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index cfae42a8..1e50b275 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -29,7 +29,7 @@ describe('scope/model', function(){ expect(model.name).toEqual('works'); }); - //$onEval + //$watch it('should watch an expression for change', function(){ var model = createScope(); model.oldValue = ""; @@ -42,7 +42,7 @@ describe('scope/model', function(){ }); model.name = 'misko'; model.$eval(); - expect(count).toEqual(1); + expect(count).toEqual(2); // since watches trigger $eval expect(model.newValue).toEqual('misko'); expect(model.oldValue).toEqual('adam'); }); -- cgit v1.2.3 From a7d62dcb5533ceb9a7ae47ee27e2054400a0196b Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 30 Mar 2010 14:55:04 -0700 Subject: more tests fixed --- test/ScopeSpec.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index 1e50b275..7e1a899f 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -33,16 +33,18 @@ describe('scope/model', function(){ it('should watch an expression for change', function(){ var model = createScope(); model.oldValue = ""; - var count = 0; + var nameCount = 0, evalCount = 0; model.name = 'adam'; - model.$watch('name', function(){ count ++; }); + model.$watch('name', function(){ nameCount ++; }); model.$watch(function(){return model.name;}, function(newValue, oldValue){ this.newValue = newValue; this.oldValue = oldValue; }); + model.$onEval(function(){evalCount ++;}); model.name = 'misko'; model.$eval(); - expect(count).toEqual(2); // since watches trigger $eval + expect(nameCount).toEqual(1); + expect(evalCount).toEqual(1); expect(model.newValue).toEqual('misko'); expect(model.oldValue).toEqual('adam'); }); -- cgit v1.2.3 From 11a6431f8926c557f3c58408dacc98466e76cde1 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 31 Mar 2010 17:56:16 -0700 Subject: started to add services --- test/ScopeSpec.js | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index 7e1a899f..8d2a0ed4 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -11,7 +11,8 @@ describe('scope/model', function(){ model.$set('name', 'adam'); expect(model.name).toEqual('adam'); expect(model.$get('name')).toEqual('adam'); - expect(model.$parent).toEqual(parent); + expect(model.$parent).toEqual(model); + expect(model.$root).toEqual(model); }); //$eval @@ -78,15 +79,50 @@ describe('scope/model', function(){ expect(model.printed).toEqual(true); }); - - //$tryEval it('should report error on element', function(){ - + var scope = createScope(); + scope.$tryEval('throw "myerror";', function(error){ + scope.error = error; + }); + expect(scope.error).toEqual('myerror'); }); it('should report error on visible element', function(){ + var element = jqLite('
'); + var scope = createScope(); + scope.$tryEval('throw "myError"', element); + expect(element.attr('ng-error')).toEqual('"myError"'); // errors are jsonified + expect(element.hasClass('ng-exception')).toBeTruthy(); + }); + + // $onEval + + it("should eval using priority", function(){ + var scope = createScope(); + scope.log = ""; + scope.$onEval('log = log + "middle;"'); + scope.$onEval(-1, 'log = log + "first;"'); + scope.$onEval(1, 'log = log + "last;"'); + scope.$eval(); + expect(scope.log).toEqual('first;middle;last;'); + }); + + // Services are initialized + it("should inject services", function(){ + var scope = createScope(serviceAdapter({ + $window: function(){ + return window; + } + })); + expect(scope.$window).toEqual(window); + }); + it("should have $root and $parent", function(){ + var parent = createScope(); + var scope = createScope(parent); + expect(scope.$root).toEqual(parent); + expect(scope.$parent).toEqual(parent); }); }); -- cgit v1.2.3 From a80a61839a66d244c8bb14bbe2975746e02516c8 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Sat, 3 Apr 2010 17:04:36 -0700 Subject: injection is now working --- test/ScopeSpec.js | 57 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 24 deletions(-) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index 8d2a0ed4..a7322cae 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -65,20 +65,6 @@ describe('scope/model', function(){ expect(model.$bind(function(){return this.name;})()).toEqual('misko'); }); - //$behavior - it('should behave as class', function(){ - function Printer(brand){ - this.brand = brand; - }; - Printer.prototype.print = function(){ - this.printed = true; - }; - var model = createScope({ name: 'parent' }, Printer, 'hp'); - expect(model.brand).toEqual('hp'); - model.print(); - expect(model.printed).toEqual(true); - }); - //$tryEval it('should report error on element', function(){ var scope = createScope(); @@ -108,16 +94,6 @@ describe('scope/model', function(){ expect(scope.log).toEqual('first;middle;last;'); }); - // Services are initialized - it("should inject services", function(){ - var scope = createScope(serviceAdapter({ - $window: function(){ - return window; - } - })); - expect(scope.$window).toEqual(window); - }); - it("should have $root and $parent", function(){ var parent = createScope(); var scope = createScope(parent); @@ -125,4 +101,37 @@ describe('scope/model', function(){ expect(scope.$parent).toEqual(parent); }); + // Service injection + it('should inject services', function(){ + var scope = createScope(null, { + service:function(){ + return "ABC"; + } + }); + expect(scope.service).toEqual("ABC"); + }); + + it('should inject arugments', function(){ + var scope = createScope(null, { + name:function(){ + return "misko"; + }, + greet: extend(function(name) { + return 'hello ' + name; + }, {inject:['name']}) + }); + expect(scope.greet).toEqual("hello misko"); + }); + + it('should throw error on missing dependency', function(){ + try { + createScope(null, { + greet: extend(function(name) { + }, {inject:['name']}) + }); + } catch(e) { + expect(e).toEqual("Don't know how to inject 'name'."); + } + }); + }); -- cgit v1.2.3 From 7a4b48020688060debe9cb0f9c17615d7585cbe7 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Mon, 5 Apr 2010 11:46:53 -0700 Subject: added ng:switch widget --- test/ScopeSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index a7322cae..09f4d875 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -44,7 +44,7 @@ describe('scope/model', function(){ model.$onEval(function(){evalCount ++;}); model.name = 'misko'; model.$eval(); - expect(nameCount).toEqual(1); + expect(nameCount).toEqual(2); expect(evalCount).toEqual(1); expect(model.newValue).toEqual('misko'); expect(model.oldValue).toEqual('adam'); -- cgit v1.2.3 From e0ad7dfcd47196d0aa9271e84b2c4ac26cfda3f4 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 7 Apr 2010 17:24:24 -0700 Subject: seperatio validation and exception handling --- test/ScopeSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index 09f4d875..0665968b 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -78,7 +78,7 @@ describe('scope/model', function(){ var element = jqLite(''); var scope = createScope(); scope.$tryEval('throw "myError"', element); - expect(element.attr('ng-error')).toEqual('"myError"'); // errors are jsonified + expect(element.attr('ng-exception')).toEqual('"myError"'); // errors are jsonified expect(element.hasClass('ng-exception')).toBeTruthy(); }); -- cgit v1.2.3 From 70e401ef100614295fc808e32f0142f07c315461 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 15 Apr 2010 14:17:33 -0700 Subject: added $route service --- test/ScopeSpec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index 0665968b..23638b27 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -82,6 +82,16 @@ describe('scope/model', function(){ expect(element.hasClass('ng-exception')).toBeTruthy(); }); + it('should report error on $excetionHandler', function(){ + var element = jqLite(''); + var scope = createScope(); + scope.$exceptionHandler = function(e){ + this.error = e; + }; + scope.$tryEval('throw "myError"'); + expect(scope.error).toEqual("myError"); + }); + // $onEval it("should eval using priority", function(){ -- cgit v1.2.3 From 22d1464d7abc284dd56d6beaf47e8d85088e01c5 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 13 May 2010 13:57:39 -0700 Subject: fixed issue with radio view clobering model if radio was checked. --- test/ScopeSpec.js | 213 +++++++++++++++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 100 deletions(-) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index 23638b27..a3b6d9ae 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -15,133 +15,146 @@ describe('scope/model', function(){ expect(model.$root).toEqual(model); }); - //$eval - it('should eval function with correct this and pass arguments', function(){ - var model = createScope(); - model.$eval(function(name){ - this.name = name; - }, 'works'); - expect(model.name).toEqual('works'); - }); + describe('$eval', function(){ + it('should eval function with correct this and pass arguments', function(){ + var model = createScope(); + model.$eval(function(name){ + this.name = name; + }, 'works'); + expect(model.name).toEqual('works'); + }); - it('should eval expression with correct this', function(){ - var model = createScope(); - model.$eval('name="works"'); - expect(model.name).toEqual('works'); - }); + it('should eval expression with correct this', function(){ + var model = createScope(); + model.$eval('name="works"'); + expect(model.name).toEqual('works'); + }); - //$watch - it('should watch an expression for change', function(){ - var model = createScope(); - model.oldValue = ""; - var nameCount = 0, evalCount = 0; - model.name = 'adam'; - model.$watch('name', function(){ nameCount ++; }); - model.$watch(function(){return model.name;}, function(newValue, oldValue){ - this.newValue = newValue; - this.oldValue = oldValue; + it('should do nothing on empty string and not update view', function(){ + var model = createScope(); + var onEval = jasmine.createSpy('onEval'); + model.$onEval(onEval); + model.$eval(''); + expect(onEval).wasNotCalled(); }); - model.$onEval(function(){evalCount ++;}); - model.name = 'misko'; - model.$eval(); - expect(nameCount).toEqual(2); - expect(evalCount).toEqual(1); - expect(model.newValue).toEqual('misko'); - expect(model.oldValue).toEqual('adam'); }); - it('should eval with no arguments', function(){ - var model = createScope(); - var count = 0; - model.$onEval(function(){count++;}); - model.$eval(); - expect(count).toEqual(1); - }); + describe('$watch', function(){ + it('should watch an expression for change', function(){ + var model = createScope(); + model.oldValue = ""; + var nameCount = 0, evalCount = 0; + model.name = 'adam'; + model.$watch('name', function(){ nameCount ++; }); + model.$watch(function(){return model.name;}, function(newValue, oldValue){ + this.newValue = newValue; + this.oldValue = oldValue; + }); + model.$onEval(function(){evalCount ++;}); + model.name = 'misko'; + model.$eval(); + expect(nameCount).toEqual(2); + expect(evalCount).toEqual(1); + expect(model.newValue).toEqual('misko'); + expect(model.oldValue).toEqual('adam'); + }); - //$bind - it('should curry a function with respect to scope', function(){ - var model = createScope(); - model.name = 'misko'; - expect(model.$bind(function(){return this.name;})()).toEqual('misko'); + it('should eval with no arguments', function(){ + var model = createScope(); + var count = 0; + model.$onEval(function(){count++;}); + model.$eval(); + expect(count).toEqual(1); + }); }); - //$tryEval - it('should report error on element', function(){ - var scope = createScope(); - scope.$tryEval('throw "myerror";', function(error){ - scope.error = error; + describe('$bind', function(){ + it('should curry a function with respect to scope', function(){ + var model = createScope(); + model.name = 'misko'; + expect(model.$bind(function(){return this.name;})()).toEqual('misko'); }); - expect(scope.error).toEqual('myerror'); }); - it('should report error on visible element', function(){ - var element = jqLite(''); - var scope = createScope(); - scope.$tryEval('throw "myError"', element); - expect(element.attr('ng-exception')).toEqual('"myError"'); // errors are jsonified - expect(element.hasClass('ng-exception')).toBeTruthy(); - }); + describe('$tryEval', function(){ + it('should report error on element', function(){ + var scope = createScope(); + scope.$tryEval('throw "myerror";', function(error){ + scope.error = error; + }); + expect(scope.error).toEqual('myerror'); + }); - it('should report error on $excetionHandler', function(){ - var element = jqLite(''); - var scope = createScope(); - scope.$exceptionHandler = function(e){ - this.error = e; - }; - scope.$tryEval('throw "myError"'); - expect(scope.error).toEqual("myError"); + it('should report error on visible element', function(){ + var element = jqLite(''); + var scope = createScope(); + scope.$tryEval('throw "myError"', element); + expect(element.attr('ng-exception')).toEqual('"myError"'); // errors are jsonified + expect(element.hasClass('ng-exception')).toBeTruthy(); + }); + + it('should report error on $excetionHandler', function(){ + var element = jqLite(''); + var scope = createScope(); + scope.$exceptionHandler = function(e){ + this.error = e; + }; + scope.$tryEval('throw "myError"'); + expect(scope.error).toEqual("myError"); + }); }); // $onEval + describe('$onEval', function(){ + it("should eval using priority", function(){ + var scope = createScope(); + scope.log = ""; + scope.$onEval('log = log + "middle;"'); + scope.$onEval(-1, 'log = log + "first;"'); + scope.$onEval(1, 'log = log + "last;"'); + scope.$eval(); + expect(scope.log).toEqual('first;middle;last;'); + }); - it("should eval using priority", function(){ - var scope = createScope(); - scope.log = ""; - scope.$onEval('log = log + "middle;"'); - scope.$onEval(-1, 'log = log + "first;"'); - scope.$onEval(1, 'log = log + "last;"'); - scope.$eval(); - expect(scope.log).toEqual('first;middle;last;'); - }); - - it("should have $root and $parent", function(){ - var parent = createScope(); - var scope = createScope(parent); - expect(scope.$root).toEqual(parent); - expect(scope.$parent).toEqual(parent); + it("should have $root and $parent", function(){ + var parent = createScope(); + var scope = createScope(parent); + expect(scope.$root).toEqual(parent); + expect(scope.$parent).toEqual(parent); + }); }); - // Service injection - it('should inject services', function(){ - var scope = createScope(null, { - service:function(){ + describe('service injection', function(){ + it('should inject services', function(){ + var scope = createScope(null, { + service:function(){ return "ABC"; } + }); + expect(scope.service).toEqual("ABC"); }); - expect(scope.service).toEqual("ABC"); - }); - it('should inject arugments', function(){ - var scope = createScope(null, { - name:function(){ + it('should inject arugments', function(){ + var scope = createScope(null, { + name:function(){ return "misko"; }, greet: extend(function(name) { return 'hello ' + name; }, {inject:['name']}) - }); - expect(scope.greet).toEqual("hello misko"); - }); - - it('should throw error on missing dependency', function(){ - try { - createScope(null, { - greet: extend(function(name) { - }, {inject:['name']}) }); - } catch(e) { - expect(e).toEqual("Don't know how to inject 'name'."); - } - }); + expect(scope.greet).toEqual("hello misko"); + }); + it('should throw error on missing dependency', function(){ + try { + createScope(null, { + greet: extend(function(name) { + }, {inject:['name']}) + }); + } catch(e) { + expect(e).toEqual("Don't know how to inject 'name'."); + } + }); + }); }); -- cgit v1.2.3 From 2e33e89a77d115ff17f5841ec328b1c1e4228161 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Sun, 30 May 2010 19:42:21 -0700 Subject: added compiled getterFN for better performance --- test/ScopeSpec.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index a3b6d9ae..d93400e5 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -157,4 +157,25 @@ describe('scope/model', function(){ } }); }); + + describe('getterFn', function(){ + it('should get chain', function(){ + expect(getterFn('a.b')(undefined)).toEqual(undefined); + expect(getterFn('a.b')({})).toEqual(undefined); + expect(getterFn('a.b')({a:null})).toEqual(undefined); + expect(getterFn('a.b')({a:{}})).toEqual(undefined); + expect(getterFn('a.b')({a:{b:null}})).toEqual(null); + expect(getterFn('a.b')({a:{b:0}})).toEqual(0); + expect(getterFn('a.b')({a:{b:'abc'}})).toEqual('abc'); + }); + + it('should map type method on top of expression', function(){ + expect(getterFn('a.$filter')({a:[]})('')).toEqual([]); + }); + + it('should bind function this', function(){ + expect(getterFn('a')({a:function($){return this.b + $;}, b:1})(2)).toEqual(3); + + }); + }); }); -- cgit v1.2.3