Get tests working

master
Gregory Pike 10 years ago
parent b5c681b6f6
commit 58da1088d8

@ -25,73 +25,75 @@ describe('localStorageService', function() {
return function($window, localStorageService) { return function($window, localStorageService) {
elmSpy = spyOn($window.localStorage, 'getItem').andCallThrough(); elmSpy = spyOn($window.localStorage, 'getItem').andCallThrough();
localStorageService.get(key); localStorageService.get(key);
} };
} }
function addItem(key, value) { function addItem(key, value) {
return function($window, localStorageService) { return function($window, localStorageService) {
elmSpy = spyOn($window.localStorage, 'setItem').andCallThrough(); elmSpy = spyOn($window.localStorage, 'setItem').andCallThrough();
localStorageService.set(key, value); localStorageService.set(key, value);
} };
} }
function removeItem(key) { function removeItem(key) {
return function($window, localStorageService) { return function($window, localStorageService) {
elmSpy = spyOn($window.localStorage, 'removeItem').andCallThrough(); elmSpy = spyOn($window.localStorage, 'removeItem').andCallThrough();
localStorageService.remove(key); localStorageService.remove(key);
} };
} }
//Expectations //Expectations
function expectGetting(key) { function expectGetting(key) {
return function() { return function() {
expect(elmSpy).toHaveBeenCalledWith(key); expect(elmSpy).toHaveBeenCalledWith(key);
} };
} }
function expectAdding(key, value) { function expectAdding(key, value) {
return function() { return function() {
expect(elmSpy).toHaveBeenCalledWith(key, value); expect(elmSpy).toHaveBeenCalledWith(key, value);
} };
} }
function expectRemoving(key) { function expectRemoving(key) {
return function() { return function() {
expect(elmSpy).toHaveBeenCalledWith(key); expect(elmSpy).toHaveBeenCalledWith(key);
} };
} }
function expectMatching(key, expected) { function expectMatching(key, expected) {
return function(localStorageService) { return function(localStorageService) {
expect(localStorageService.get(key)).toEqual(expected); expect(localStorageService.get(key)).toEqual(expected);
} };
} }
function expectStorageTyping(type) { function expectStorageTyping(type) {
return function(localStorageService) { return function(localStorageService) {
expect(localStorageService.getStorageType()).toEqual(type); expect(localStorageService.getStorageType()).toEqual(type);
} };
} }
function expectSupporting(expected) { function expectSupporting(expected) {
return function(localStorageService) { return function(localStorageService) {
expect(localStorageService.isSupported).toEqual(expected); expect(localStorageService.isSupported).toEqual(expected);
} };
} }
function expectDomain(domain) { function expectDomain(domain) {
return function($document, localStorageService) { return function($document, localStorageService) {
localStorageService.set('foo','bar'); //Should trigger first time localStorageService.set('foo','bar'); //Should trigger first time
expect($document.cookie.indexOf('domain=' + domain)).not.toEqual(-1); expect($document.cookie.indexOf('domain=' + domain)).not.toEqual(-1);
} };
} }
function expectCookieConfig(exp, path) { function expectCookieConfig(exp, path) {
return function($document, localStorageService) { return function($document, localStorageService) {
localStorageService.set('foo','bar'); //Should trigger first time localStorageService.set('foo','bar'); //Should trigger first time
expect($document.cookie.indexOf('expires=' + exp)).not.toEqual(-1); // Just compare the expiry date, not the time, because of daylight savings
var expiryStringPartial = exp.substr(0, exp.indexOf(new Date().getFullYear()));
expect($document.cookie.indexOf('expires=' + expiryStringPartial)).not.toEqual(-1);
expect($document.cookie.indexOf('path=' + path)).not.toEqual(-1); expect($document.cookie.indexOf('path=' + path)).not.toEqual(-1);
} };
} }
//Provider //Provider
@ -110,19 +112,19 @@ describe('localStorageService', function() {
function setStorage(type) { function setStorage(type) {
return function(localStorageServiceProvider) { return function(localStorageServiceProvider) {
localStorageServiceProvider.setStorageType(type); localStorageServiceProvider.setStorageType(type);
} };
} }
function setCookieDomain(domain) { function setCookieDomain(domain) {
return function(localStorageServiceProvider) { return function(localStorageServiceProvider) {
localStorageServiceProvider.setStorageCookieDomain(domain); localStorageServiceProvider.setStorageCookieDomain(domain);
} };
} }
function setStorageCookie(exp, path) { function setStorageCookie(exp, path) {
return function(localStorageServiceProvider) { return function(localStorageServiceProvider) {
localStorageServiceProvider.setStorageCookie(exp, path); localStorageServiceProvider.setStorageCookie(exp, path);
} };
} }
beforeEach(module('LocalStorageModule', function($provide) { beforeEach(module('LocalStorageModule', function($provide) {
@ -277,7 +279,7 @@ describe('localStorageService', function() {
inject(function($window, localStorageService) { inject(function($window, localStorageService) {
var setSpy = spyOn($window.sessionStorage, 'setItem'), var setSpy = spyOn($window.sessionStorage, 'setItem'),
getSpy = spyOn($window.sessionStorage, 'getItem'), getSpy = spyOn($window.sessionStorage, 'getItem'),
removeSpy = spyOn($window.sessionStorage, 'removeItem') removeSpy = spyOn($window.sessionStorage, 'removeItem');
localStorageService.set('foo', 'bar'); localStorageService.set('foo', 'bar');
localStorageService.get('foo'); localStorageService.get('foo');
@ -302,14 +304,14 @@ describe('localStorageService', function() {
inject( inject(
expectSupporting(true) expectSupporting(true)
); );
}) });
}); });
//cookie //cookie
describe('Cookie', function() { describe('Cookie', function() {
beforeEach(module('LocalStorageModule', function($provide, localStorageServiceProvider) { beforeEach(module('LocalStorageModule', function($provide) {
$provide.value('$window', { $provide.value('$window', {
localStorage: false, localStorage: false,
sessionStorage: false sessionStorage: false