aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorIgor Minar2011-01-04 17:54:37 -0800
committerIgor Minar2011-01-07 14:39:41 -0800
commit16086aa37c5c0c98f5c4a42d2a15136bb6d18605 (patch)
tree8b8e4b6b585e9d267588cb324745a3246bc5bc41 /test
parentc0a26b18531482d493d544cf1a207586e8aacaf4 (diff)
downloadangular.js-16086aa37c5c0c98f5c4a42d2a15136bb6d18605.tar.bz2
$location service should utilize onhashchange events instead of polling
Diffstat (limited to 'test')
-rw-r--r--test/BrowserSpecs.js106
-rw-r--r--test/angular-mocks.js15
-rw-r--r--test/servicesSpec.js12
3 files changed, 111 insertions, 22 deletions
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js
index eb43e3c5..89fc14ed 100644
--- a/test/BrowserSpecs.js
+++ b/test/BrowserSpecs.js
@@ -1,6 +1,6 @@
describe('browser', function(){
- var browser, location, head, xhr, setTimeoutQueue;
+ var browser, fakeWindow, xhr, logs, scripts, setTimeoutQueue;
function fakeSetTimeout(fn) {
setTimeoutQueue.push(fn);
@@ -15,19 +15,31 @@ describe('browser', function(){
beforeEach(function(){
setTimeoutQueue = [];
-
- location = {href:"http://server", hash:""};
- head = {
- scripts: [],
- append: function(node){head.scripts.push(node);}
- };
+ scripts = [];
xhr = null;
- browser = new Browser(location, jqLite(window.document), head, function(){
+ fakeWindow = {
+ location: {href:"http://server"},
+ setTimeout: fakeSetTimeout
+ }
+
+ var fakeBody = {append: function(node){scripts.push(node)}};
+
+ var fakeXhr = function(){
xhr = this;
this.open = noop;
this.setRequestHeader = noop;
this.send = noop;
- }, undefined, fakeSetTimeout);
+ }
+
+ logs = {log:[], warn:[], info:[], error:[]};
+
+ var fakeLog = {log: function() { logs.log.push(slice.call(arguments)); },
+ warn: function() { logs.warn.push(slice.call(arguments)); },
+ info: function() { logs.info.push(slice.call(arguments)); },
+ error: function() { logs.error.push(slice.call(arguments)); }};
+
+ browser = new Browser(fakeWindow, jqLite(window.document), fakeBody, fakeXhr,
+ fakeLog);
});
it('should contain cookie cruncher', function() {
@@ -60,13 +72,13 @@ describe('browser', function(){
browser.xhr('JSON', 'http://example.org/path?cb=JSON_CALLBACK', function(code, data){
log += code + ':' + data + ';';
});
- expect(head.scripts.length).toEqual(1);
- var url = head.scripts[0].src.split('?cb=');
+ expect(scripts.length).toEqual(1);
+ var url = scripts[0].src.split('?cb=');
expect(url[0]).toEqual('http://example.org/path');
- expect(typeof window[url[1]]).toEqual($function);
- window[url[1]]('data');
+ expect(typeof fakeWindow[url[1]]).toEqual($function);
+ fakeWindow[url[1]]('data');
expect(log).toEqual('200:data;');
- expect(typeof window[url[1]]).toEqual('undefined');
+ expect(typeof fakeWindow[url[1]]).toEqual('undefined');
});
});
});
@@ -107,16 +119,8 @@ describe('browser', function(){
}
}
- var browser, log, logs;
-
beforeEach(function() {
deleteAllCookies();
- logs = {log:[], warn:[], info:[], error:[]};
- log = {log: function() { logs.log.push(slice.call(arguments)); },
- warn: function() { logs.warn.push(slice.call(arguments)); },
- info: function() { logs.info.push(slice.call(arguments)); },
- error: function() { logs.error.push(slice.call(arguments)); }};
- browser = new Browser({}, jqLite(document), undefined, XHR, log);
expect(document.cookie).toEqual('');
});
@@ -334,4 +338,62 @@ describe('browser', function(){
expect(returnedFn).toBe(fn);
});
});
+
+
+ describe('url api', function() {
+ it('should use $browser poller to detect url changes when onhashchange event is unsupported',
+ function() {
+
+ fakeWindow = {location: {href:"http://server"}};
+
+ browser = new Browser(fakeWindow, {}, {});
+
+ var events = [];
+
+ browser.onHashChange(function() {
+ events.push('x');
+ });
+
+ fakeWindow.location.href = "http://server/#newHash";
+ expect(events).toEqual([]);
+ browser.poll();
+ expect(events).toEqual(['x']);
+ });
+
+
+ it('should use onhashchange events to detect url changes when supported by browser',
+ function() {
+
+ var onHashChngListener;
+
+ fakeWindow = {location: {href:"http://server"},
+ addEventListener: function(type, listener) {
+ expect(type).toEqual('hashchange');
+ onHashChngListener = listener;
+ },
+ removeEventListener: angular.noop
+ };
+ fakeWindow.onhashchange = true;
+
+ browser = new Browser(fakeWindow, {}, {});
+
+ var events = [],
+ event = {type: "hashchange"}
+
+ browser.onHashChange(function(e) {
+ events.push(e);
+ });
+
+ expect(events).toEqual([]);
+ onHashChngListener(event);
+
+ expect(events.length).toBe(1);
+ expect(events[0].originalEvent || events[0]).toBe(event); // please jQuery and jqLite
+
+ // clean up the jqLite cache so that the global afterEach doesn't complain
+ if (!jQuery) {
+ jqLite(fakeWindow).dealoc();
+ }
+ });
+ });
});
diff --git a/test/angular-mocks.js b/test/angular-mocks.js
index fd53a189..5a4e1de5 100644
--- a/test/angular-mocks.js
+++ b/test/angular-mocks.js
@@ -63,8 +63,23 @@ function MockBrowser() {
this.isMock = true;
self.url = "http://server";
+ self.lastUrl = self.url; // used by url polling fn
self.pollFns = [];
+
+ // register url polling fn
+
+ self.onHashChange = function(listener) {
+ self.pollFns.push(
+ function() {
+ if (self.lastUrl != self.url) {
+ listener();
+ }
+ }
+ );
+ };
+
+
self.xhr = function(method, url, data, callback) {
if (angular.isFunction(data)) {
callback = data;
diff --git a/test/servicesSpec.js b/test/servicesSpec.js
index b2cac224..6df83beb 100644
--- a/test/servicesSpec.js
+++ b/test/servicesSpec.js
@@ -128,6 +128,18 @@ describe("service", function(){
expect($location.hashSearch).toEqual({key: 'value', flag: true, key2: ''});
});
+
+ it('should update location when browser url changed', function() {
+ var origUrl = $location.href;
+ expect(origUrl).toEqual($browser.getUrl());
+
+ var newUrl = 'http://somenew/url#foo';
+ $browser.setUrl(newUrl);
+ $browser.poll();
+ expect($location.href).toEqual(newUrl);
+ });
+
+
it('toString() should return actual representation', function() {
var href = 'http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=';
$location.update(href);