From 42062dab34192d2cb9ed66a720c0f791408c61c0 Mon Sep 17 00:00:00 2001
From: Misko Hevery
Date: Wed, 10 Aug 2011 13:15:43 -0700
Subject: refactor(scope): remove $flush/$observe ng:eval/ng:eval-order
---
test/AngularSpec.js | 8 +-
test/BinderSpec.js | 8 +-
test/CompilerSpec.js | 23 +++---
test/ScenarioSpec.js | 6 +-
test/ScopeSpec.js | 176 ++++------------------------------------
test/directivesSpec.js | 91 ++++++++-------------
test/markupSpec.js | 30 +++----
test/service/cookieStoreSpec.js | 6 +-
test/service/cookiesSpec.js | 16 ++--
test/service/locationSpec.js | 4 +-
test/service/routeSpec.js | 6 +-
test/service/updateViewSpec.js | 63 --------------
test/service/xhr.cacheSpec.js | 2 +-
test/widgetsSpec.js | 150 +++++++++++++++++-----------------
14 files changed, 184 insertions(+), 405 deletions(-)
delete mode 100644 test/service/updateViewSpec.js
(limited to 'test')
diff --git a/test/AngularSpec.js b/test/AngularSpec.js
index abb34f3e..cbafa54c 100644
--- a/test/AngularSpec.js
+++ b/test/AngularSpec.js
@@ -551,7 +551,7 @@ describe('angular', function(){
it('should link to existing node and create scope', function(){
template = angular.element('
{{greeting = "hello world"}}
');
scope = angular.compile(template)();
- scope.$flush();
+ scope.$digest();
expect(template.text()).toEqual('hello world');
expect(scope.greeting).toEqual('hello world');
});
@@ -560,7 +560,7 @@ describe('angular', function(){
scope = angular.scope();
template = angular.element('{{greeting = "hello world"}}
');
angular.compile(template)(scope);
- scope.$flush();
+ scope.$digest();
expect(template.text()).toEqual('hello world');
expect(scope).toEqual(scope);
});
@@ -575,7 +575,7 @@ describe('angular', function(){
templateFn(scope, function(clone){
templateClone = clone;
});
- scope.$flush();
+ scope.$digest();
expect(template.text()).toEqual('');
expect(scope.$element.text()).toEqual('hello world');
@@ -586,7 +586,7 @@ describe('angular', function(){
it('should link to cloned node and create scope', function(){
scope = angular.scope();
template = jqLite('{{greeting = "hello world"}}
');
- angular.compile(template)(scope, noop).$flush();
+ angular.compile(template)(scope, noop).$digest();
expect(template.text()).toEqual('');
expect(scope.$element.text()).toEqual('hello world');
expect(scope.greeting).toEqual('hello world');
diff --git a/test/BinderSpec.js b/test/BinderSpec.js
index a84fe68a..ecfe0682 100644
--- a/test/BinderSpec.js
+++ b/test/BinderSpec.js
@@ -54,8 +54,8 @@ describe('Binder', function(){
});
it('BindUpdate', function(){
- var scope = this.compile('');
- scope.$flush();
+ var scope = this.compile('');
+ scope.$digest();
assertEquals(123, scope.a);
});
@@ -284,6 +284,7 @@ describe('Binder', function(){
assertEquals(['ErrorMsg1'], errorLogs.shift());
scope.error['throw'] = function(){throw "MyError";};
+ errorLogs.length = 0;
scope.$apply();
span = childNode(doc, 0);
assertTrue(span.hasClass('ng-exception'));
@@ -309,8 +310,9 @@ describe('Binder', function(){
'throw': function(){throw new Error("ErrorMsg" + (++count));}
};
scope.$apply();
- expect(errorLogs.length).toMatch(1);
+ expect(errorLogs.length).not.toEqual(0);
expect(errorLogs.shift()).toMatch(/ErrorMsg1/);
+ errorLogs.length = 0;
scope.error['throw'] = function(){ return 'X';};
scope.$apply();
diff --git a/test/CompilerSpec.js b/test/CompilerSpec.js
index 90afbaff..8f86e99a 100644
--- a/test/CompilerSpec.js
+++ b/test/CompilerSpec.js
@@ -15,7 +15,7 @@ describe('compiler', function(){
observe: function(expression, element){
return function() {
- this.$observe(expression, function(scope, val){
+ this.$watch(expression, function(scope, val){
if (val)
log += ":" + val;
});
@@ -76,13 +76,13 @@ describe('compiler', function(){
it('should observe scope', function(){
scope = compile('');
expect(log).toEqual("");
- scope.$flush();
+ scope.$digest();
scope.name = 'misko';
- scope.$flush();
- scope.$flush();
+ scope.$digest();
+ scope.$digest();
scope.name = 'adam';
- scope.$flush();
- scope.$flush();
+ scope.$digest();
+ scope.$digest();
expect(log).toEqual(":misko:adam");
});
@@ -100,18 +100,21 @@ describe('compiler', function(){
element.removeAttr("duplicate");
var linker = this.compile(element);
return function(marker) {
- this.$observe(function() {
+ this.$watch('value', function() {
var scope = linker(angular.scope(), noop);
marker.after(scope.$element);
});
};
};
scope = compile('beforexafter');
- scope.$flush();
+ scope.value = 1;
+ scope.$digest();
expect(sortedHtml(scope.$element)).toEqual('before<#comment>#comment>xafter
');
- scope.$flush();
+ scope.value = 2;
+ scope.$digest();
expect(sortedHtml(scope.$element)).toEqual('before<#comment>#comment>xxafter
');
- scope.$flush();
+ scope.value = 3;
+ scope.$digest();
expect(sortedHtml(scope.$element)).toEqual('before<#comment>#comment>xxxafter
');
});
diff --git a/test/ScenarioSpec.js b/test/ScenarioSpec.js
index ce8b0dec..e91e0b98 100644
--- a/test/ScenarioSpec.js
+++ b/test/ScenarioSpec.js
@@ -15,20 +15,20 @@ describe("ScenarioSpec: Compilation", function(){
it("should compile dom node and return scope", function(){
var node = jqLite('{{b=a+1}}
')[0];
scope = angular.compile(node)();
- scope.$flush();
+ scope.$digest();
expect(scope.a).toEqual(1);
expect(scope.b).toEqual(2);
});
it("should compile jQuery node and return scope", function(){
scope = compile(jqLite('{{a=123}}
'))();
- scope.$flush();
+ scope.$digest();
expect(jqLite(scope.$element).text()).toEqual('123');
});
it("should compile text node and return scope", function(){
scope = angular.compile('{{a=123}}
')();
- scope.$flush();
+ scope.$digest();
expect(jqLite(scope.$element).text()).toEqual('123');
});
});
diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js
index 6105df21..5a14abd6 100644
--- a/test/ScopeSpec.js
+++ b/test/ScopeSpec.js
@@ -5,9 +5,6 @@ describe('Scope', function(){
beforeEach(function(){
root = createScope(angular.service, {
- $updateView: function(){
- root.$flush();
- },
'$exceptionHandler': $exceptionHandlerMockFactory()
});
mockHandler = root.$service('$exceptionHandler');
@@ -108,6 +105,9 @@ describe('Scope', function(){
it('should watch and fire on simple property change', function(){
var spy = jasmine.createSpy();
root.$watch('name', spy);
+ root.$digest();
+ spy.reset();
+
expect(spy).not.wasCalled();
root.$digest();
expect(spy).not.wasCalled();
@@ -120,6 +120,9 @@ describe('Scope', function(){
it('should watch and fire on expression change', function(){
var spy = jasmine.createSpy();
root.$watch('name.first', spy);
+ root.$digest();
+ spy.reset();
+
root.name = {};
expect(spy).not.wasCalled();
root.$digest();
@@ -170,6 +173,8 @@ describe('Scope', function(){
root.$watch('c', function(self, v){self.d = v; log+='c'; });
root.$watch('b', function(self, v){self.c = v; log+='b'; });
root.$watch('a', function(self, v){self.b = v; log+='a'; });
+ root.$digest();
+ log = '';
root.a = 1;
expect(root.$digest()).toEqual(3);
expect(root.b).toEqual(1);
@@ -204,22 +209,13 @@ describe('Scope', function(){
root.a = 1;
root.$watch('a', function(){ log += 'a'; });
root.$watch('b', function(){ log += 'b'; });
- expect(log).toEqual('');
+ root.$digest();
+ log = '';
expect(root.$digest()).toEqual(0);
expect(log).toEqual('');
});
- it('should return the listener to force a initial watch', function(){
- var log = '';
- root.a = 1;
- root.$watch('a', function(scope, o1, o2){ log += scope.a + ':' + (o1 == o2 == 1) ; })();
- expect(log).toEqual('1:true');
- expect(root.$digest()).toEqual(0);
- expect(log).toEqual('1:true');
- });
-
-
it('should watch objects', function(){
var log = '';
root.a = [];
@@ -227,7 +223,7 @@ describe('Scope', function(){
root.$watch('a', function(){ log +='.';});
root.$watch('b', function(){ log +='!';});
root.$digest();
- expect(log).toEqual('');
+ log = '';
root.a.push({});
root.b.name = '';
@@ -243,9 +239,6 @@ describe('Scope', function(){
expect(function(){
root.$digest();
}).toThrow('$digest already in progress');
- expect(function(){
- root.$flush();
- }).toThrow('$digest already in progress');
callCount++;
});
root.name = 'a';
@@ -255,138 +248,6 @@ describe('Scope', function(){
});
- describe('$observe/$flush', function(){
- it('should register simple property observer and fire on change', function(){
- var spy = jasmine.createSpy();
- root.$observe('name', spy);
- expect(spy).not.wasCalled();
- root.$flush();
- expect(spy).wasCalled();
- expect(spy.mostRecentCall.args[0]).toEqual(root);
- expect(spy.mostRecentCall.args[1]).toEqual(undefined);
- expect(spy.mostRecentCall.args[2].toString()).toEqual(NaN.toString());
- root.name = 'misko';
- root.$flush();
- expect(spy).wasCalledWith(root, 'misko', undefined);
- });
-
-
- it('should register expression observers and fire them on change', function(){
- var spy = jasmine.createSpy();
- root.$observe('name.first', spy);
- root.name = {};
- expect(spy).not.wasCalled();
- root.$flush();
- expect(spy).wasCalled();
- root.name.first = 'misko';
- root.$flush();
- expect(spy).wasCalled();
- });
-
-
- it('should delegate exceptions', function(){
- root.$observe('a', function(){throw new Error('abc');});
- root.a = 1;
- root.$flush();
- expect(mockHandler.errors[0].message).toEqual('abc');
- $logMock.error.logs.shift();
- });
-
-
- it('should fire observers in order of addition', function(){
- // this is not an external guarantee, just our own sanity
- var log = '';
- root.$observe('a', function(){ log += 'a'; });
- root.$observe('b', function(){ log += 'b'; });
- root.$observe('c', function(){ log += 'c'; });
- root.a = root.b = root.c = 1;
- root.$flush();
- expect(log).toEqual('abc');
- });
-
-
- it('should delegate $flush to children in addition order', function(){
- // this is not an external guarantee, just our own sanity
- var log = '';
- var childA = root.$new();
- var childB = root.$new();
- var childC = root.$new();
- childA.$observe('a', function(){ log += 'a'; });
- childB.$observe('b', function(){ log += 'b'; });
- childC.$observe('c', function(){ log += 'c'; });
- childA.a = childB.b = childC.c = 1;
- root.$flush();
- expect(log).toEqual('abc');
- });
-
-
- it('should fire observers once at beggining and on change', function(){
- var log = '';
- root.$observe('c', function(self, v){self.d = v; log += 'c';});
- root.$observe('b', function(self, v){self.c = v; log += 'b';});
- root.$observe('a', function(self, v){self.b = v; log += 'a';});
- root.a = 1;
- root.$flush();
- expect(root.b).toEqual(1);
- expect(log).toEqual('cba');
- root.$flush();
- expect(root.c).toEqual(1);
- expect(log).toEqual('cbab');
- root.$flush();
- expect(root.d).toEqual(1);
- expect(log).toEqual('cbabc');
- });
-
-
- it('should fire on initial observe', function(){
- var log = '';
- root.a = 1;
- root.$observe('a', function(){ log += 'a'; });
- root.$observe('b', function(){ log += 'b'; });
- expect(log).toEqual('');
- root.$flush();
- expect(log).toEqual('ab');
- });
-
-
- it('should observe objects', function(){
- var log = '';
- root.a = [];
- root.b = {};
- root.$observe('a', function(){ log +='.';});
- root.$observe('a', function(){ log +='!';});
- root.$flush();
- expect(log).toEqual('.!');
-
- root.$flush();
- expect(log).toEqual('.!');
-
- root.a.push({});
- root.b.name = '';
-
- root.$digest();
- expect(log).toEqual('.!');
- });
-
-
- it('should prevent recursion', function(){
- var callCount = 0;
- root.$observe('name', function(){
- expect(function(){
- root.$digest();
- }).toThrow('$flush already in progress');
- expect(function(){
- root.$flush();
- }).toThrow('$flush already in progress');
- callCount++;
- });
- root.name = 'a';
- root.$flush();
- expect(callCount).toEqual(1);
- });
- });
-
-
describe('$destroy', function(){
var first, middle, last, log;
@@ -401,6 +262,7 @@ describe('Scope', function(){
middle.$watch(function(){ log += '2';});
last.$watch(function(){ log += '3';});
+ root.$digest();
log = '';
});
@@ -450,9 +312,8 @@ describe('Scope', function(){
var log = '';
var child = root.$new();
root.$watch('a', function(scope, a){ log += '1'; });
- root.$observe('a', function(scope, a){ log += '2'; });
child.$apply('$parent.a=0');
- expect(log).toEqual('12');
+ expect(log).toEqual('1');
});
@@ -460,25 +321,24 @@ describe('Scope', function(){
var log = '';
var child = root.$new();
root.$watch('a', function(scope, a){ log += '1'; });
- root.$observe('a', function(scope, a){ log += '2'; });
root.a = 0;
child.$apply(function(){ throw new Error('MyError'); });
- expect(log).toEqual('12');
+ expect(log).toEqual('1');
expect(mockHandler.errors[0].message).toEqual('MyError');
$logMock.error.logs.shift();
});
describe('exceptions', function(){
- var $exceptionHandler, $updateView, log;
+ var $exceptionHandler, log;
beforeEach(function(){
log = '';
$exceptionHandler = jasmine.createSpy('$exceptionHandler');
- $updateView = jasmine.createSpy('$updateView');
root.$service = function(name) {
- return {$updateView:$updateView, $exceptionHandler:$exceptionHandler}[name];
+ return {$exceptionHandler:$exceptionHandler}[name];
};
root.$watch(function(){ log += '$digest;'; });
+ root.$digest();
log = '';
});
@@ -490,7 +350,6 @@ describe('Scope', function(){
})).toEqual('abc');
expect(log).toEqual('$digest;');
expect($exceptionHandler).not.wasCalled();
- expect($updateView).wasCalled();
});
@@ -499,7 +358,6 @@ describe('Scope', function(){
root.$apply(function(){ throw error; });
expect(log).toEqual('$digest;');
expect($exceptionHandler).wasCalledWith(error);
- expect($updateView).wasCalled();
});
});
});
diff --git a/test/directivesSpec.js b/test/directivesSpec.js
index a05861ae..1a02e318 100644
--- a/test/directivesSpec.js
+++ b/test/directivesSpec.js
@@ -20,20 +20,12 @@ describe("directive", function(){
expect(scope.a).toEqual(123);
});
- it("should ng:eval", function() {
- var scope = compile('');
- scope.$flush();
- expect(scope.a).toEqual(1);
- scope.$flush();
- expect(scope.a).toEqual(2);
- });
-
describe('ng:bind', function(){
it('should set text', function() {
var scope = compile('');
expect(element.text()).toEqual('');
scope.a = 'misko';
- scope.$flush();
+ scope.$digest();
expect(element.hasClass('ng-binding')).toEqual(true);
expect(element.text()).toEqual('misko');
});
@@ -41,24 +33,24 @@ describe("directive", function(){
it('should set text to blank if undefined', function() {
var scope = compile('');
scope.a = 'misko';
- scope.$flush();
+ scope.$digest();
expect(element.text()).toEqual('misko');
scope.a = undefined;
- scope.$flush();
+ scope.$digest();
expect(element.text()).toEqual('');
});
it('should set html', function() {
var scope = compile('');
scope.html = 'hello
';
- scope.$flush();
+ scope.$digest();
expect(lowercase(element.html())).toEqual('hello
');
});
it('should set unsafe html', function() {
var scope = compile('');
scope.html = 'hello
';
- scope.$flush();
+ scope.$digest();
expect(lowercase(element.html())).toEqual('hello
');
});
@@ -67,7 +59,7 @@ describe("directive", function(){
return jqLite('hello');
};
var scope = compile('');
- scope.$flush();
+ scope.$digest();
expect(lowercase(element.html())).toEqual('hello');
});
@@ -77,14 +69,14 @@ describe("directive", function(){
return 'HELLO';
};
var scope = compile('');
- scope.$flush();
+ scope.$digest();
expect(sortedHtml(scope.$element)).toEqual('');
});
it('should suppress rendering of falsy values', function(){
var scope = compile('{{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}
');
- scope.$flush();
+ scope.$digest();
expect(scope.$element.text()).toEqual('-0false');
});
@@ -94,19 +86,19 @@ describe("directive", function(){
it('should ng:bind-template', function() {
var scope = compile('');
scope.name = 'Misko';
- scope.$flush();
+ scope.$digest();
expect(element.hasClass('ng-binding')).toEqual(true);
expect(element.text()).toEqual('Hello Misko!');
});
it('should have $element set to current bind element', function(){
- var innerText = 'blank';
+ var innerText;
angularFilter.myFilter = function(text){
- innerText = this.$element.text();
+ innerText = innerText || this.$element.text();
return text;
};
var scope = compile('beforeINNERafter
');
- scope.$flush();
+ scope.$digest();
expect(scope.$element.text()).toEqual("beforeHELLOafter");
expect(innerText).toEqual('INNER');
});
@@ -116,14 +108,14 @@ describe("directive", function(){
describe('ng:bind-attr', function(){
it('should bind attributes', function(){
var scope = compile('');
- scope.$flush();
+ scope.$digest();
expect(element.attr('src')).toEqual('http://localhost/mysrc');
expect(element.attr('alt')).toEqual('myalt');
});
it('should not pretty print JSON in attributes', function(){
var scope = compile('
');
- scope.$flush();
+ scope.$digest();
expect(element.attr('alt')).toEqual('{"a":1}');
});
});
@@ -138,7 +130,7 @@ describe("directive", function(){
scope.disabled = true;
scope.readonly = true;
scope.checked = true;
- scope.$flush();
+ scope.$digest();
expect(input.disabled).toEqual(true);
expect(input.readOnly).toEqual(true);
@@ -148,7 +140,7 @@ describe("directive", function(){
describe('ng:click', function(){
it('should get called on a click', function(){
var scope = compile('');
- scope.$flush();
+ scope.$digest();
expect(scope.clicked).toBeFalsy();
browserTrigger(element, 'click');
@@ -157,7 +149,7 @@ describe("directive", function(){
it('should stop event propagation', function() {
var scope = compile('');
- scope.$flush();
+ scope.$digest();
expect(scope.outer).not.toBeDefined();
expect(scope.inner).not.toBeDefined();
@@ -175,7 +167,7 @@ describe("directive", function(){
var scope = compile('');
- scope.$flush();
+ scope.$digest();
expect(scope.submitted).not.toBeDefined();
browserTrigger(element.children()[0]);
@@ -187,18 +179,18 @@ describe("directive", function(){
it('should add new and remove old classes dynamically', function() {
var scope = compile('');
scope.dynClass = 'A';
- scope.$flush();
+ scope.$digest();
expect(element.hasClass('existing')).toBe(true);
expect(element.hasClass('A')).toBe(true);
scope.dynClass = 'B';
- scope.$flush();
+ scope.$digest();
expect(element.hasClass('existing')).toBe(true);
expect(element.hasClass('A')).toBe(false);
expect(element.hasClass('B')).toBe(true);
delete scope.dynClass;
- scope.$flush();
+ scope.$digest();
expect(element.hasClass('existing')).toBe(true);
expect(element.hasClass('A')).toBe(false);
expect(element.hasClass('B')).toBe(false);
@@ -206,7 +198,7 @@ describe("directive", function(){
it('should support adding multiple classes', function(){
var scope = compile('');
- scope.$flush();
+ scope.$digest();
expect(element.hasClass('existing')).toBeTruthy();
expect(element.hasClass('A')).toBeTruthy();
expect(element.hasClass('B')).toBeTruthy();
@@ -216,7 +208,7 @@ describe("directive", function(){
it('should ng:class odd/even', function(){
var scope = compile('