Skip to content

Commit 3560a5e

Browse files
authored
fix: initialization fails in non-browser environment that doesn't support indexedDB (#1569)
1 parent 2712e41 commit 3560a5e

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

src/IndexedDBStorageController.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/**
22
* @flow
33
*/
4+
/* global window */
45

56
import { createStore, del, set, get, clear, keys } from 'idb-keyval';
67

7-
try {
8+
if (typeof window !== 'undefined' && window.indexedDB) {
89
const ParseStore = createStore('parseDB', 'parseStore');
910

1011
const IndexedDBStorageController = {
@@ -27,6 +28,7 @@ try {
2728
};
2829

2930
module.exports = IndexedDBStorageController;
30-
} catch (e) {
31+
} else {
3132
// IndexedDB not supported
33+
module.exports = undefined;
3234
}

src/Parse.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,9 @@ Parse.Storage = require('./Storage');
246246
Parse.User = require('./ParseUser').default;
247247
Parse.LiveQuery = require('./ParseLiveQuery').default;
248248
Parse.LiveQueryClient = require('./LiveQueryClient').default;
249-
Parse.IndexedDB = require('./IndexedDBStorageController');
250-
249+
if (process.env.PARSE_BUILD === 'browser') {
250+
Parse.IndexedDB = require('./IndexedDBStorageController');
251+
}
251252
Parse._request = function (...args) {
252253
return CoreManager.getRESTController().request.apply(null, args);
253254
};

src/__tests__/Parse-test.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ jest.dontMock('../LocalDatastore');
1616
jest.dontMock('crypto-js/aes');
1717
jest.setMock('../EventuallyQueue', { poll: jest.fn() });
1818

19+
global.indexedDB = require('./test_helpers/mockIndexedDB');
1920
const CoreManager = require('../CoreManager');
2021
const EventuallyQueue = require('../EventuallyQueue');
21-
const Parse = require('../Parse');
2222

2323
describe('Parse module', () => {
24+
let Parse;
25+
beforeEach(() => {
26+
jest.isolateModules(() => {
27+
Parse = require('../Parse');
28+
});
29+
});
30+
2431
it('can be initialized with keys', () => {
2532
Parse.initialize('A', 'B');
2633
expect(CoreManager.get('APPLICATION_ID')).toBe('A');
@@ -165,6 +172,7 @@ describe('Parse module', () => {
165172
Parse.enableEncryptedUser();
166173
expect(Parse.encryptedUser).toBe(true);
167174
expect(Parse.isEncryptedUserEnabled()).toBe(true);
175+
process.env.PARSE_BUILD = 'node';
168176
});
169177

170178
it('can set an encrypt token as String', () => {
@@ -240,10 +248,13 @@ describe('Parse module', () => {
240248
});
241249

242250
it('can get IndexedDB storage', () => {
243-
console.log(Parse.IndexedDB);
244-
expect(Parse.IndexedDB).toBeDefined();
245-
CoreManager.setStorageController(Parse.IndexedDB);
251+
expect(Parse.IndexedDB).toBeUndefined();
252+
process.env.PARSE_BUILD = 'browser';
253+
const ParseInstance = require('../Parse');
254+
expect(ParseInstance.IndexedDB).toBeDefined();
255+
CoreManager.setStorageController(ParseInstance.IndexedDB);
246256
const currentStorage = CoreManager.getStorageController();
247-
expect(currentStorage).toEqual(Parse.IndexedDB);
257+
expect(currentStorage).toEqual(ParseInstance.IndexedDB);
258+
process.env.PARSE_BUILD = 'node';
248259
});
249260
});

src/__tests__/Storage-test.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const CoreManager = require('../CoreManager');
1717

1818
global.wx = mockWeChat;
1919
global.localStorage = mockStorageInterface;
20+
global.indexedDB = mockIndexedDB;
2021
jest.mock('idb-keyval', () => {
2122
return mockIndexedDB;
2223
});
@@ -165,10 +166,12 @@ describe('React Native StorageController', () => {
165166
});
166167
});
167168

168-
const IndexedDBStorageController = require('../IndexedDBStorageController');
169-
170-
describe('React Native StorageController', () => {
169+
describe('IndexDB StorageController', () => {
170+
let IndexedDBStorageController;
171171
beforeEach(() => {
172+
jest.isolateModules(() => {
173+
IndexedDBStorageController = require('../IndexedDBStorageController');
174+
});
172175
IndexedDBStorageController.clear();
173176
});
174177

@@ -203,6 +206,13 @@ describe('React Native StorageController', () => {
203206
const keys = await IndexedDBStorageController.getAllKeysAsync();
204207
expect(keys[0]).toBe('myKey');
205208
});
209+
210+
it('handle indexedDB is not defined', async () => {
211+
global.indexedDB = undefined;
212+
const dbController = require('../IndexedDBStorageController');
213+
expect(dbController).toBeUndefined();
214+
global.indexedDB = mockIndexedDB;
215+
});
206216
});
207217

208218
const DefaultStorageController = require('../StorageController.default');

0 commit comments

Comments
 (0)