aboutsummaryrefslogtreecommitdiffstats
path: root/test/ng/httpBackendSpec.js
diff options
context:
space:
mode:
authorIgor Minar2012-04-09 14:27:14 -0700
committerIgor Minar2012-04-09 17:59:47 -0700
commitfbaa1968b7c596ccb63ea8b4be1d3bd92eda50d8 (patch)
treef67c6b8cf724431efead5197cced66dc2340f9f5 /test/ng/httpBackendSpec.js
parent13d5528a5f5a2f0feee5c742788a914d2371841e (diff)
downloadangular.js-fbaa1968b7c596ccb63ea8b4be1d3bd92eda50d8.tar.bz2
chore($browser): remove the addJs method
this was never meant to be a public api used by apps. I refactored the code to hide the functionality. BREAKING CHANGE: $browser.addJs method was removed apps that depended on this functionality should either use many of the existing script loaders or create a simple helper method specific to the app.
Diffstat (limited to 'test/ng/httpBackendSpec.js')
-rw-r--r--test/ng/httpBackendSpec.js73
1 files changed, 53 insertions, 20 deletions
diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js
index 91c0574d..06b63c3c 100644
--- a/test/ng/httpBackendSpec.js
+++ b/test/ng/httpBackendSpec.js
@@ -1,7 +1,7 @@
describe('$httpBackend', function() {
var $backend, $browser, callbacks,
- xhr, fakeBody, callback;
+ xhr, fakeDocument, callback;
// TODO(vojta): should be replaced by $defer mock
function fakeTimeout(fn, delay) {
@@ -21,8 +21,24 @@ describe('$httpBackend', function() {
beforeEach(inject(function($injector) {
callbacks = {counter: 0};
$browser = $injector.get('$browser');
- fakeBody = {removeChild: jasmine.createSpy('body.removeChild')};
- $backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeBody);
+ fakeDocument = {
+ $$scripts: [],
+ createElement: jasmine.createSpy('createElement').andCallFake(function() {
+ return {};
+ }),
+ body: {
+ appendChild: jasmine.createSpy('body.appendChid').andCallFake(function(script) {
+ fakeDocument.$$scripts.push(script);
+ }),
+ removeChild: jasmine.createSpy('body.removeChild').andCallFake(function(script) {
+ var index = indexOf(fakeDocument.$$scripts, script);
+ if (index != -1) {
+ fakeDocument.$$scripts.splice(index, 1);
+ }
+ })
+ }
+ };
+ $backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeDocument);
callback = jasmine.createSpy('done');
}));
@@ -131,14 +147,20 @@ describe('$httpBackend', function() {
});
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback);
- expect($browser.$$scripts.length).toBe(1);
+ expect(fakeDocument.$$scripts.length).toBe(1);
- var script = $browser.$$scripts.shift(),
- url = script.url.match(SCRIPT_URL);
+ var script = fakeDocument.$$scripts.shift(),
+ url = script.src.match(SCRIPT_URL);
expect(url[1]).toBe('http://example.org/path');
callbacks[url[2]]('some-data');
- script.done();
+
+ if (script.onreadystatechange) {
+ script.readyState = 'complete';
+ script.onreadystatechange();
+ } else {
+ script.onload()
+ }
expect(callback).toHaveBeenCalledOnce();
});
@@ -146,17 +168,23 @@ describe('$httpBackend', function() {
it('should clean up the callback and remove the script', function() {
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback);
- expect($browser.$$scripts.length).toBe(1);
+ expect(fakeDocument.$$scripts.length).toBe(1);
+
- var script = $browser.$$scripts.shift(),
- callbackId = script.url.match(SCRIPT_URL)[2];
+ var script = fakeDocument.$$scripts.shift(),
+ callbackId = script.src.match(SCRIPT_URL)[2];
callbacks[callbackId]('some-data');
- script.done();
+
+ if (script.onreadystatechange) {
+ script.readyState = 'complete';
+ script.onreadystatechange();
+ } else {
+ script.onload()
+ }
expect(callbacks[callbackId]).toBeUndefined();
- expect(fakeBody.removeChild).toHaveBeenCalledOnce();
- expect(fakeBody.removeChild).toHaveBeenCalledWith(script);
+ expect(fakeDocument.body.removeChild).toHaveBeenCalledOnceWith(script);
});
@@ -167,21 +195,26 @@ describe('$httpBackend', function() {
});
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback);
- expect($browser.$$scripts.length).toBe(1);
-
- $browser.$$scripts.shift().done();
+ expect(fakeDocument.$$scripts.length).toBe(1);
+
+ var script = fakeDocument.$$scripts.shift();
+ if (script.onreadystatechange) {
+ script.readyState = 'complete';
+ script.onreadystatechange();
+ } else {
+ script.onload()
+ }
expect(callback).toHaveBeenCalledOnce();
});
it('should set url to current location if not specified or empty string', function() {
$backend('JSONP', undefined, null, callback);
- expect($browser.$$scripts[0].url).toBe($browser.url());
- $browser.$$scripts.shift();
+ expect(fakeDocument.$$scripts[0].src).toBe($browser.url());
+ fakeDocument.$$scripts.shift();
$backend('JSONP', '', null, callback);
- expect($browser.$$scripts[0].url).toBe($browser.url());
- $browser.$$scripts.shift();
+ expect(fakeDocument.$$scripts[0].src).toBe($browser.url());
});