aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMisko Hevery2010-09-22 13:24:40 +0200
committerMisko Hevery2010-09-22 16:17:44 +0200
commit0649009624e8e7bd6fb39537f62c6f00facbfb16 (patch)
treee85077e148220ce75926bffce2d1e7daf8069945 /test
parenteefb920d0e0345485a8eb120aeecc3b1aa9f6719 (diff)
downloadangular.js-0649009624e8e7bd6fb39537f62c6f00facbfb16.tar.bz2
Refactored the Browser:
- change from using prototype to inner functions to help with better compression - removed watchers (url/cookie) and introduced a poller concept - moved the checking of URL and cookie into services which register with poolers Benefits: - Smaller minified file - can call $browser.poll() from tests to simulate polling - single place where setTimeout needs to be tested - More testable $browser
Diffstat (limited to 'test')
-rw-r--r--test/BrowserSpecs.js110
-rw-r--r--test/ScenarioSpec.js4
-rw-r--r--test/angular-mocks.js24
-rw-r--r--test/servicesSpec.js6
4 files changed, 47 insertions, 97 deletions
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js
index 4138a9d9..abd761eb 100644
--- a/test/BrowserSpecs.js
+++ b/test/BrowserSpecs.js
@@ -1,6 +1,6 @@
describe('browser', function(){
- var browser, location, head;
+ var browser, location, head, xhr;
beforeEach(function(){
location = {href:"http://server", hash:""};
@@ -8,44 +8,19 @@ describe('browser', function(){
scripts: [],
append: function(node){head.scripts.push(node);}
};
- browser = new Browser(location, jqLite(window.document), head);
- browser.setTimeout = noop;
- });
-
- it('should watch url', function(){
- browser.delay = 1;
- expectAsserts(2);
- browser.watchUrl(function(url){
- assertEquals('http://getangular.test', url);
+ xhr = null;
+ browser = new Browser(location, jqLite(window.document), head, function(){
+ xhr = this;
+ this.open = noop;
+ this.setRequestHeader = noop;
+ this.send = noop;
});
- browser.setTimeout = function(fn, delay){
- assertEquals(1, delay);
- location.href = "http://getangular.test";
- browser.setTimeout = function(fn, delay) {};
- fn();
- };
- browser.startUrlWatcher();
});
it('should contain cookie cruncher', function() {
expect(browser.cookies).toBeDefined();
});
- it('should be able to start cookie watcher', function() {
- browser.delay = 1;
- expectAsserts(2);
- browser.watchCookies(function(cookies){
- assertEquals({'foo':'bar'}, cookies);
- });
- browser.setTimeout = function(fn, delay){
- assertEquals(1, delay);
- document.cookie = 'foo=bar';
- browser.setTimeout = function(fn, delay) {};
- fn();
- };
- browser.startCookieWatcher();
- });
-
describe('outstading requests', function(){
it('should process callbacks immedietly with no outstanding requests', function(){
var callback = jasmine.createSpy('callback');
@@ -55,15 +30,12 @@ describe('browser', function(){
it('should queue callbacks with outstanding requests', function(){
var callback = jasmine.createSpy('callback');
- browser.outstandingRequests.count = 1;
+ browser.xhr('GET', '/url', noop);
browser.notifyWhenNoOutstandingRequests(callback);
expect(callback).not.wasCalled();
- browser.processRequestCallbacks();
- expect(callback).not.wasCalled();
-
- browser.outstandingRequests.count = 0;
- browser.processRequestCallbacks();
+ xhr.readyState = 4;
+ xhr.onreadystatechange();
expect(callback).wasCalled();
});
});
@@ -220,44 +192,6 @@ describe('browser', function(){
});
- describe('watch', function() {
-
- it('should allow listeners to be registered', function() {
- expectAsserts(1);
-
- browser.watchCookies(function(cookies) {
- assertEquals({'aaa':'bbb'}, cookies);
- });
-
- browser.cookies('aaa','bbb');
- browser.cookies();
- });
-
-
- it('should fire listeners when cookie changes are discovered', function() {
- expectAsserts(1);
-
- browser.watchCookies(function(cookies) {
- assertEquals({'foo':'bar'}, cookies);
- });
-
- document.cookie = 'foo=bar';
- browser.cookies();
- });
-
-
- it('should not fire listeners when no cookies were changed', function() {
- expectAsserts(0);
-
- browser.cookies(function(cookies) {
- assertEquals({'shouldnt':'fire'}, cookies);
- });
-
- browser.cookies(true);
- });
- });
-
-
it('should pick up external changes made to browser cookies', function() {
browser.cookies('oatmealCookie', 'drool');
expect(browser.cookies()).toEqual({'oatmealCookie':'drool'});
@@ -274,5 +208,29 @@ describe('browser', function(){
});
});
+
+ describe('poll', function(){
+ it('should call all fns on poll', function(){
+ var log = '';
+ browser.addPollFn(function(){log+='a';});
+ browser.addPollFn(function(){log+='b';});
+ expect(log).toEqual('');
+ browser.poll();
+ expect(log).toEqual('ab');
+ browser.poll();
+ expect(log).toEqual('abab');
+ });
+
+ it('should startPoller', function(){
+ var log = '';
+ var setTimeoutSpy = jasmine.createSpy('setTimeout');
+ browser.addPollFn(function(){log+='.';});
+ browser.startPoller(50, setTimeoutSpy);
+ expect(log).toEqual('.');
+ expect(setTimeoutSpy.mostRecentCall.args[1]).toEqual(50);
+ setTimeoutSpy.mostRecentCall.args[0]();
+ expect(log).toEqual('..');
+ });
+ });
});
diff --git a/test/ScenarioSpec.js b/test/ScenarioSpec.js
index 7ea3192d..ede49a49 100644
--- a/test/ScenarioSpec.js
+++ b/test/ScenarioSpec.js
@@ -42,10 +42,10 @@ describe("ScenarioSpec: configuration", function(){
it("should take location object", function(){
var url = "http://server/#?book=moby";
var scope = compile("<div>{{$location}}</div>");
- var $location = scope.$get('$location');
+ var $location = scope.$location;
expect($location.hashSearch.book).toBeUndefined();
scope.$browser.setUrl(url);
- scope.$browser.fireUrlWatchers();
+ scope.$browser.poll();
expect($location.hashSearch.book).toEqual('moby');
});
});
diff --git a/test/angular-mocks.js b/test/angular-mocks.js
index 1e547f77..a0d25042 100644
--- a/test/angular-mocks.js
+++ b/test/angular-mocks.js
@@ -29,7 +29,7 @@ function MockBrowser() {
this.isMock = true;
self.url = "http://server";
- self.watches = [];
+ self.pollFns = [];
self.xhr = function(method, url, data, callback) {
if (angular.isFunction(data)) {
@@ -78,6 +78,14 @@ function MockBrowser() {
}
MockBrowser.prototype = {
+ poll: function poll(){
+ foreach(this.pollFns, function(pollFn){ pollFn(); });
+ },
+
+ addPollFn: function(pollFn) {
+ this.pollFns.push(pollFn);
+ },
+
hover: function(onHover) {
},
@@ -89,20 +97,6 @@ MockBrowser.prototype = {
this.url = url;
},
- watchUrl: function(fn) {
- this.watches.push(fn);
- },
-
- watchCookies: function(fn) {
- this.watches.push(fn);
- },
-
- fireUrlWatchers: function() {
- for(var i=0; i<this.watches.length; i++) {
- this.watches[i](this.url);
- }
- },
-
cookies: function(name, value) {
if (name) {
if (value == undefined) {
diff --git a/test/servicesSpec.js b/test/servicesSpec.js
index b39e401c..6fa2c5f5 100644
--- a/test/servicesSpec.js
+++ b/test/servicesSpec.js
@@ -377,8 +377,7 @@ describe("service", function(){
expect(scope.$cookies).toEqual({});
scope.$browser.cookies('brandNew', 'cookie');
- //TODO: This is a hacky way of calling the watch function, once pooling is refactored, this will go away.
- scope.$browser.watches[1](scope.$browser.cookies());
+ scope.$browser.poll();
expect(scope.$cookies).toEqual({'brandNew':'cookie'});
});
@@ -448,8 +447,7 @@ describe("service", function(){
it('should deserialize json to object', function() {
scope.$browser.cookies('objectCookie', '{"id":123,"name":"blah"}');
- //TODO: This is a hacky way of calling the watch function, once pooling is refactored, this will go away.
- scope.$browser.watches[1](scope.$browser.cookies());
+ scope.$browser.poll();
expect(scope.$sessionStore.get('objectCookie')).toEqual({id: 123, name: 'blah'});
});