Add JS tests
This commit is contained in:
parent
8004ac3742
commit
1bc6117045
3 changed files with 89 additions and 4 deletions
70
reconcile-js/src/index.test.ts
Normal file
70
reconcile-js/src/index.test.ts
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
import { init, reconcile, reconcileWithHistory } from "./index";
|
||||||
|
import * as fs from "fs";
|
||||||
|
|
||||||
|
describe("reconcile", () => {
|
||||||
|
it("tries calling functions without init", () => {
|
||||||
|
expect(() => reconcile("Hello", "Hello world", "Hi world")).toThrow(
|
||||||
|
/call init()/
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(() =>
|
||||||
|
reconcileWithHistory("Hello", "Hello world", "Hi world")
|
||||||
|
).toThrow(/call init()/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("call reconcile without cursors", async () => {
|
||||||
|
await initWasm();
|
||||||
|
|
||||||
|
expect(reconcile("Hello", "Hello world", "Hi world").text).toEqual(
|
||||||
|
"Hi world"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("call reconcile with cursors", async () => {
|
||||||
|
await initWasm();
|
||||||
|
|
||||||
|
const result = reconcile(
|
||||||
|
"Hello",
|
||||||
|
{
|
||||||
|
text: "Hello world",
|
||||||
|
cursors: [
|
||||||
|
{
|
||||||
|
id: 3,
|
||||||
|
position: 2,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "Hi world",
|
||||||
|
cursors: [
|
||||||
|
{
|
||||||
|
id: 4,
|
||||||
|
position: 0,
|
||||||
|
},
|
||||||
|
{ id: 5, position: 3 },
|
||||||
|
],
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.text).toEqual("Hi world");
|
||||||
|
expect(result.cursors).toEqual([
|
||||||
|
{ id: 3, position: 0 },
|
||||||
|
{ id: 4, position: 0 },
|
||||||
|
{ id: 5, position: 3 },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("call reconcileWithHistory", async () => {
|
||||||
|
await initWasm();
|
||||||
|
|
||||||
|
const result = reconcileWithHistory("Hello", "Hello world", "Hi world");
|
||||||
|
|
||||||
|
expect(result.text).toEqual("Hi world");
|
||||||
|
expect(result.history.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
async function initWasm() {
|
||||||
|
const wasmBin = fs.readFileSync("../pkg/reconcile_bg.wasm");
|
||||||
|
await init({ module_or_path: wasmBin });
|
||||||
|
}
|
||||||
|
|
@ -49,6 +49,9 @@ export type Tokenizer = "word" | "character";
|
||||||
|
|
||||||
let isInitialised = false;
|
let isInitialised = false;
|
||||||
|
|
||||||
|
const UNINITIALISED_MODULE_ERROR =
|
||||||
|
"Reconcile module has not been initialized. Please call init() before using any other functions.";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the WASM module for text reconciliation.
|
* Initializes the WASM module for text reconciliation.
|
||||||
* Must be called before using any other functions.
|
* Must be called before using any other functions.
|
||||||
|
|
@ -83,6 +86,10 @@ export function reconcile(
|
||||||
right: string | TextWithCursors,
|
right: string | TextWithCursors,
|
||||||
tokenizer: BuiltinTokenizer = "Word"
|
tokenizer: BuiltinTokenizer = "Word"
|
||||||
): TextWithCursors {
|
): TextWithCursors {
|
||||||
|
if (!isInitialised) {
|
||||||
|
throw new Error(UNINITIALISED_MODULE_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
const leftCursor = toWasmTextWithCursors(left);
|
const leftCursor = toWasmTextWithCursors(left);
|
||||||
const rightCursor = toWasmTextWithCursors(right);
|
const rightCursor = toWasmTextWithCursors(right);
|
||||||
|
|
||||||
|
|
@ -114,6 +121,10 @@ export function reconcileWithHistory(
|
||||||
right: string | TextWithCursors,
|
right: string | TextWithCursors,
|
||||||
tokenizer: BuiltinTokenizer = "Word"
|
tokenizer: BuiltinTokenizer = "Word"
|
||||||
): TextWithCursorsAndHistory {
|
): TextWithCursorsAndHistory {
|
||||||
|
if (!isInitialised) {
|
||||||
|
throw new Error(UNINITIALISED_MODULE_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
const leftCursor = toWasmTextWithCursors(left);
|
const leftCursor = toWasmTextWithCursors(left);
|
||||||
const rightCursor = toWasmTextWithCursors(right);
|
const rightCursor = toWasmTextWithCursors(right);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,14 @@
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
wasm-pack build --target web --features wasm
|
wasm-pack build --target web --features wasm,wee_alloc
|
||||||
cargo test --verbose
|
cargo test --verbose
|
||||||
cargo test --features serde
|
cargo test --features serde
|
||||||
cargo test --features wasm
|
cargo test --features wasm,wee_alloc
|
||||||
wasm-pack test --node --features wasm
|
wasm-pack test --node --features wasm,wee_alloc
|
||||||
|
|
||||||
|
cd reconcile-js
|
||||||
|
npm run test
|
||||||
|
cd -
|
||||||
|
|
||||||
echo "Success!"
|
echo "Success!"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue