diff --git a/package.json b/package.json index c69a454..c6bb2cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "secure-ls", - "version": "1.2.6", + "version": "1.3.0", "description": "Secure localStorage data with high level of encryption and data compression", "main": "./dist/secure-ls.js", "typings": "./dist/secure-ls.d.ts", @@ -56,6 +56,7 @@ "homepage": "https://github.com/softvar/secure-ls", "dependencies": { "crypto-js": "^3.1.6", - "lz-string": "^1.4.4" + "lz-string": "^1.4.4", + "window-or-global": "^1.0.1" } } diff --git a/src/index.js b/src/index.js index ab9da75..42b8152 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import root from 'window-or-global'; import utils from './utils'; import constants from './constants'; import enc from './enc-utf8'; @@ -9,6 +10,13 @@ import DES from 'crypto-js/tripledes'; import RABBIT from 'crypto-js/rabbit'; import RC4 from 'crypto-js/rc4'; +const globalStorage = { + setItem: () => {}, + getItem: () => {}, + removeItem: () => {}, + clear: () => {} +}; + export default class SecureLS { constructor(config) { config = config || {}; @@ -36,7 +44,7 @@ export default class SecureLS { config.encodingType.toLowerCase() : constants.EncrytionTypes.BASE64; - this.ls = localStorage; + this.ls = root.localStorage || globalStorage; this.init(); }; diff --git a/test/library.spec.js b/test/library.spec.js index 28317a5..6ef167b 100644 --- a/test/library.spec.js +++ b/test/library.spec.js @@ -5,3 +5,4 @@ import './utils.spec'; import './ls-data-compression.spec'; import './ls-data-enc-dec.spec'; import './functional.spec'; +import './windowOrGlobal.spec'; diff --git a/test/windowOrGlobal.spec.js b/test/windowOrGlobal.spec.js new file mode 100644 index 0000000..377696b --- /dev/null +++ b/test/windowOrGlobal.spec.js @@ -0,0 +1,51 @@ +import sinon from 'sinon'; +import SecureLS from '../dist/secure-ls.js'; + +describe('root - LocalStorage Tests->', () => { + let lib; + + beforeEach(() => { + global.localStorage = { + setItem: sinon.spy(), + getItem: sinon.spy(), + removeItem: sinon.spy(), + clear: sinon.spy() + }; + lib = new SecureLS(); + }); + + describe('setItem method', () => { + it('set should call setItem on localStorage', () => { + const data = [1, 2, 3]; + const key = 'key-1'; + + lib.set(key, data); + sinon.assert.calledWith(global.localStorage.setItem, key); + }); + }); + + describe('getItem method', () => { + it('get should call getItem on localStorage', () => { + const key = 'key-1'; + + lib.get(key); + sinon.assert.calledWith(global.localStorage.getItem, key); + }); + }); + + describe('removeItem method', () => { + it('remove should call removeItem on localStorage', () => { + const key = 'key-1'; + + lib.remove(key); + sinon.assert.calledWith(global.localStorage.removeItem, key); + }); + }); + + describe('clear method', () => { + it('clear should call clear on localStorage', () => { + lib.clear(); + sinon.assert.calledWith(global.localStorage.clear); + }); + }); +}); diff --git a/yarn.lock b/yarn.lock index cc20f57..f520f11 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3739,6 +3739,11 @@ wide-align@^1.1.0: dependencies: string-width "^1.0.2 || 2" +window-or-global@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/window-or-global/-/window-or-global-1.0.1.tgz#dbe45ba2a291aabc56d62cf66c45b7fa322946de" + integrity sha1-2+RboqKRqrxW1iz2bEW3+jIpRt4= + window-size@0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"