diff options
| author | Vojta Jina | 2011-08-05 01:24:41 +0200 |
|---|---|---|
| committer | Igor Minar | 2011-11-30 11:12:14 -0500 |
| commit | 59adadca086853c5de6867ae853f6f27a3af4bbe (patch) | |
| tree | f56e4501975a7e53475f6e0d7bb606e530983a7b /test/widgetsSpec.js | |
| parent | 497839f583ca3dd75583fb996bb764cbd6d7c4ac (diff) | |
| download | angular.js-59adadca086853c5de6867ae853f6f27a3af4bbe.tar.bz2 | |
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
Diffstat (limited to 'test/widgetsSpec.js')
| -rw-r--r-- | test/widgetsSpec.js | 103 |
1 files changed, 89 insertions, 14 deletions
diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index 82aa4956..2ddb26e1 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -1,10 +1,6 @@ 'use strict'; describe("widget", function() { - beforeEach(inject(function($provide){ - $provide.factory('$xhrCache', ['$xhr.cache', identity]); - })); - describe('ng:switch', inject(function($rootScope, $compile) { it('should switch on value change', inject(function($rootScope, $compile) { var element = $compile( @@ -60,26 +56,26 @@ describe("widget", function() { describe('ng:include', inject(function($rootScope, $compile) { - it('should include on external file', inject(function($rootScope, $compile, $xhrCache) { + it('should include on external file', inject(function($rootScope, $compile, $cacheFactory) { var element = jqLite('<ng:include src="url" scope="childScope"></ng:include>'); var element = $compile(element)($rootScope); $rootScope.childScope = $rootScope.$new(); $rootScope.childScope.name = 'misko'; $rootScope.url = 'myUrl'; - $xhrCache.data.myUrl = {value:'{{name}}'}; + $cacheFactory.get('templates').put('myUrl', '{{name}}'); $rootScope.$digest(); expect(element.text()).toEqual('misko'); })); it('should remove previously included text if a falsy value is bound to src', - inject(function($rootScope, $compile, $xhrCache) { + inject(function($rootScope, $compile, $cacheFactory) { var element = jqLite('<ng:include src="url" scope="childScope"></ng:include>'); var element = $compile(element)($rootScope); $rootScope.childScope = $rootScope.$new(); $rootScope.childScope.name = 'igor'; $rootScope.url = 'myUrl'; - $xhrCache.data.myUrl = {value:'{{name}}'}; + $cacheFactory.get('templates').put('myUrl', '{{name}}'); $rootScope.$digest(); expect(element.text()).toEqual('igor'); @@ -91,11 +87,11 @@ describe("widget", function() { })); - it('should allow this for scope', inject(function($rootScope, $compile, $xhrCache) { + it('should allow this for scope', inject(function($rootScope, $compile, $cacheFactory) { var element = jqLite('<ng:include src="url" scope="this"></ng:include>'); var element = $compile(element)($rootScope); $rootScope.url = 'myUrl'; - $xhrCache.data.myUrl = {value:'{{"abc"}}'}; + $cacheFactory.get('templates').put('myUrl', '{{"abc"}}'); $rootScope.$digest(); // TODO(misko): because we are using scope==this, the eval gets registered // during the flush phase and hence does not get called. @@ -108,28 +104,28 @@ describe("widget", function() { it('should evaluate onload expression when a partial is loaded', - inject(function($rootScope, $compile, $xhrCache) { + inject(function($rootScope, $compile, $cacheFactory) { var element = jqLite('<ng:include src="url" onload="loaded = true"></ng:include>'); var element = $compile(element)($rootScope); expect($rootScope.loaded).not.toBeDefined(); $rootScope.url = 'myUrl'; - $xhrCache.data.myUrl = {value:'my partial'}; + $cacheFactory.get('templates').put('myUrl', 'my partial'); $rootScope.$digest(); expect(element.text()).toEqual('my partial'); expect($rootScope.loaded).toBe(true); })); - it('should destroy old scope', inject(function($rootScope, $compile, $xhrCache) { + it('should destroy old scope', inject(function($rootScope, $compile, $cacheFactory) { var element = jqLite('<ng:include src="url"></ng:include>'); var element = $compile(element)($rootScope); expect($rootScope.$$childHead).toBeFalsy(); $rootScope.url = 'myUrl'; - $xhrCache.data.myUrl = {value:'my partial'}; + $cacheFactory.get('templates').put('myUrl', 'my partial'); $rootScope.$digest(); expect($rootScope.$$childHead).toBeTruthy(); @@ -137,6 +133,55 @@ describe("widget", function() { $rootScope.$digest(); expect($rootScope.$$childHead).toBeFalsy(); })); + + it('should do xhr request and cache it', inject(function($rootScope, $browser, $compile) { + var element = $compile('<ng:include src="url"></ng:include>')($rootScope); + var $browserXhr = $browser.xhr; + $browserXhr.expectGET('myUrl').respond('my partial'); + + $rootScope.url = 'myUrl'; + $rootScope.$digest(); + $browserXhr.flush(); + expect(element.text()).toEqual('my partial'); + + $rootScope.url = null; + $rootScope.$digest(); + expect(element.text()).toEqual(''); + + $rootScope.url = 'myUrl'; + $rootScope.$digest(); + expect(element.text()).toEqual('my partial'); + dealoc($rootScope); + })); + + it('should clear content when error during xhr request', + inject(function($browser, $compile, $rootScope) { + var element = $compile('<ng:include src="url">content</ng:include>')($rootScope); + var $browserXhr = $browser.xhr; + $browserXhr.expectGET('myUrl').respond(404, ''); + + $rootScope.url = 'myUrl'; + $rootScope.$digest(); + $browserXhr.flush(); + + expect(element.text()).toBe(''); + })); + + it('should be async even if served from cache', inject(function($rootScope, $compile, $cacheFactory) { + var element = $compile('<ng:include src="url"></ng:include>')($rootScope); + + $rootScope.url = 'myUrl'; + $cacheFactory.get('templates').put('myUrl', 'my partial'); + + var called = 0; + // we want to assert only during first watch + $rootScope.$watch(function() { + if (!called++) expect(element.text()).toBe(''); + }); + + $rootScope.$digest(); + expect(element.text()).toBe('my partial'); + })); })); @@ -587,6 +632,36 @@ describe("widget", function() { expect($rootScope.$element.text()).toEqual('2'); })); + + it('should clear the content when error during xhr request', + inject(function($route, $location, $rootScope, $browser) { + $route.when('/foo', {controller: noop, template: 'myUrl1'}); + + $location.path('/foo'); + $browser.xhr.expectGET('myUrl1').respond(404, ''); + $rootScope.$element.text('content'); + + $rootScope.$digest(); + $browser.xhr.flush(); + + expect($rootScope.$element.text()).toBe(''); + })); + + it('should be async even if served from cache', + inject(function($route, $rootScope, $location, $cacheFactory) { + $route.when('/foo', {controller: noop, template: 'myUrl1'}); + $cacheFactory.get('templates').put('myUrl1', 'my partial'); + $location.path('/foo'); + + var called = 0; + // we want to assert only during first watch + $rootScope.$watch(function() { + if (!called++) expect(element.text()).toBe(''); + }); + + $rootScope.$digest(); + expect(element.text()).toBe('my partial'); + })); }); |
