Updated for tests and linting

revert-117-master
Gregory Pike 10 years ago
parent d236005a44
commit d699266785

@ -16,7 +16,7 @@ module.exports = function(grunt) {
], ],
configFile: 'test/karma.conf.js', configFile: 'test/karma.conf.js',
reporters: ['dots'], reporters: ['dots'],
singleRun: false singleRun: true
}, },
unit: {} unit: {}
}, },
@ -29,9 +29,7 @@ module.exports = function(grunt) {
}, },
dev: { dev: {
src: ['angular-local-storage.js'], src: ['angular-local-storage.js'],
options: { options: {}
jshintrc: '.jshintrc',
}
}, },
test: { test: {
src: ['test/spec/**/*.js'], src: ['test/spec/**/*.js'],

@ -215,10 +215,10 @@ angularLocalStorage.provider('localStorageService', function() {
// Should be used mostly for development purposes // Should be used mostly for development purposes
var clearAllFromLocalStorage = function (regularExpression) { var clearAllFromLocalStorage = function (regularExpression) {
var regularExpression = regularExpression || ""; regularExpression = regularExpression || "";
//accounting for the '.' in the prefix when creating a regex //accounting for the '.' in the prefix when creating a regex
var tempPrefix = prefix.slice(0, -1) + "\."; var tempPrefix = prefix.slice(0, -1);
var testRegex = RegExp(tempPrefix + regularExpression); var testRegex = new RegExp(tempPrefix + '.' + regularExpression);
if (!browserSupportsLocalStorage) { if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED'); $rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
@ -357,7 +357,7 @@ angularLocalStorage.provider('localStorageService', function() {
clearAll: clearAllFromCookies clearAll: clearAllFromCookies
} }
}; };
}] }];
}); });
}).call(this); }).call(this);

@ -40,10 +40,10 @@ module.exports = function(config) {
logLevel: config.LOG_INFO, logLevel: config.LOG_INFO,
// web server port // web server port
port: 8080, port: 8999,
// Continuous Integration mode // Continuous Integration mode
// if true, it capture browsers, run tests and exit // if true, it capture browsers, run tests and exit
singleRun: false singleRun: true,
}); });
}; };

@ -1,55 +1,59 @@
describe('Tests functionality of the localStorage module', function(){ 'use strict';
beforeEach(module('LocalStorageModule', function(localStorageServiceProvider){
p = localStorageServiceProvider;
}));
var ls, store = [];
beforeEach(inject(function(_localStorageService_){
ls = _localStorageService_;
spyOn(ls, 'get').andCallFake(function(key){
if(store[key].charAt(0) === "{" || store[key].charAt(0) === "["){
return angular.fromJson(store[key]);
}else{
return store[key];
}
});
spyOn(ls, 'set').andCallFake(function(key, val){
if(angular.isObject(val) || angular.isArray(val)){
val = angular.toJson(val);
}
if(angular.isNumber(val)){
val = val.toString();
}
return store[key] = val;
});
spyOn(ls, 'clearAll').andCallFake(function(){
store = {};
return store;
});
}));
it("Should add a value to my local storage", function(){
var n = 234;
ls.set('test', n);
//Since localStorage makes the value a string, we look for the '234' and not 234
expect(ls.get('test')).toBe('234');
var obj = { key: 'val' };
ls.set('object', obj);
var res = ls.get('object');
expect(res.key).toBe('val');
describe('Tests functionality of the localStorage module', function() {
var ls, p, store = [];
beforeEach(module('LocalStorageModule', function(localStorageServiceProvider) {
p = localStorageServiceProvider;
}));
beforeEach(inject(function(_localStorageService_) {
ls = _localStorageService_;
spyOn(ls, 'get').andCallFake(function(key) {
if (store[key].charAt(0) === '{' || store[key].charAt(0) === '[') {
return angular.fromJson(store[key]);
} else {
return store[key];
}
}); });
it('Should allow me to set a prefix', function(){ spyOn(ls, 'set').andCallFake(function(key, val) {
p.setPrefix("myPref"); if (angular.isObject(val) || angular.isArray(val)) {
expect(p.prefix).toBe("myPref"); val = angular.toJson(val);
}
if (angular.isNumber(val)){
val = val.toString();
}
store[key] = val;
return store[key];
}); });
it('Should allow me to set the cookie values', function(){ spyOn(ls, 'clearAll').andCallFake(function() {
p.setStorageCookie(60, '/path'); store = {};
expect(p.cookie.expiry).toBe(60); return store;
expect(p.cookie.path).toBe('/path');
}); });
}));
it('Should add a value to my local storage', function() {
var n = 234;
ls.set('test', n);
//Since localStorage makes the value a string, we look for the '234' and not 234
expect(ls.get('test')).toBe('234');
var obj = { key: 'val' };
ls.set('object', obj);
var res = ls.get('object');
expect(res.key).toBe('val');
});
it('Should allow me to set a prefix', function() {
p.setPrefix('myPref');
expect(p.prefix).toBe('myPref');
});
it('Should allow me to set the cookie values', function() {
p.setStorageCookie(60, '/path');
expect(p.cookie.expiry).toBe(60);
expect(p.cookie.path).toBe('/path');
});
}); });