IndexedDB API mock
Unit tested mock implementation of the browser IndexedDB API. Conforms as closely as possible to the W3C Indexed Database API (version 1.0).
Important: There are two outstanding features from the IndexedDB 1.0 spec that are not supported by this mock:
- Enforcing constraint for unique indexes (throw
DOMException('DataError')
on put()
and add()
etc). - Indexes with array (multiple) keypaths and support for the
multiEntry
flag.
Please note:
This mock supports the getAll()
method without any optional params, functionality added in the 2.0 spec, but it does not support other functionality added in the 2.0 spec, such as getKey()
, getAllKeys()
and openKeyCursor()
Installation
npm install shelving-mock-indexeddb
Usage
Example: Using IndexedDB mock for Jest tests
This example shows the IndexedDB mock in use for testing including requiring the mock, making it global (so your code can access it through window.indexedDB
, as it would in a browser), and inserting/reading records.
const { IDBFactory, IDBKeyRange, reset } = require('shelving-mock-indexeddb');
window.indexedDB = new IDBFactory();
window.IDBKeyRange = IDBKeyRange;
beforeEach(() => reset());
afterEach(() => reset());
beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.runAllTimers());
test('etc', () => {
const request = window.indexedDB.open('myDatabase', 1);
request.addEventListener('upgradeneeded', () => {
const store = request.result.createObjectStore('myStore', { keyPath: 'id', autoIncrement: true });
const index = store.createIndex('myNameIndex', 'name');
const index = store.createIndex('myLengthIndex', 'length');
});
request.addEventListener('success', () => {
const putTransaction = request.result.transaction(['myStore'], 'readwrite');
const putStore = putTransaction.objectStore('myStore');
putStore.put({ name: 'Fusilli', length: 4 });
putStore.put({ name: 'Spaghetti', length: 22 });
putStore.put({ name: 'Linguini', length: 28 });
putStore.put({ name: 'Tagliatteli', length: 22 });
const getTransaction = request.result.transaction(['myStore'], 'readonly');
const getStore = getTransaction.objectStore('myStore');
getStore.get(2).addEventListener('success', (event) => {
console.log('Found', event.target.result);
});
getStore.get(4).addEventListener('success', (event) => {
console.log('Found', event.target.result);
});
const indexTransaction = request.result.transaction(['myStore'], 'readonly');
const indexStore = indexTransaction.objectStore('myStore');
const lengthIndex = indexStore.index('myLengthIndex');
const nameIndex = indexStore.index('myNameIndex');
nameIndex.count('Fusilli').addEventListener('success', (event) => {
console.log('Found ' + event.target.result + ' pasta called Fusilli');
});
lengthIndex.count(IDBKeyRange.upperBound(20)).addEventListener('success', (event) => {
console.log('Found ' + event.target.result + ' pastas with length > 20');
});
});
});
API
IndexedDB
This mock is conformant to the W3C Indexed Database API (version 1.0). Once required and set as global (see the example above), it will work like the standard IndexedDB API.
For additional usage examples of the IndexedDB API see the Mozilla IndexedDB API documentation.
For reference, this mock provides the following objects from the IndexedDB API:
IDBFactory
IDBDatabase
IDBTransaction
IDBRequest
IDBOpenDBRequest
IDBObjectStore
IDBIndex
IDBKeyRange
IDBCursor
IDBCursorWithValue
Reset
reset()
Resets all data in the database. Clears all version, object stores, and data.
Todo