Skip to content

Commit 745d682

Browse files
authored
Improve e2e tests to check for a "ready" event emitted by the UI (#117)
Fixes: #116 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent b6aa98f commit 745d682

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

protocol/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface PanelState {
4949
noFileSelected?: boolean;
5050
}
5151

52-
export type WebviewCommand = 'goToPosition' | 'formatSchema' | 'openExternal';
52+
export type WebviewCommand = 'goToPosition' | 'formatSchema' | 'openExternal' | 'ready';
5353

5454
export interface WebviewToExtensionMessage {
5555
command: WebviewCommand;

test/vscode/extension.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ suite('Extension Test Suite', () => {
5252

5353
await new Promise(resolve => setTimeout(resolve, 1000));
5454

55-
assert.ok(true, 'Command executed without error');
55+
const ready = await vscode.commands.executeCommand('sourcemeta-studio.isWebviewReady') as boolean;
56+
assert.ok(ready);
5657
});
5758

5859
test('Should handle JSON file opening', async function() {
@@ -168,8 +169,7 @@ suite('Extension Test Suite', () => {
168169
assert.ok(diagnostics.length > 0);
169170

170171
const hasLintDiagnostic = diagnostics.some(diagnostic =>
171-
diagnostic.message.includes('description') ||
172-
diagnostic.source === 'Sourcemeta Lint');
172+
diagnostic.source === 'Sourcemeta Studio (Lint)');
173173

174174
assert.ok(hasLintDiagnostic);
175175
});

vscode/src/extension.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let lastActiveTextEditor: vscode.TextEditor | undefined;
1515
let cachedCliVersion = 'Loading...';
1616
let extensionVersion = 'Loading...';
1717
let currentPanelState: PanelState | null = null;
18+
let webviewReady = false;
1819

1920
/**
2021
* Extension activation
@@ -47,10 +48,15 @@ export function activate(context: vscode.ExtensionContext): void {
4748
});
4849

4950
const openPanelCommand = vscode.commands.registerCommand('sourcemeta-studio.openPanel', () => {
51+
webviewReady = false;
5052
panelManager.createOrReveal(context);
5153
updatePanelContent();
5254
});
5355

56+
const isWebviewReadyCommand = vscode.commands.registerCommand('sourcemeta-studio.isWebviewReady', () => {
57+
return webviewReady;
58+
});
59+
5460
const activeEditorChangeListener = vscode.window.onDidChangeActiveTextEditor((editor) => {
5561
handleActiveEditorChange(editor);
5662
});
@@ -65,6 +71,7 @@ export function activate(context: vscode.ExtensionContext): void {
6571

6672
context.subscriptions.push(
6773
openPanelCommand,
74+
isWebviewReadyCommand,
6875
activeEditorChangeListener,
6976
documentSaveListener
7077
);
@@ -74,6 +81,12 @@ export function activate(context: vscode.ExtensionContext): void {
7481
* Handle messages from the webview
7582
*/
7683
function handleWebviewMessage(message: WebviewToExtensionMessage): void {
84+
if (message.command === 'ready') {
85+
webviewReady = true;
86+
console.log('[Sourcemeta Studio] Webview ready');
87+
return;
88+
}
89+
7790
if (message.command === 'goToPosition' && lastActiveTextEditor && message.position) {
7891
const range = errorPositionToRange(message.position);
7992

webview/src/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, useEffect } from 'react';
22
import type { PanelState, TabType } from '../../protocol/types';
3-
import { getActiveTab, setActiveTab as setActiveTabInState } from './message';
3+
import { getActiveTab, setActiveTab as setActiveTabInState, notifyReady } from './message';
44
import { FileInfo } from './components/FileInfo';
55
import { HealthBar } from './components/HealthBar';
66
import { Tabs } from './components/Tabs';
@@ -30,6 +30,9 @@ function App() {
3030

3131
window.addEventListener('message', handleMessage);
3232

33+
// Notify the extension that the webview is ready
34+
notifyReady();
35+
3336
return () => {
3437
window.removeEventListener('message', handleMessage);
3538
};

webview/src/message.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ export function getActiveTab(): TabType | undefined {
3838
export function setActiveTab(tab: TabType): void {
3939
vsCodeApi.setState({ activeTab: tab } satisfies WebviewState);
4040
}
41+
42+
export function notifyReady(): void {
43+
postMessage({ command: 'ready' });
44+
}

0 commit comments

Comments
 (0)