Skip to content

Commit

Permalink
update mocks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispader committed Apr 9, 2024
1 parent 2db6236 commit a257dc2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
21 changes: 20 additions & 1 deletion jestSetup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
jest.mock('idb-keyval', () => {
const idbKeyValMockBase = require('./node_modules/idb-keyval/dist/mock');

Check failure on line 2 in jestSetup.js

View workflow job for this annotation

GitHub Actions / lint

Missing file extension for "./node_modules/idb-keyval/dist/mock"

return {
clear: jest.fn(idbKeyValMockBase.clear),
createStore: jest.fn(idbKeyValMockBase.createStore),
del: jest.fn(idbKeyValMockBase.del),
delMany: jest.fn(idbKeyValMockBase.delMany),
entries: jest.fn(idbKeyValMockBase.entries),
get: jest.fn(idbKeyValMockBase.get),
getMany: jest.fn(idbKeyValMockBase.getMany),
keys: jest.fn(idbKeyValMockBase.keys),
promisifyRequest: jest.fn(idbKeyValMockBase.promisifyRequest),
set: jest.fn(idbKeyValMockBase.set),
setMany: jest.fn(idbKeyValMockBase.setMany),
update: jest.fn(idbKeyValMockBase.update),
values: jest.fn(idbKeyValMockBase.values),
};
});

jest.mock('./lib/storage');
jest.mock('./lib/storage/providers/IDBKeyValProvider', () => require('./lib/storage/providers/__mocks__/IDBKeyValProvider'));
jest.mock('./lib/storage/platforms/index.native', () => require('./lib/storage/__mocks__'));
jest.mock('idb-keyval', () => require('./node_modules/idb-keyval/dist/mock'));

jest.mock('react-native-device-info', () => ({getFreeDiskStorage: () => {}}));
jest.mock('react-native-quick-sqlite', () => ({
Expand Down
5 changes: 2 additions & 3 deletions lib/storage/providers/__mocks__/IDBKeyValProvider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import _ from 'underscore';
import idbKeyVal from 'idb-keyval';
import type {StorageProvider, MockStorageProvider} from '../types';

const idbKeyVal = jest.requireActual('idb-keyval');

const IDBKeyValProvider: StorageProvider = jest.requireActual('../IDBKeyValProvider').default;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {name: _name, ...IDBKeyValProviderFunctions} = IDBKeyValProvider;
Expand All @@ -18,7 +17,7 @@ const IDBKeyValProviderMockBase = _.reduce<typeof IDBKeyValProviderFunctions, Pa

const IDBKeyValProviderMock = {
...IDBKeyValProviderMockBase,
idbKeyvalSet: jest.fn(idbKeyVal.set),
idbKeyval: idbKeyVal,
};

export default IDBKeyValProviderMock;
3 changes: 1 addition & 2 deletions tests/unit/storage/providers/IDBKeyvalProviderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ describe('storage/providers/IDBKeyVal', () => {

// When they are saved
return IDBKeyValProviderMock.multiSet(pairs).then(() => {
// We expect a call to idbKeyval.setItem for each pair
_.each(pairs, ([key, value]) => expect(IDBKeyValProviderMock.idbKeyvalSet).toHaveBeenCalledWith(key, value));
expect(IDBKeyValProviderMock.idbKeyval.setMany).toHaveBeenCalledTimes(1);
});
});

Expand Down

0 comments on commit a257dc2

Please sign in to comment.