2019-01-12 02:33:44 +07:00
|
|
|
import { createClient } from "./helpers";
|
|
|
|
|
|
|
|
describe("Evaluate", () => {
|
|
|
|
const client = createClient();
|
|
|
|
|
|
|
|
it("should transfer string", async () => {
|
2019-02-19 23:42:32 +07:00
|
|
|
const value = await client.evaluate(() => {
|
2019-01-12 02:33:44 +07:00
|
|
|
return "hi";
|
|
|
|
});
|
2019-02-06 07:08:48 +07:00
|
|
|
|
2019-01-12 02:33:44 +07:00
|
|
|
expect(value).toEqual("hi");
|
|
|
|
}, 100);
|
|
|
|
|
2019-01-12 03:08:57 +07:00
|
|
|
it("should compute from string", async () => {
|
|
|
|
const start = "ban\%\$\"``a,,,,asdasd";
|
|
|
|
const value = await client.evaluate((a) => {
|
|
|
|
return a;
|
|
|
|
}, start);
|
|
|
|
|
|
|
|
expect(value).toEqual(start);
|
|
|
|
}, 100);
|
|
|
|
|
2019-01-12 02:33:44 +07:00
|
|
|
it("should compute from object", async () => {
|
|
|
|
const value = await client.evaluate((arg) => {
|
|
|
|
return arg.bananas * 2;
|
|
|
|
}, { bananas: 1 });
|
2019-02-06 07:08:48 +07:00
|
|
|
|
2019-01-12 02:33:44 +07:00
|
|
|
expect(value).toEqual(2);
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
it("should transfer object", async () => {
|
|
|
|
const value = await client.evaluate(() => {
|
|
|
|
return { alpha: "beta" };
|
|
|
|
});
|
2019-02-06 07:08:48 +07:00
|
|
|
|
2019-01-12 02:33:44 +07:00
|
|
|
expect(value.alpha).toEqual("beta");
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
it("should require", async () => {
|
|
|
|
const value = await client.evaluate(() => {
|
|
|
|
const fs = require("fs") as typeof import("fs");
|
2019-02-06 07:08:48 +07:00
|
|
|
|
2019-01-12 02:33:44 +07:00
|
|
|
return Object.keys(fs).filter((f) => f === "readFileSync");
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(value[0]).toEqual("readFileSync");
|
|
|
|
}, 100);
|
2019-01-12 02:46:00 +07:00
|
|
|
|
|
|
|
it("should resolve with promise", async () => {
|
|
|
|
const value = await client.evaluate(async () => {
|
2019-02-19 23:17:03 +07:00
|
|
|
await new Promise((r): number => setTimeout(r, 100));
|
2019-01-12 02:46:00 +07:00
|
|
|
|
|
|
|
return "donkey";
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(value).toEqual("donkey");
|
|
|
|
}, 250);
|
2019-01-30 07:48:02 +07:00
|
|
|
|
|
|
|
it("should do active process", (done) => {
|
|
|
|
const runner = client.run((ae) => {
|
2019-02-19 23:42:32 +07:00
|
|
|
ae.on("first", () => {
|
|
|
|
ae.emit("first:response");
|
|
|
|
ae.on("second", () => ae.emit("second:response"));
|
2019-01-30 07:48:02 +07:00
|
|
|
});
|
2019-02-19 23:17:03 +07:00
|
|
|
|
2019-02-19 23:42:32 +07:00
|
|
|
const disposeCallbacks = <Array<() => void>>[];
|
|
|
|
const dispose = (): void => {
|
|
|
|
disposeCallbacks.forEach((cb) => cb());
|
|
|
|
ae.emit("disposed");
|
|
|
|
};
|
|
|
|
|
2019-02-19 23:17:03 +07:00
|
|
|
return {
|
2019-02-19 23:42:32 +07:00
|
|
|
onDidDispose: (cb: () => void): number => disposeCallbacks.push(cb),
|
|
|
|
dispose,
|
2019-02-19 23:17:03 +07:00
|
|
|
};
|
2019-01-30 07:48:02 +07:00
|
|
|
});
|
2019-02-19 23:42:32 +07:00
|
|
|
|
|
|
|
runner.emit("first");
|
|
|
|
runner.on("first:response", () => runner.emit("second"));
|
|
|
|
runner.on("second:response", () => client.dispose());
|
|
|
|
|
|
|
|
runner.on("disposed", () => done());
|
2019-01-30 07:48:02 +07:00
|
|
|
});
|
2019-02-06 07:08:48 +07:00
|
|
|
});
|