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')
onput()
andadd()
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.
// Require the mock.const IDBFactory IDBKeyRange reset = ; // Create an IDBFactory at window.indexedDB so your code can use IndexedDB.windowindexedDB = ; // Make IDBKeyRange global so your code can create key ranges.windowIDBKeyRange = IDBKeyRange; // Reset the IndexedDB mock before/after tests.// This will clear all object stores, indexes, and data.;; // The IndexedDB mock uses setTimeout() to simulate the asyncronous API.// Add fake timers before/after tests to ensure the asyncronous responses are received by the test.;; // Your tests...;
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
- Enforce constraint for unique indexes (throw
DOMException('DataError')
onput()
andadd()
etc). - Allow indexes with array (multiple) keypaths and support for
multiEntry
flag. - Add support for https://w3c.github.io/IndexedDB/#async-api features.