|
| 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 | +}); |
0 commit comments