|
| 1 | +import StorageManager from '../../src/StorageManager' |
| 2 | + |
| 3 | +describe(StorageManager, () => { |
| 4 | + let key = '' |
| 5 | + |
| 6 | + beforeEach(() => key = `key${Math.floor(Math.random() * 10000)}`) |
| 7 | + |
| 8 | + afterEach(() => { |
| 9 | + localStorage.clear() |
| 10 | + sessionStorage.clear() |
| 11 | + jest.resetAllMocks() |
| 12 | + }) |
| 13 | + |
| 14 | + describe('localStorage', () => { |
| 15 | + it('retrieves key from storage', () => { |
| 16 | + localStorage[key] = 'foo' |
| 17 | + |
| 18 | + expect(StorageManager.get(key, 'local')).toEqual('foo') |
| 19 | + }) |
| 20 | + |
| 21 | + it('removes key from storage', () => { |
| 22 | + localStorage[key] = 'foo' |
| 23 | + |
| 24 | + StorageManager.remove(key) |
| 25 | + |
| 26 | + expect(StorageManager.get(key, 'local')).toEqual(undefined) |
| 27 | + }) |
| 28 | + |
| 29 | + it('falls back to alternate storage if storage is unavailable', () => { |
| 30 | + jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => { throw 1 }) |
| 31 | + |
| 32 | + expect(StorageManager.get(key, 'local')).toEqual(undefined) |
| 33 | + |
| 34 | + StorageManager.save(key, 'foo', 'local') |
| 35 | + |
| 36 | + expect(StorageManager.get(key, 'local')).toEqual('foo') |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + describe('sessionStorage', () => { |
| 41 | + it('removes key from storage', () => { |
| 42 | + sessionStorage.setItem(key, 'foo') |
| 43 | + |
| 44 | + StorageManager.remove(key) |
| 45 | + |
| 46 | + expect(StorageManager.get(key, 'session')).toEqual(undefined) |
| 47 | + }) |
| 48 | + |
| 49 | + it('falls back to alternate storage if storage is unavailable', () => { |
| 50 | + jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => { throw 1 }) |
| 51 | + |
| 52 | + expect(StorageManager.get(key, 'session')).toEqual(undefined) |
| 53 | + |
| 54 | + StorageManager.save(key, 'foo', 'session') |
| 55 | + |
| 56 | + expect(StorageManager.get(key, 'session')).toEqual('foo') |
| 57 | + }) |
| 58 | + }) |
| 59 | +}) |
0 commit comments