|
| 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('canAdapt', () => { |
| 10 | + it('returns true if command execution is supported', () => { |
| 11 | + const result = CommandExecutionAdapter.canAdapt({ |
| 12 | + executeCommandProvider: {commands: []}, |
| 13 | + }); |
| 14 | + expect(result).to.be.true; |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns false it no formatting supported', () => { |
| 18 | + const result = CommandExecutionAdapter.canAdapt({}); |
| 19 | + expect(result).to.be.false; |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('executeCommand', () => { |
| 24 | + it('invokes an executeCommand object from given inputs', async () => { |
| 25 | + const connection = createSpyConnection(); |
| 26 | + const languageClient = new ls.LanguageClientConnection(connection); |
| 27 | + const testCommand = { |
| 28 | + command: 'testCommand', |
| 29 | + arguments: ['a', 'b'], |
| 30 | + }; |
| 31 | + sinon.stub(languageClient, 'executeCommand').returns(Promise.resolve(testCommand)); |
| 32 | + |
| 33 | + const result = await CommandExecutionAdapter.executeCommand( |
| 34 | + languageClient, |
| 35 | + testCommand.command, |
| 36 | + testCommand.arguments |
| 37 | + ); |
| 38 | + |
| 39 | + expect(result.command).to.equal(testCommand.command); |
| 40 | + expect(result.arguments).to.equal(testCommand.arguments); |
| 41 | + |
| 42 | + expect((languageClient as any).executeCommand.called).to.be.true; |
| 43 | + expect((languageClient as any).executeCommand.getCalls()[0].args).to.deep.equal([{ |
| 44 | + command: testCommand.command, |
| 45 | + arguments: testCommand.arguments |
| 46 | + } as ExecuteCommandParams]); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('registerCustomCallbackForCommand', () => { |
| 51 | + it('registers a custom callback for a command, to be executed on executeCommand', async () => { |
| 52 | + const connection = createSpyConnection(); |
| 53 | + const languageClient = new ls.LanguageClientConnection(connection); |
| 54 | + const testCallback: CommandCustomCallbackFunction = (command: ExecuteCommandParams) => Promise.resolve(command.command); |
| 55 | + const testCommand = { |
| 56 | + command: 'testCommand', |
| 57 | + arguments: ['a', 'b'], |
| 58 | + }; |
| 59 | + |
| 60 | + const spiedCallback = sinon.spy(testCallback); |
| 61 | + sinon.spy(languageClient, 'executeCommand'); |
| 62 | + |
| 63 | + CommandExecutionAdapter.registerCustomCallbackForCommand(testCommand.command, spiedCallback); |
| 64 | + |
| 65 | + const result = await CommandExecutionAdapter.executeCommand( |
| 66 | + languageClient, |
| 67 | + testCommand.command, |
| 68 | + testCommand.arguments |
| 69 | + ); |
| 70 | + |
| 71 | + expect(spiedCallback.called).to.be.true; |
| 72 | + |
| 73 | + expect((languageClient as any).executeCommand.called).to.be.false; |
| 74 | + |
| 75 | + expect(result).to.equal(testCommand.command); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments