Skip to content

Commit

Permalink
Finalizing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khawkins committed Oct 26, 2023
1 parent c382928 commit 5028e0b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 21 deletions.
81 changes: 79 additions & 2 deletions src/test/suite/webviews.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import * as assert from 'assert';
import * as sinon from 'sinon';
import { Uri, WebviewPanel, env } from 'vscode';
import { afterEach, beforeEach } from 'mocha';
import * as fs from 'fs';
import * as fs from 'node:fs';
import { mkdir, mkdtemp, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import {
WebviewMessageHandler,
WebviewProcessor
} from '../../webviews/processor';

suite('InstructionsWebviewProvider Test Suite', () => {
suite('Webview Test Suite', () => {
const extensionUri = Uri.parse('file:///tmp/testdir');

beforeEach(function () {});
Expand Down Expand Up @@ -225,4 +228,78 @@ suite('InstructionsWebviewProvider Test Suite', () => {
panel.dispose();
postMessageStub.restore();
});

test('Unique types for message handler collection passes validation', async () => {
const messageHandlers: WebviewMessageHandler[] = [
{
type: 'type1',
action: (_panel, data, callback) => {}
},
{
type: 'type2',
action: (_panel, data, callback) => {}
}
];
WebviewProcessor.validateMessageHanders(messageHandlers);
});

test('Repeated types for message handler collection fails validation', async () => {
const messageHandlers: WebviewMessageHandler[] = [
{
type: 'type1',
action: (_panel, _data) => {}
},
{
type: 'type1',
action: (_panel, _data) => {}
}
];
assert.throws(() => {
WebviewProcessor.validateMessageHanders(messageHandlers);
}, 'A collection of message handlers that has more than one instance of a given type, should fail validation.');
});

test('Get webview content with script demarcator', async () => {
const extensionUriTempDir = await mkdtemp(
join(tmpdir(), 'salesforcedx-vscode-mobile-')
);
const extensionUri = Uri.file(extensionUriTempDir);
const processor = new WebviewProcessor(extensionUri);
const webviewPanel = processor.createWebviewPanel(
'someViewType',
'someTitle'
);
const contentWithDemarcator =
'<html><body><script src="--- MESSAGING_SCRIPT_SRC ---"></script></body></html>';
const contentWithDemarcatorDereferenced = `<html><body><script src="${webviewPanel.webview.asWebviewUri(
processor.getMessagingJsPathUri()
)}"></script></body></html>`;

const contentFilename = 'contentFile.html';
const contentDirPathRelative = 'content';
const contentDirPathAbsolute = join(
extensionUriTempDir,
contentDirPathRelative
);
const contentPathRelative = join(
contentDirPathRelative,
contentFilename
);
const contentPathAbsolute = join(
contentDirPathAbsolute,
contentFilename
);
await mkdir(join(extensionUriTempDir, contentDirPathRelative));
await writeFile(contentPathAbsolute, contentWithDemarcator);

const generatedWebviewContent = processor.getWebviewContent(
webviewPanel,
contentPathRelative
);
assert.equal(
generatedWebviewContent,
contentWithDemarcatorDereferenced
);
webviewPanel.dispose();
});
});
15 changes: 1 addition & 14 deletions src/webviews/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class InstructionsWebviewProvider {
contentPath: string,
messageHandlers: WebviewMessageHandler[]
) {
this.validateMessageHanders(messageHandlers);
WebviewProcessor.validateMessageHanders(messageHandlers);
const panel = this.processor.createWebviewPanel(
INSTRUCTION_VIEW_TYPE,
title
Expand Down Expand Up @@ -65,17 +65,4 @@ export class InstructionsWebviewProvider {
]);
});
}

private validateMessageHanders(messageHandlers: WebviewMessageHandler[]) {
const handlerMap: { [type: string]: boolean } = {};
for (const handler of messageHandlers) {
if (handlerMap[handler.type] === true) {
throw new Error(
`There can be only one message handler per type. There are at least two handlers with type '${handler.type}'.`
);
} else {
handlerMap[handler.type] = true;
}
}
}
}
25 changes: 20 additions & 5 deletions src/webviews/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ export class WebviewProcessor {
return panel;
}

public static validateMessageHanders(
messageHandlers: WebviewMessageHandler[]
) {
const handlerMap: { [type: string]: boolean } = {};
for (const handler of messageHandlers) {
if (handlerMap[handler.type] === true) {
throw new Error(
`There can be only one message handler per type. There are at least two handlers with type '${handler.type}'.`
);
} else {
handlerMap[handler.type] = true;
}
}
}

public onWebviewReceivedMessage(
data: any,
panel: WebviewPanel,
Expand Down Expand Up @@ -71,11 +86,7 @@ export class WebviewProcessor {
public getWebviewContent(panel: WebviewPanel, contentPath: string): string {
const localeContentPath = this.getLocaleContentPath(contentPath);
const htmlPath = Uri.joinPath(this.extensionUri, localeContentPath);
const messagingJsPath = Uri.joinPath(
this.extensionUri,
MESSAGING_JS_PATH
);

const messagingJsPath = this.getMessagingJsPathUri();
let webviewContent = readFileSync(htmlPath.fsPath, {
encoding: 'utf-8'
});
Expand All @@ -86,6 +97,10 @@ export class WebviewProcessor {
return webviewContent;
}

public getMessagingJsPathUri(): Uri {
return Uri.joinPath(this.extensionUri, MESSAGING_JS_PATH);
}

/**
* Check to see if a locale-specific file exists, otherwise return the default.
* @param contentPath The relative path (and filename) of the content to display.
Expand Down

0 comments on commit 5028e0b

Please sign in to comment.