|
| 1 | +import LocalStorage from '../../src/platform/LocalStorage'; |
| 2 | + |
| 3 | +it('can set values', async () => { |
| 4 | + const logger = { |
| 5 | + debug: jest.fn(), |
| 6 | + info: jest.fn(), |
| 7 | + warn: jest.fn(), |
| 8 | + error: jest.fn(), |
| 9 | + }; |
| 10 | + // Storage here needs to be the global browser 'Storage' not the interface |
| 11 | + // for our platform. |
| 12 | + const spy = jest.spyOn(Storage.prototype, 'setItem'); |
| 13 | + |
| 14 | + const storage = new LocalStorage(logger); |
| 15 | + storage.set('test-key', 'test-value'); |
| 16 | + expect(spy).toHaveBeenCalledWith('test-key', 'test-value'); |
| 17 | + |
| 18 | + expect(logger.debug).not.toHaveBeenCalled(); |
| 19 | + expect(logger.info).not.toHaveBeenCalled(); |
| 20 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 21 | + expect(logger.error).not.toHaveBeenCalled(); |
| 22 | +}); |
| 23 | + |
| 24 | +it('can handle an error setting a value', async () => { |
| 25 | + const logger = { |
| 26 | + debug: jest.fn(), |
| 27 | + info: jest.fn(), |
| 28 | + warn: jest.fn(), |
| 29 | + error: jest.fn(), |
| 30 | + }; |
| 31 | + // Storage here needs to be the global browser 'Storage' not the interface |
| 32 | + // for our platform. |
| 33 | + const spy = jest.spyOn(Storage.prototype, 'setItem'); |
| 34 | + spy.mockImplementation(() => { |
| 35 | + throw new Error('bad'); |
| 36 | + }); |
| 37 | + |
| 38 | + const storage = new LocalStorage(logger); |
| 39 | + storage.set('test-key', 'test-value'); |
| 40 | + |
| 41 | + expect(logger.debug).not.toHaveBeenCalled(); |
| 42 | + expect(logger.info).not.toHaveBeenCalled(); |
| 43 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 44 | + expect(logger.error).toHaveBeenCalledWith( |
| 45 | + 'Error setting key in localStorage: test-key, reason: Error: bad', |
| 46 | + ); |
| 47 | +}); |
| 48 | + |
| 49 | +it('can get values', async () => { |
| 50 | + const logger = { |
| 51 | + debug: jest.fn(), |
| 52 | + info: jest.fn(), |
| 53 | + warn: jest.fn(), |
| 54 | + error: jest.fn(), |
| 55 | + }; |
| 56 | + // Storage here needs to be the global browser 'Storage' not the interface |
| 57 | + // for our platform. |
| 58 | + const spy = jest.spyOn(Storage.prototype, 'getItem'); |
| 59 | + |
| 60 | + const storage = new LocalStorage(logger); |
| 61 | + storage.get('test-key'); |
| 62 | + expect(spy).toHaveBeenCalledWith('test-key'); |
| 63 | + |
| 64 | + expect(logger.debug).not.toHaveBeenCalled(); |
| 65 | + expect(logger.info).not.toHaveBeenCalled(); |
| 66 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 67 | + expect(logger.error).not.toHaveBeenCalled(); |
| 68 | +}); |
| 69 | + |
| 70 | +it('can handle an error getting a value', async () => { |
| 71 | + const logger = { |
| 72 | + debug: jest.fn(), |
| 73 | + info: jest.fn(), |
| 74 | + warn: jest.fn(), |
| 75 | + error: jest.fn(), |
| 76 | + }; |
| 77 | + // Storage here needs to be the global browser 'Storage' not the interface |
| 78 | + // for our platform. |
| 79 | + const spy = jest.spyOn(Storage.prototype, 'getItem'); |
| 80 | + spy.mockImplementation(() => { |
| 81 | + throw new Error('bad'); |
| 82 | + }); |
| 83 | + |
| 84 | + const storage = new LocalStorage(logger); |
| 85 | + storage.get('test-key'); |
| 86 | + |
| 87 | + expect(logger.debug).not.toHaveBeenCalled(); |
| 88 | + expect(logger.info).not.toHaveBeenCalled(); |
| 89 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 90 | + expect(logger.error).toHaveBeenCalledWith( |
| 91 | + 'Error getting key from localStorage: test-key, reason: Error: bad', |
| 92 | + ); |
| 93 | +}); |
| 94 | + |
| 95 | +it('can clear values', async () => { |
| 96 | + const logger = { |
| 97 | + debug: jest.fn(), |
| 98 | + info: jest.fn(), |
| 99 | + warn: jest.fn(), |
| 100 | + error: jest.fn(), |
| 101 | + }; |
| 102 | + // Storage here needs to be the global browser 'Storage' not the interface |
| 103 | + // for our platform. |
| 104 | + const spy = jest.spyOn(Storage.prototype, 'removeItem'); |
| 105 | + |
| 106 | + const storage = new LocalStorage(logger); |
| 107 | + storage.clear('test-key'); |
| 108 | + expect(spy).toHaveBeenCalledWith('test-key'); |
| 109 | + |
| 110 | + expect(logger.debug).not.toHaveBeenCalled(); |
| 111 | + expect(logger.info).not.toHaveBeenCalled(); |
| 112 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 113 | + expect(logger.error).not.toHaveBeenCalled(); |
| 114 | +}); |
| 115 | + |
| 116 | +it('can handle an error clearing a value', async () => { |
| 117 | + const logger = { |
| 118 | + debug: jest.fn(), |
| 119 | + info: jest.fn(), |
| 120 | + warn: jest.fn(), |
| 121 | + error: jest.fn(), |
| 122 | + }; |
| 123 | + // Storage here needs to be the global browser 'Storage' not the interface |
| 124 | + // for our platform. |
| 125 | + const spy = jest.spyOn(Storage.prototype, 'removeItem'); |
| 126 | + spy.mockImplementation(() => { |
| 127 | + throw new Error('bad'); |
| 128 | + }); |
| 129 | + |
| 130 | + const storage = new LocalStorage(logger); |
| 131 | + storage.clear('test-key'); |
| 132 | + |
| 133 | + expect(logger.debug).not.toHaveBeenCalled(); |
| 134 | + expect(logger.info).not.toHaveBeenCalled(); |
| 135 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 136 | + expect(logger.error).toHaveBeenCalledWith( |
| 137 | + 'Error clearing key from localStorage: test-key, reason: Error: bad', |
| 138 | + ); |
| 139 | +}); |
0 commit comments