Skip to content

Commit 4b54305

Browse files
deankevorkianaminya
authored andcommitted
test: addede tests for CommandExecutionAdapter
1 parent fc6e182 commit 4b54305

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

lib/adapters/command-execution-adapter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Command, ExecuteCommandParams } from "../languageclient";
1+
import { ExecuteCommandParams } from "../languageclient";
22
import { LanguageClientConnection } from "../main";
33

4-
export type CommandCustomCallbackFunction = (command: ExecuteCommandParams) => Promise<void>;
4+
export type CommandCustomCallbackFunction = (command: ExecuteCommandParams) => Promise<any | void>;
55

66
export default class CommandExecutionAdapter {
77
private static commandsCustomCallbacks: Map<string, CommandCustomCallbackFunction> = new Map<string, CommandCustomCallbackFunction>();
@@ -17,7 +17,7 @@ export default class CommandExecutionAdapter {
1717
return commandCustomCallback != null ? await commandCustomCallback(executeCommandParams) : await connection.executeCommand(executeCommandParams);
1818
}
1919

20-
public static createExecuteCommandParams(command: string, commandArgs?: any[] | undefined): ExecuteCommandParams {
20+
private static createExecuteCommandParams(command: string, commandArgs?: any[] | undefined): ExecuteCommandParams {
2121
return {
2222
command: command,
2323
arguments: commandArgs
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { expect } from 'chai';
2+
import * as sinon from 'sinon';
3+
import * as ls from '../../lib/languageclient';
4+
import CommandExecutionAdapter, { CommandCustomCallbackFunction } from '../../lib/adapters/command-execution-adapter';
5+
import { createSpyConnection } from '../helpers.js';
6+
import { ExecuteCommandParams } from '../../lib/languageclient';
7+
8+
describe('CommandExecutionAdapter', () => {
9+
describe('executeCommand', () => {
10+
it('invokes an executeCommand object from given inputs', async () => {
11+
const connection = createSpyConnection();
12+
const languageClient = new ls.LanguageClientConnection(connection);
13+
const testCommand = {
14+
command: 'testCommand',
15+
arguments: ['a', 'b'],
16+
};
17+
sinon.stub(languageClient, 'executeCommand').returns(Promise.resolve(testCommand));
18+
19+
const result = await CommandExecutionAdapter.executeCommand(
20+
languageClient,
21+
testCommand.command,
22+
testCommand.arguments
23+
);
24+
25+
expect(result.command).to.equal(testCommand.command);
26+
expect(result.arguments).to.equal(testCommand.arguments);
27+
28+
expect((languageClient as any).executeCommand.called).to.be.true;
29+
expect((languageClient as any).executeCommand.getCalls()[0].args).to.deep.equal([{
30+
command: testCommand.command,
31+
arguments: testCommand.arguments
32+
} as ExecuteCommandParams]);
33+
});
34+
});
35+
36+
describe('registerCustomCallbackForCommand', () => {
37+
it('registers a custom callback for a command, to be executed on executeCommand', async () => {
38+
const connection = createSpyConnection();
39+
const languageClient = new ls.LanguageClientConnection(connection);
40+
const testCallback: CommandCustomCallbackFunction = (command: ExecuteCommandParams) => Promise.resolve(command.command);
41+
const testCommand = {
42+
command: 'testCommand',
43+
arguments: ['a', 'b'],
44+
};
45+
46+
const spiedCallback = sinon.spy(testCallback);
47+
sinon.spy(languageClient, 'executeCommand');
48+
49+
CommandExecutionAdapter.registerCustomCallbackForCommand(testCommand.command, spiedCallback);
50+
51+
const result = await CommandExecutionAdapter.executeCommand(
52+
languageClient,
53+
testCommand.command,
54+
testCommand.arguments
55+
);
56+
57+
expect(spiedCallback.called).to.be.true;
58+
59+
expect((languageClient as any).executeCommand.called).to.be.false;
60+
61+
expect(result).to.equal(testCommand.command);
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)