Skip to content

Commit

Permalink
Merge pull request #37 from projectatomic/tests/message-proxy
Browse files Browse the repository at this point in the history
tests: adding tests for MessageProxy
axel7083 authored Jan 15, 2024
2 parents aaf290f + cfc72af commit 280c18b
Showing 4 changed files with 99 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/backend/src/studio.spec.ts
Original file line number Diff line number Diff line change
@@ -22,7 +22,11 @@ import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import { Studio } from './studio';
import type { ExtensionContext } from '@podman-desktop/api';

const mockedExtensionContext = {} as unknown as ExtensionContext;
const mockedExtensionContext = {
subscriptions: {
push: vi.fn()
}
} as unknown as ExtensionContext;

const studio = new Studio(mockedExtensionContext);

7 changes: 7 additions & 0 deletions packages/backend/vitest.config.js
Original file line number Diff line number Diff line change
@@ -17,13 +17,20 @@
***********************************************************************/

import path from 'node:path';
import { join } from 'path';

const PACKAGE_ROOT = __dirname;

const config = {
test: {
include: ['**/*.{test,spec}.?(c|m)[jt]s?(x)', '../shared/**/*.{test,spec}.?(c|m)[jt]s?(x)']
},
resolve: {
alias: {
'@podman-desktop/api': path.resolve(__dirname, '__mocks__/@podman-desktop/api.js'),
'/@/': join(PACKAGE_ROOT, 'src') + '/',
'/@gen/': join(PACKAGE_ROOT, 'src-generated') + '/',
'@shared/': join(PACKAGE_ROOT, '../shared') + '/',
},
},
};
85 changes: 85 additions & 0 deletions packages/shared/MessageProxy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { test, expect, beforeAll } from 'vitest';
import { RpcBrowser, RpcExtension } from './MessageProxy';
import { PodmanDesktopApi } from '../../types/podman-desktop-api';
import type { Webview } from '@podman-desktop/api';

let webview: Webview;
let window: Window;
let api: PodmanDesktopApi;

beforeAll(() => {

let windowListener: (message: any) => void;
let webviewListener: (message: any) => void;

webview = {
onDidReceiveMessage: (listener: (message: any) => void) => {
webviewListener = listener;
},
postMessage: async (message: any): Promise<void> => {
windowListener({data: message} as MessageEvent);
}
} as unknown as Webview;

window = {
addEventListener: (channel: string, listener: (message: any) => void) => {
expect(channel).toBe('message');
windowListener = listener;
},
} as unknown as Window;

api = {
postMessage: (message: any) => {
webviewListener(message);
},
} as unknown as PodmanDesktopApi;
})

test('Test register channel no argument', async () => {
const rpcExtension = new RpcExtension(webview);
const rpcBrowser = new RpcBrowser(window, api);

rpcExtension.register('ping', () => {
return 'pong';
});

expect(await rpcBrowser.invoke('ping')).toBe('pong');
});

test('Test register channel one argument', async () => {
const rpcExtension = new RpcExtension(webview);
const rpcBrowser = new RpcBrowser(window, api);

rpcExtension.register('double', (value: number) => {
return value*2;
});

expect(await rpcBrowser.invoke('double', 4)).toBe(8);
});

test('Test register channel multiple arguments', async () => {
const rpcExtension = new RpcExtension(webview);
const rpcBrowser = new RpcBrowser(window, api);

rpcExtension.register('sum', (...args: number[]) => {
return args.reduce((prev, current) => prev+current, 0)
});

expect(await rpcBrowser.invoke('sum', 1, 2, 3, 4, 5)).toBe(15);
});

test('Test register instance with async', async () => {
class Dummy {
async ping(): Promise<string> {
return 'pong';
}
}

const rpcExtension = new RpcExtension(webview);
const rpcBrowser = new RpcBrowser(window, api);

rpcExtension.registerInstance(Dummy, new Dummy());

const proxy = rpcBrowser.getProxy<Dummy>();
expect(await proxy.ping()).toBe('pong');
});
4 changes: 2 additions & 2 deletions packages/shared/MessageProxy.ts
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ export class RpcExtension {

if(!this.methods.has(message.channel)) {
console.error(`Trying to call on an unknown channel ${message.channel}. Available: ${Array.from(this.methods.keys())}`);
return;
throw new Error('channel does not exist.');
}

try {
@@ -149,7 +149,7 @@ export class RpcBrowser {
return;
reject(new Error('Timeout'));
this.promises.delete(requestId);
}, 10000000);
}, 5000);

// Create a Promise
return new Promise((resolve, reject) => {

0 comments on commit 280c18b

Please sign in to comment.