Updated to 3.0

This commit is contained in:
2021-01-28 13:15:46 -05:00
parent 311a4311ac
commit 954049d992
6 changed files with 128 additions and 49 deletions

View File

@ -1,22 +1,32 @@
import {LocalStorage, SessionStorage} from "../src";
const CUSTOM_KEY = '__MY_KEY'
const CUSTOM_KEY = '_MY_KEY'
const ENCRYPTION_KEY = 'abc123';
class TestClass {
class TestType {
constructor(public first: string, public last: string) { }
fullName() { return `${this.last}, ${this.first}`; }
}
class TestStorage {
@LocalStorage() localStorage: any;
@LocalStorage({a: true, b: 'test', c: 3.14}) defaultedLocalStorage: any;
@LocalStorage(null, {key: CUSTOM_KEY}) customLocalStorage: any;
@LocalStorage(null, {encryptWith: ENCRYPTION_KEY}) encryptedLocalStorage: any;
@LocalStorage(new TestType('John', 'Smith')) objectLocalStorage!: TestType;
@SessionStorage() sessionStorage: any;
@SessionStorage({a: true, b: 'test', c: 3.14}) defaultedSessionStorage: any;
@SessionStorage(null, {key: CUSTOM_KEY}) customSessionStorage: any;
@SessionStorage(null, {encryptWith: ENCRYPTION_KEY}) encryptedSessionStorage: any;
@SessionStorage(new TestType('John', 'Smith')) objectSessionStorage!: TestType;
}
describe('WebStorage', () => {
let testComponent: TestClass;
describe('Webstorage Decorators', () => {
let testComponent: TestStorage;
beforeEach(() => {
localStorage.clear();
testComponent = new TestClass();
testComponent = new TestStorage();
});
describe('LocalStorage', () => {
@ -52,6 +62,26 @@ describe('WebStorage', () => {
expect(localStorage.getItem(CUSTOM_KEY)).toBe(JSON.stringify(testValue));
expect(testComponent.customLocalStorage).toBe(testValue);
});
test('Encrypted', () => {
const testValue = Math.random();
testComponent.encryptedLocalStorage = testValue;
expect(localStorage.getItem('encryptedLocalStorage')).not.toBe(JSON.stringify(testValue));
expect(testComponent.encryptedLocalStorage).toBe(testValue);
});
test('Impure Functions', () => {
testComponent.localStorage = [1];
testComponent.localStorage.push(2);
expect(localStorage.getItem('localStorage')).toStrictEqual(JSON.stringify([1]));
testComponent.localStorage; // Trigger save
expect(localStorage.getItem('localStorage')).toStrictEqual(JSON.stringify([1, 2]));
expect(testComponent.localStorage).toStrictEqual([1, 2]);
});
test('Object Functions', () => {
expect(testComponent.objectLocalStorage.fullName()).toEqual('Smith, John');
testComponent.objectLocalStorage.last = 'Snow';
testComponent.objectLocalStorage; // Trigger save
expect(testComponent.objectLocalStorage.fullName()).toEqual('Snow, John');
});
});
describe('SessionStorage', () => {
@ -87,5 +117,25 @@ describe('WebStorage', () => {
expect(sessionStorage.getItem(CUSTOM_KEY)).toBe(JSON.stringify(testValue));
expect(testComponent.customSessionStorage).toBe(testValue);
});
test('Encrypted', () => {
const testValue = Math.random();
testComponent.encryptedSessionStorage = testValue;
expect(sessionStorage.getItem('encryptedSessionStorage')).not.toBe(JSON.stringify(testValue));
expect(testComponent.encryptedSessionStorage).toBe(testValue);
});
test('Impure Functions', () => {
testComponent.sessionStorage = [1];
testComponent.sessionStorage.push(2);
expect(sessionStorage.getItem('sessionStorage')).toStrictEqual(JSON.stringify([1]));
testComponent.sessionStorage; // Trigger save
expect(sessionStorage.getItem('sessionStorage')).toStrictEqual(JSON.stringify([1, 2]));
expect(testComponent.sessionStorage).toStrictEqual([1, 2]);
});
test('Object Functions', () => {
expect(testComponent.objectSessionStorage.fullName()).toEqual('Smith, John');
testComponent.objectSessionStorage.last = 'Snow';
testComponent.objectSessionStorage; // Trigger save
expect(testComponent.objectSessionStorage.fullName()).toEqual('Snow, John');
});
});
});