|
| 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