MockDBProviderclass
new MockDBProvider<I, T>()
| Return | |
|---|---|
MockDBProvider<I, T> | In-memory database provider that records every operation to its calls log, for testing. |
| Property | Type | |
|---|---|---|
.calls | MockDBCall[] | The log of operations performed through this provider, in the order they happened. Defaults to [] readonly |
In-memory database provider that records every operation to its calls log, for testing.
- Extends
MemoryDBProvider, so it stores data normally, then appends aMockDBCallentry (including the result) for each call. - Assert against
callsin tests to check which operations ran and with what arguments.
A test provider that records every call. MockDBProvider extends MemoryDBProvider — so it behaves like a real in-memory database — and additionally captures every operation in a .calls array so tests can assert exactly what happened.
Usage
import { MockDBProvider } from "shelving/db";
const mock = new MockDBProvider();
await mock.addItem(POSTS, { title: "Hello", body: "", published: false });
console.log(mock.calls[0]);
// { type: "addItem", collection: "posts", data: { … }, result: <id> }Examples
const provider = new MockDBProvider();
await provider.addItem(users, { name: "Dave" });
provider.calls; // [{ type: "addItem", collection: "users", data: { name: "Dave" }, result: 123 }]