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 withrespond(reply)orfail(error)) and delivers server events (live.pushEvent(event, payload)), withdeferred()andnextTurn()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, andcreateFakeTabs()(locksandBroadcastChannel) for stores or bindings that play several tabs.profile.openTab()(on acreateFakeTabs({ 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'sstorage,locksandBroadcastChannelunless itspersistnames its own (nullincluded). 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 oflive_sliceitself, the test's own tab. Calltab.resetStores()after the test.
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 announcedimport * 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.