Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
feat(cookies): use $cookies (1.4+) or $cookieStore (<1.4)
Browse files Browse the repository at this point in the history
Solves #1061
  • Loading branch information
knalli committed Feb 11, 2017
1 parent d26d9ef commit 51330f5
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 8 deletions.
38 changes: 31 additions & 7 deletions src/service/storage-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,34 @@ angular.module('pascalprecht.translate')
*/
.factory('$translateCookieStorage', $translateCookieStorageFactory);

function $translateCookieStorageFactory($cookieStore) {
function $translateCookieStorageFactory($injector) {

'use strict';

// Since AngularJS 1.4, $cookieStore is deprecated
var delegate;
if (angular.version.major === 1 && angular.version.minor >= 4) {
var $cookies = $injector.get('$cookies');
delegate = {
get : function (key) {
return $cookies.get(key);
},
put : function (key, value) {
$cookies.put(key, value);
}
};
} else {
var $cookieStore = $injector.get('$cookieStore');
delegate = {
get : function (key) {
return $cookieStore.get(key);
},
put : function (key, value) {
$cookieStore.put(key, value);
}
};
}

var $translateCookieStorage = {

/**
Expand All @@ -29,8 +53,8 @@ function $translateCookieStorageFactory($cookieStore) {
* @param {string} name Item name
* @return {string} Value of item name
*/
get: function (name) {
return $cookieStore.get(name);
get : function (name) {
return delegate.get(name);
},

/**
Expand All @@ -46,8 +70,8 @@ function $translateCookieStorageFactory($cookieStore) {
* @param {string} name Item name
* @param {string} value Item value
*/
set: function (name, value) {
$cookieStore.put(name, value);
set : function (name, value) {
delegate.put(name, value);
},

/**
Expand All @@ -61,8 +85,8 @@ function $translateCookieStorageFactory($cookieStore) {
* @param {string} name Item name
* @param {string} value Item value
*/
put: function (name, value) {
$cookieStore.put(name, value);
put : function (name, value) {
delegate.put(name, value);
}
};

Expand Down
96 changes: 95 additions & 1 deletion test/unit/service/storage-cookie.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('pascalprecht.translate', function () {
});
});

describe('$translate#storage', function () {
describe('$translate#storage (with ngCookies)', function () {

beforeEach(module('pascalprecht.translate', 'ngCookies', function ($translateProvider) {
$translateProvider.useStorage('$translateCookieStorage');
Expand All @@ -93,4 +93,98 @@ describe('pascalprecht.translate', function () {
expect($translate.storage().get).toBeDefined();
});
});

if (angular.version.major === 1 && angular.version.minor >= 4) {
describe('$translate#storage (with fake $cookies)', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {
$provide.factory('$cookies', function () {
var data = {};
return {
get : function (key) {
return data[key];
},
put : function (key, value) {
data[key] = value;
}
};
});
$translateProvider.useStorage('$translateCookieStorage');
}));

var $translate, $translateCookieStorage;

beforeEach(inject(function (_$translate_, _$translateCookieStorage_) {
$translate = _$translate_;
$translateCookieStorage = _$translateCookieStorage_;
}));

it('should be defined', function () {
expect($translate.storage).toBeDefined();
});

it('should be a function', function () {
expect(typeof $translate.storage).toBe('function');
});

it('should return registered storage instance if exists', function () {
expect(typeof $translate.storage()).toBe('object');
expect($translate.storage().set).toBeDefined(); // deprecated
expect($translate.storage().put).toBeDefined();
expect($translate.storage().get).toBeDefined();
});

it('should return value again', function () {
$translateCookieStorage.put('ABCD', 'EFGH');
expect($translateCookieStorage.get('ABCD', 'EFGH')).toBe('EFGH');
});
});
}

if (angular.version.major === 1 && angular.version.minor < 4) {
describe('$translate#storage (with fake $cookieStore)', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {
$provide.factory('$cookieStore', function () {
var data = {};
return {
get : function (key) {
return data[key];
},
put : function (key, value) {
data[key] = value;
}
};
});
$translateProvider.useStorage('$translateCookieStorage');
}));

var $translate, $translateCookieStorage;

beforeEach(inject(function (_$translate_, _$translateCookieStorage_) {
$translate = _$translate_;
$translateCookieStorage = _$translateCookieStorage_;
}));

it('should be defined', function () {
expect($translate.storage).toBeDefined();
});

it('should be a function', function () {
expect(typeof $translate.storage).toBe('function');
});

it('should return registered storage instance if exists', function () {
expect(typeof $translate.storage()).toBe('object');
expect($translate.storage().set).toBeDefined(); // deprecated
expect($translate.storage().put).toBeDefined();
expect($translate.storage().get).toBeDefined();
});

it('should return value again', function () {
$translateCookieStorage.put('ABCD', 'EFGH');
expect($translateCookieStorage.get('ABCD', 'EFGH')).toBe('EFGH');
});
});
}
});

0 comments on commit 51330f5

Please sign in to comment.