Skip to content

Commit ab85151

Browse files
committed
test: migrate from chai and sinon to jest framework for writing tests
1 parent 23fce2e commit ab85151

19 files changed

+1152
-1263
lines changed

.babelrc

Lines changed: 0 additions & 4 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ pids
88
*.pid
99
*.seed
1010

11-
# Directory for instrumented libs generated by jscoverage/JSCover
12-
lib-cov
13-
14-
# Coverage directory used by tools like istanbul
11+
# Coverage directory used by tools like istanbul
1512
coverage
1613
.coveralls.yml
1714

@@ -27,3 +24,5 @@ build/Release
2724
# Dependency directory
2825
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2926
node_modules
27+
.vscode
28+
.DS_STORE

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
3+
};

jest.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
transform: {
3+
'^.+\\.jsx?$': 'babel-jest',
4+
},
5+
// This is for handling extensions, if needed
6+
moduleFileExtensions: ['js', 'jsx', 'json', 'node', 'mjs'],
7+
// This might be required if you want to ignore some files
8+
transformIgnorePatterns: ['/node_modules/'],
9+
testEnvironment: 'jest-environment-jsdom',
10+
};

test/basic.spec.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

test/basic.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import SecureLS from '../src/index';
2+
3+
let lib;
4+
5+
var localStorageMock = (function () {
6+
var store = {};
7+
return {
8+
getItem: function (key) {
9+
return store[key];
10+
},
11+
setItem: function (key, value) {
12+
store[key] = value.toString();
13+
},
14+
clear: function () {
15+
store = {};
16+
},
17+
removeItem: function (key) {
18+
delete store[key];
19+
},
20+
};
21+
})();
22+
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
23+
24+
describe('Basic suites', () => {
25+
beforeAll(() => {});
26+
27+
describe('instance creation', () => {
28+
lib = new SecureLS();
29+
30+
test('should check correct instance creation', () => {
31+
expect(lib).toBeInstanceOf(SecureLS);
32+
});
33+
34+
test('should return the name', () => {
35+
expect(lib._name).toBe('secure-ls');
36+
});
37+
});
38+
39+
describe('constructor', () => {
40+
lib = new SecureLS();
41+
42+
test('should be called on instance creation', () => {
43+
expect(lib._name).toBeDefined();
44+
expect(lib.Base64).toBeDefined();
45+
expect(lib.LZString).toBeDefined();
46+
expect(lib.AES).toBeDefined();
47+
expect(lib.DES).toBeDefined();
48+
expect(lib.RABBIT).toBeDefined();
49+
expect(lib.RC4).toBeDefined();
50+
expect(lib.enc).toBeDefined();
51+
expect(lib.storage).toBeDefined();
52+
expect(lib.config).toBeDefined();
53+
expect(lib.config).toBeInstanceOf(Object);
54+
expect(lib.config).toHaveProperty('encodingType');
55+
expect(lib.config).toHaveProperty('isCompression');
56+
});
57+
58+
test('should call init method', () => {
59+
const spy = jest.spyOn(lib, 'init');
60+
61+
// mock as if new instance is created but actually not
62+
// Can't expect otherwise Object reference would be lost
63+
expect(spy).not.toHaveBeenCalled();
64+
lib.init();
65+
expect(spy).toHaveBeenCalled();
66+
spy.mockRestore(); // Reset spy after test
67+
});
68+
});
69+
});

test/functional.spec.js

Lines changed: 0 additions & 217 deletions
This file was deleted.

0 commit comments

Comments
 (0)