Added tests

This commit is contained in:
Zakary Timson 2018-05-03 15:15:20 -04:00
parent e21c8a23de
commit db4784520b
4 changed files with 6229 additions and 4 deletions

6133
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,8 +5,8 @@
"main": "./lib/index.js", "main": "./lib/index.js",
"typings": "./lib/index.d.ts", "typings": "./lib/index.d.ts",
"scripts": { "scripts": {
"prepare": "npm run build", "build": "tsc",
"build": "tsc" "test": "jest"
}, },
"files": [ "files": [
"lib" "lib"
@ -18,11 +18,24 @@
], ],
"author": "Zak Timson", "author": "Zak Timson",
"license": "ISC", "license": "ISC",
"jest": {
"moduleFileExtensions": [
"ts",
"js"
],
"transform": {
"\\.ts$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "/src/.*\\.spec\\.ts$"
},
"dependencies": { "dependencies": {
"@types/crypto-js": "^3.1.39", "@types/crypto-js": "^3.1.39",
"crypto-js": "^3.1.9-1" "crypto-js": "^3.1.9-1"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^22.2.3",
"jest": "^22.4.3",
"ts-jest": "^22.4.4",
"typescript": "^2.8.3" "typescript": "^2.8.3"
} }
} }

80
src/webStorage.spec.ts Normal file
View File

@ -0,0 +1,80 @@
import {LocalStorage, SessionStorage} from './index'
class TestClass {
@LocalStorage() localStorage: any;
@SessionStorage() sessionStorage: any;
@LocalStorage({fieldName: 'customKey'}) customKey: any;
@LocalStorage({fieldName: 'encrypted', encryptionKey: 'ENCRYPTION_KEY'}) encrypted: any;
@LocalStorage({defaultValue: 'test'}) defaultedStorage: any;
@LocalStorage({defaultValue: {a: true, b: 'test', c: 3.14}}) objectDefault: any;
}
describe('LocalStorage Tests', () => {
it('LocalStorage', () => {
let testValue = Math.random().toString(36).substring(7);
new TestClass().localStorage = testValue;
expect(JSON.parse(<string>localStorage.getItem('_localStorage'))).toEqual(testValue);
});
it('SessionStorage', () => {
let testValue = Math.random().toString(36).substring(7);
new TestClass().sessionStorage = testValue;
expect(JSON.parse(<string>sessionStorage.getItem('_sessionStorage'))).toEqual(testValue);
});
it('Custom Key', () => {
let testValue = Math.random().toString(36).substring(7);
new TestClass().customKey = testValue;
expect(JSON.parse(<string>localStorage.getItem('customKey'))).toEqual(testValue);
});
it('Maintain Object Structure', () => {
let testObject = new TestClass();
let testValue = {a: Math.random().toString(36).substring(7), b: Math.random(), c: true, d: [{a: false}, {a: true}]};
testObject.localStorage = testValue;
expect(testObject.localStorage).toEqual(testValue);
});
describe('Default', () => {
it('Default Value', () => {
localStorage.removeItem('_defaultedStorage');
let testObject = new TestClass();
expect(testObject.defaultedStorage).toEqual('test');
});
it('Key Already Has Value', () => {
let testValue = {a: Math.random().toString(36).substring(7), b: Math.random(), c: true, d: [{a: false}, {a: true}]};
localStorage.setItem('_defaultedStorage', JSON.stringify(testValue));
let testObject = new TestClass();
expect(testObject.defaultedStorage).toEqual(testValue);
});
it('Using Object As Default', () => {
let testObject = new TestClass();
expect(testObject.objectDefault).toEqual({a: true, b: 'test', c: 3.14});
})
});
describe('Encryption Tests', () => {
it('Encrypt', () => {
let testObject = new TestClass();
let testValue = Math.random().toString(36).substring(7);
testObject.encrypted = testValue;
expect(JSON.parse(<string>localStorage.getItem('encrypted'))).not.toEqual(testValue);
});
it('Decrypt', () => {
let testObject = new TestClass();
let testValue = Math.random().toString(36).substring(7);
testObject.encrypted = testValue;
expect(testObject.encrypted).toEqual(testValue);
});
it('Maintain Object Structure', () => {
let testObject = new TestClass();
let testValue = {a: Math.random().toString(36).substring(7), b: Math.random(), c: true, d: [{a: false}, {a: true}]};
testObject.encrypted = testValue;
expect(testObject.encrypted).toEqual(testValue);
});
});
});

View File

@ -4,7 +4,8 @@
"module": "commonjs", "module": "commonjs",
"declaration": true, "declaration": true,
"outDir": "lib", "outDir": "lib",
"strict": true "strict": true,
"experimentalDecorators": true,
}, },
"include": [ "include": [
"index.ts", "index.ts",