Add basic test for webview communication

This commit is contained in:
Matt Bierner
2018-10-04 18:13:47 -07:00
parent a529621b3d
commit 565f971f43

View File

@@ -0,0 +1,50 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'mocha';
import * as assert from 'assert';
import * as vscode from 'vscode';
const webviewId = 'myWebview';
suite('Webview tests', () => {
test('webview communication', async () => {
const webview = vscode.window.createWebviewPanel(webviewId, 'title', { viewColumn: vscode.ViewColumn.One }, { enableScripts: true });
webview.webview.html = createHtmlDocumentWithBody(/*html*/`
<script>
const vscode = acquireVsCodeApi();
window.addEventListener('message', (message) => {
vscode.postMessage({ value: message.data.value + 1 });
});
</script>`);
const p = new Promise<any>(resolve => {
webview.webview.onDidReceiveMessage(message => {
resolve(message);
});
});
webview.webview.postMessage({ value: 1 });
const response = await p;
assert.strictEqual(response.value, 2);
});
});
function createHtmlDocumentWithBody(body: string): string {
return /*html*/`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
${body}
</body>
</html>`;
}