Current section
Files
Jump to
Current section
Files
guides/testing.md
# Testing an application
`live_slice/testing` exports what the library's own tests use, so an application tests its stores, bridges
and collaborative bindings in Node, without a browser or a Phoenix server:
- `FakeLiveView`: mounts hooks (`live.mount(hook, el)`), records their pushes (`live.pushed`, each answered with
`respond(reply)` or `fail(error)`) and delivers server events (`live.pushEvent(event, payload)`), with
`deferred()` and `nextTurn()` to sequence replies;
- `createReferenceServer(Y)`: the collaborative key protocol, with generations, `delete(name, meta)`,
background delivery (`attach(live, { deliverable })`) and refusals on demand (`client.refuse`);
- `createFakeIndexedDB()` for the storages and the chunk cache, and `createFakeTabs()` (`locks` and
`BroadcastChannel`) for stores or bindings that play several tabs.
- `profile.openTab()` (on a `createFakeTabs({ storage })`, a memory storage by default) for a second tab of the
same browser profile: it resolves with a store registry of its own (`createStore`, `ensureStore`,
`getStore`, `removeStore`, `resetStores`), so the same store name can be created in each tab, as each tab of a
browser runs its own copy of the page. A persisted store created through a tab uses the profile's `storage`,
`locks` and `BroadcastChannel` unless its `persist` names its own (`null` included). The registry is a
separate instance of live_slice's store module (Node's ES modules load it again under another URL); bridges
and collaborative keys still look stores up in the registry of `live_slice` itself, the test's own tab.
Call `tab.resetStores()` after the test.
```js
import { createFakeTabs } from "live_slice/testing";
const profile = createFakeTabs();
const [a, b] = [await profile.openTab(), await profile.openTab()];
const storeA = a.createStore("settings", { state: { theme: "light" }, persist: { keys: ["theme"] } });
const storeB = b.createStore("settings", { state: { theme: "light" }, persist: { keys: ["theme"] } });
storeA.setState({ theme: "dark" }); // storeB shows it once written and announced
```
```js
import * as Y from "yjs";
import { JSDOM } from "jsdom";
import { createLiveSliceHook, createStore } from "live_slice";
import { createCollaborativeKey, createMemoryDocumentStorage } from "live_slice/crdt";
import { FakeLiveView, createReferenceServer, nextTurn } from "live_slice/testing";
const { window } = new JSDOM(`<div id="notes-store" data-store="notes" data-channels='{"crdt":["body"]}'></div>`);
const server = createReferenceServer(Y);
const live = new FakeLiveView();
server.attach(live);
const store = createStore("notes", { state: { body: "" } });
live.mount(createLiveSliceHook(), window.document.getElementById("notes-store"));
const notes = createCollaborativeKey({ Y, store, key: "body", doc: "notes:1", type: "text",
persistence: createMemoryDocumentStorage(), BroadcastChannel: null, batchMs: 0 });
await notes.synced;
notes.update((text) => text.insert(0, "Hello"));
await nextTurn();
server.docFor("notes:1").getText("root").toString(); // "Hello"
```
It is a separate entry: nothing else in live_slice imports it, so it stays out of production bundles. It needs
Node (the reference server uses `Buffer`, `nextTurn` uses `setImmediate`); the DOM (jsdom here) is the
application's.