loro/crates/loro-wasm/tests/counter.test.ts
Zixuan Chen 62a3a93552
Refactor: merge two js packages (#532)
* feat: make vitest tests pass

* chore: update readme & add deno test for web bundle

* chore: bump version to 1.0.8-alpha.0

* chore: bump loro-crdt version

* fix: build script
export init method from loro-wasm/web

* chore: bump version

* chore: specify which files to include for npm publish

* refactor: rename loro-js to loro-js-test

* refactor: remove the old loro-js folder

* fix: build scripts

* chore: 1.0.8-alpha.3

* chore: add release info
2024-10-29 21:46:56 +08:00

60 lines
1.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { CounterDiff, LoroDoc } from "../bundler/index";
function oneMs(): Promise<void> {
return new Promise((r) => setTimeout(r));
}
describe("counter", () => {
it("increment", () => {
const doc = new LoroDoc();
const counter = doc.getCounter("counter");
counter.increment(1);
counter.increment(2);
counter.decrement(1);
expect(counter.value).toBe(2);
});
it("encode", async () => {
const doc = new LoroDoc();
const counter = doc.getCounter("counter");
counter.increment(1);
counter.increment(2);
counter.decrement(4);
const updates = doc.exportFrom();
const snapshot = doc.exportSnapshot();
const json = doc.exportJsonUpdates();
const doc2 = new LoroDoc();
doc2.import(updates);
expect(doc2.toJSON()).toStrictEqual(doc.toJSON());
const doc3 = new LoroDoc();
doc3.import(snapshot);
expect(doc3.toJSON()).toStrictEqual(doc.toJSON());
const doc4 = new LoroDoc();
doc4.importJsonUpdates(json);
expect(doc4.toJSON()).toStrictEqual(doc.toJSON());
});
});
describe("counter event", () => {
it("event", async () => {
const doc = new LoroDoc();
let triggered = false;
doc.subscribe((e) => {
triggered = true;
const diff = e.events[0].diff as CounterDiff;
expect(diff.type).toBe("counter");
expect(diff.increment).toStrictEqual(-1);
});
const counter = doc.getCounter("counter");
counter.increment(1);
counter.increment(2);
counter.decrement(4);
doc.commit();
await oneMs();
expect(triggered).toBe(true);
});
});