Skip to content

Commit

Permalink
fix: initialization fails in non-browser environment that doesn't sup…
Browse files Browse the repository at this point in the history
…port `indexedDB` (#1569)
  • Loading branch information
dplewis authored Oct 13, 2022
1 parent 2712e41 commit 3560a5e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/IndexedDBStorageController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* @flow
*/
/* global window */

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

try {
if (typeof window !== 'undefined' && window.indexedDB) {
const ParseStore = createStore('parseDB', 'parseStore');

const IndexedDBStorageController = {
Expand All @@ -27,6 +28,7 @@ try {
};

module.exports = IndexedDBStorageController;
} catch (e) {
} else {
// IndexedDB not supported
module.exports = undefined;
}
5 changes: 3 additions & 2 deletions src/Parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ Parse.Storage = require('./Storage');
Parse.User = require('./ParseUser').default;
Parse.LiveQuery = require('./ParseLiveQuery').default;
Parse.LiveQueryClient = require('./LiveQueryClient').default;
Parse.IndexedDB = require('./IndexedDBStorageController');

if (process.env.PARSE_BUILD === 'browser') {
Parse.IndexedDB = require('./IndexedDBStorageController');
}
Parse._request = function (...args) {
return CoreManager.getRESTController().request.apply(null, args);
};
Expand Down
21 changes: 16 additions & 5 deletions src/__tests__/Parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ jest.dontMock('../LocalDatastore');
jest.dontMock('crypto-js/aes');
jest.setMock('../EventuallyQueue', { poll: jest.fn() });

global.indexedDB = require('./test_helpers/mockIndexedDB');
const CoreManager = require('../CoreManager');
const EventuallyQueue = require('../EventuallyQueue');
const Parse = require('../Parse');

describe('Parse module', () => {
let Parse;
beforeEach(() => {
jest.isolateModules(() => {
Parse = require('../Parse');
});
});

it('can be initialized with keys', () => {
Parse.initialize('A', 'B');
expect(CoreManager.get('APPLICATION_ID')).toBe('A');
Expand Down Expand Up @@ -165,6 +172,7 @@ describe('Parse module', () => {
Parse.enableEncryptedUser();
expect(Parse.encryptedUser).toBe(true);
expect(Parse.isEncryptedUserEnabled()).toBe(true);
process.env.PARSE_BUILD = 'node';
});

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

it('can get IndexedDB storage', () => {
console.log(Parse.IndexedDB);
expect(Parse.IndexedDB).toBeDefined();
CoreManager.setStorageController(Parse.IndexedDB);
expect(Parse.IndexedDB).toBeUndefined();
process.env.PARSE_BUILD = 'browser';
const ParseInstance = require('../Parse');
expect(ParseInstance.IndexedDB).toBeDefined();
CoreManager.setStorageController(ParseInstance.IndexedDB);
const currentStorage = CoreManager.getStorageController();
expect(currentStorage).toEqual(Parse.IndexedDB);
expect(currentStorage).toEqual(ParseInstance.IndexedDB);
process.env.PARSE_BUILD = 'node';
});
});
16 changes: 13 additions & 3 deletions src/__tests__/Storage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const CoreManager = require('../CoreManager');

global.wx = mockWeChat;
global.localStorage = mockStorageInterface;
global.indexedDB = mockIndexedDB;
jest.mock('idb-keyval', () => {
return mockIndexedDB;
});
Expand Down Expand Up @@ -165,10 +166,12 @@ describe('React Native StorageController', () => {
});
});

const IndexedDBStorageController = require('../IndexedDBStorageController');

describe('React Native StorageController', () => {
describe('IndexDB StorageController', () => {
let IndexedDBStorageController;
beforeEach(() => {
jest.isolateModules(() => {
IndexedDBStorageController = require('../IndexedDBStorageController');
});
IndexedDBStorageController.clear();
});

Expand Down Expand Up @@ -203,6 +206,13 @@ describe('React Native StorageController', () => {
const keys = await IndexedDBStorageController.getAllKeysAsync();
expect(keys[0]).toBe('myKey');
});

it('handle indexedDB is not defined', async () => {
global.indexedDB = undefined;
const dbController = require('../IndexedDBStorageController');
expect(dbController).toBeUndefined();
global.indexedDB = mockIndexedDB;
});
});

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

0 comments on commit 3560a5e

Please sign in to comment.