flyde-vscode
Version:
Flyde is an open-source, visual programming language. It runs in the IDE, integrates with existing TypeScript code, both browser and Node.js.
44 lines (39 loc) • 1.17 kB
text/typescript
import { eventually } from "@flyde/core";
import * as vscode from "vscode";
import { getLastWebviewForTests } from "../flydeEditor";
import * as assert from "assert";
async function getLastWebview(): Promise<vscode.Webview> {
await eventually(() => assert(!!getLastWebviewForTests()));
return getLastWebviewForTests()!;
}
export type WebViewTestingCommands = {
$$: {
params: { selector: string };
response: { innerHTML: string; textContent: string }[];
};
click: { params: { selector: string }; response: void };
};
export async function webviewTestingCommand<
T extends keyof WebViewTestingCommands
>(
command: T,
params: WebViewTestingCommands[T]["params"]
): Promise<WebViewTestingCommands[T]["response"]> {
const webview = await getLastWebview();
return new Promise((res, rej) => {
webview.onDidReceiveMessage((message) => {
if (message.type === "__webviewTestingResponse") {
if (message.error) {
rej(message.error);
} else {
res(message.response);
}
}
});
webview.postMessage({
type: "__webviewTestingCommand",
command,
params,
});
});
}