|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { Storage } from './storage'; |
| 21 | +import { IStorage, IStorageWrapper } from './types'; |
| 22 | + |
| 23 | +const payload = { first: 'john', last: 'smith' }; |
| 24 | +const createMockStore = (): MockedKeys<IStorage> => { |
| 25 | + let store: Record<string, any> = {}; |
| 26 | + return { |
| 27 | + getItem: jest.fn().mockImplementation((key) => store[key]), |
| 28 | + setItem: jest.fn().mockImplementation((key, value) => (store[key] = value)), |
| 29 | + removeItem: jest.fn().mockImplementation((key: string) => delete store[key]), |
| 30 | + clear: jest.fn().mockImplementation(() => (store = {})), |
| 31 | + }; |
| 32 | +}; |
| 33 | + |
| 34 | +describe('StorageService', () => { |
| 35 | + let storage: IStorageWrapper; |
| 36 | + let mockStore: MockedKeys<IStorage>; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + jest.resetAllMocks(); |
| 40 | + mockStore = createMockStore(); |
| 41 | + storage = new Storage(mockStore); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('expected API', () => { |
| 45 | + test('should have expected methods', () => { |
| 46 | + expect(typeof storage.get).toBe('function'); |
| 47 | + expect(typeof storage.set).toBe('function'); |
| 48 | + expect(typeof storage.remove).toBe('function'); |
| 49 | + expect(typeof storage.clear).toBe('function'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('call behavior', () => { |
| 54 | + test('should call getItem on the store', () => { |
| 55 | + storage.get('name'); |
| 56 | + |
| 57 | + expect(mockStore.getItem).toHaveBeenCalledTimes(1); |
| 58 | + }); |
| 59 | + |
| 60 | + test('should call setItem on the store', () => { |
| 61 | + storage.set('name', 'john smith'); |
| 62 | + |
| 63 | + expect(mockStore.setItem).toHaveBeenCalledTimes(1); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should call removeItem on the store', () => { |
| 67 | + storage.remove('name'); |
| 68 | + |
| 69 | + expect(mockStore.removeItem).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should call clear on the store', () => { |
| 73 | + storage.clear(); |
| 74 | + |
| 75 | + expect(mockStore.clear).toHaveBeenCalledTimes(1); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('json data', () => { |
| 80 | + test('should parse JSON when reading from the store', () => { |
| 81 | + mockStore.getItem = jest.fn().mockImplementationOnce(() => JSON.stringify(payload)); |
| 82 | + |
| 83 | + const data = storage.get('name'); |
| 84 | + expect(data).toEqual(payload); |
| 85 | + }); |
| 86 | + |
| 87 | + test('should write JSON string to the store', () => { |
| 88 | + const key = 'name'; |
| 89 | + const value = payload; |
| 90 | + |
| 91 | + storage.set(key, value); |
| 92 | + expect(mockStore.setItem).toHaveBeenCalledWith(key, JSON.stringify(value)); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('expected responses', () => { |
| 97 | + test('should return null when not exists', () => { |
| 98 | + const data = storage.get('notexists'); |
| 99 | + expect(data).toBe(null); |
| 100 | + }); |
| 101 | + |
| 102 | + test('should return null when invalid JSON', () => { |
| 103 | + mockStore.getItem = jest.fn().mockImplementationOnce(() => 'not: json'); |
| 104 | + |
| 105 | + const data = storage.get('name'); |
| 106 | + expect(data).toBe(null); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments