diff options
| author | Igor Minar | 2011-02-15 01:12:45 -0500 |
|---|---|---|
| committer | Igor Minar | 2011-02-15 11:01:53 -0500 |
| commit | 1777110958f76ee4be5760e36c96702223385918 (patch) | |
| tree | 5aa03b246507e66877e5eac69e58e004e244d7a5 /test/service/cookiesSpec.js | |
| parent | d2089a16335276eecb8d81eb17332c2dff2cf1a2 (diff) | |
| download | angular.js-1777110958f76ee4be5760e36c96702223385918.tar.bz2 | |
split up services into individual files
- split up services into files under src/service
- split up specs into files under test/service
- rewrite all specs so that they don't depend on one global forEach
- get rid of obsolete code and tests in ng:switch
- rename mock $log spec from "$log" to "$log mock"
Diffstat (limited to 'test/service/cookiesSpec.js')
| -rw-r--r-- | test/service/cookiesSpec.js | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/test/service/cookiesSpec.js b/test/service/cookiesSpec.js new file mode 100644 index 00000000..11551393 --- /dev/null +++ b/test/service/cookiesSpec.js @@ -0,0 +1,98 @@ +describe('$cookies', function() { + var scope, $browser; + + beforeEach(function() { + $browser = new MockBrowser(); + $browser.cookieHash['preexisting'] = 'oldCookie'; + scope = angular.scope(null, angular.service, {$browser: $browser}); + scope.$cookies = scope.$service('$cookies'); + }); + + afterEach(function(){ + dealoc(scope); + }); + + + it('should provide access to existing cookies via object properties and keep them in sync', + function(){ + expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'}); + + // access internal cookie storage of the browser mock directly to simulate behavior of + // document.cookie + $browser.cookieHash['brandNew'] = 'cookie'; + $browser.poll(); + + expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie'}); + + $browser.cookieHash['brandNew'] = 'cookie2'; + $browser.poll(); + expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie2'}); + + delete $browser.cookieHash['brandNew']; + $browser.poll(); + expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'}); + }); + + + it('should create or update a cookie when a value is assigned to a property', function() { + scope.$cookies.oatmealCookie = 'nom nom'; + scope.$eval(); + + expect($browser.cookies()). + toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'}); + + scope.$cookies.oatmealCookie = 'gone'; + scope.$eval(); + + expect($browser.cookies()). + toEqual({'preexisting': 'oldCookie', 'oatmealCookie': 'gone'}); + }); + + + it('should drop or reset any cookie that was set to a non-string value', function() { + scope.$cookies.nonString = [1, 2, 3]; + scope.$cookies.nullVal = null; + scope.$cookies.undefVal = undefined; + scope.$cookies.preexisting = function(){}; + scope.$eval(); + expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'}); + expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'}); + }); + + + it('should remove a cookie when a $cookies property is deleted', function() { + scope.$cookies.oatmealCookie = 'nom nom'; + scope.$eval(); + $browser.poll(); + expect($browser.cookies()). + toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'}); + + delete scope.$cookies.oatmealCookie; + scope.$eval(); + + expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'}); + }); + + + it('should drop or reset cookies that browser refused to store', function() { + var i, longVal; + + for (i=0; i<5000; i++) { + longVal += '*'; + } + + //drop if no previous value + scope.$cookies.longCookie = longVal; + scope.$eval(); + expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'}); + + + //reset if previous value existed + scope.$cookies.longCookie = 'shortVal'; + scope.$eval(); + expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'}); + scope.$cookies.longCookie = longVal; + scope.$eval(); + expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'}); + }); +}); |
