|
6 | 6 | * LICENSE file in the root directory of this source tree. An additional grant |
7 | 7 | * of patent rights can be found in the PATENTS file in the same directory. |
8 | 8 | */ |
| 9 | +/* global window */ |
9 | 10 |
|
10 | 11 | jest.autoMockOff(); |
11 | 12 | jest.unmock('../LocalDatastoreUtils'); |
@@ -103,10 +104,6 @@ const RNDatastoreController = require('../LocalDatastoreController.react-native' |
103 | 104 | const BrowserDatastoreController = require('../LocalDatastoreController.browser'); |
104 | 105 | const DefaultDatastoreController = require('../LocalDatastoreController.default'); |
105 | 106 |
|
106 | | -const mockLocalStorage = require('./test_helpers/mockLocalStorage'); |
107 | | - |
108 | | -global.localStorage = mockLocalStorage; |
109 | | - |
110 | 107 | const item1 = new ParseObject('Item'); |
111 | 108 | const item2 = new ParseObject('Item'); |
112 | 109 | const item3 = new ParseObject('Item'); |
@@ -657,7 +654,6 @@ describe('LocalDatastore', () => { |
657 | 654 | user._localId = null; |
658 | 655 |
|
659 | 656 | const USER_KEY = LocalDatastore.getKeyForObject(user); |
660 | | - console.log(USER_KEY); |
661 | 657 | const LDS = { |
662 | 658 | [USER_KEY]: [user._toFullJSON()], |
663 | 659 | [`${PIN_PREFIX}_testPinName`]: [USER_KEY], |
@@ -872,20 +868,30 @@ describe('LocalDatastore (BrowserDatastoreController)', () => { |
872 | 868 | expect(await LocalDatastore._getRawStorage()).toEqual({}); |
873 | 869 | }); |
874 | 870 |
|
| 871 | + it('can handle getAllContent error', async () => { |
| 872 | + await LocalDatastore.pinWithName('_default', [{ value: 'WILL_BE_MOCKED' }]); |
| 873 | + const windowSpy = jest.spyOn(Object.getPrototypeOf(window.localStorage), 'getItem') |
| 874 | + .mockImplementationOnce(() => { |
| 875 | + return '[1, ]'; |
| 876 | + }); |
| 877 | + const spy = jest.spyOn(console, 'error').mockImplementationOnce(() => {}); |
| 878 | + const LDS = await LocalDatastore._getAllContents(); |
| 879 | + expect(LDS).toEqual({}); |
| 880 | + expect(spy).toHaveBeenCalled(); |
| 881 | + spy.mockRestore(); |
| 882 | + windowSpy.mockRestore(); |
| 883 | + }); |
| 884 | + |
875 | 885 | it('can handle store error', async () => { |
876 | | - const mockStorageError = { |
877 | | - setItem() { |
| 886 | + const windowSpy = jest.spyOn(Object.getPrototypeOf(window.localStorage), 'setItem') |
| 887 | + .mockImplementationOnce(() => { |
878 | 888 | throw new Error('error thrown'); |
879 | | - }, |
880 | | - }; |
881 | | - Object.defineProperty(window, 'localStorage', { // eslint-disable-line |
882 | | - value: mockStorageError |
883 | | - }); |
884 | | - try { |
885 | | - await LocalDatastore.pinWithName('myKey', [{ name: 'test' }]); |
886 | | - } catch (e) { |
887 | | - expect(e.message).toBe('error thrown'); |
888 | | - } |
| 889 | + }); |
| 890 | + const consoleSpy = jest.spyOn(console, 'log').mockImplementationOnce(() => {}); |
| 891 | + await LocalDatastore.pinWithName('myKey', [{ name: 'test' }]); |
| 892 | + expect(consoleSpy).toHaveBeenCalled(); |
| 893 | + consoleSpy.mockRestore(); |
| 894 | + windowSpy.mockRestore(); |
889 | 895 | }); |
890 | 896 | }); |
891 | 897 |
|
|
0 commit comments