|
| 1 | +// eslint-disable-next-line no-unused-vars |
| 2 | +import type * as lspTypes from "vscode-languageserver-protocol"; |
| 3 | +import { registerOrganizeImports } from "./organizeImports"; |
| 4 | + |
| 5 | +class MockRange { |
| 6 | + // eslint-disable-next-line no-unused-vars |
| 7 | + constructor(readonly start: number, readonly end: number) {} |
| 8 | +} |
| 9 | +(global as any).Range = MockRange; |
| 10 | + |
| 11 | +describe("organizeImports command", () => { |
| 12 | + beforeEach(() => { |
| 13 | + (global as any).nova = Object.assign(nova, { |
| 14 | + commands: { |
| 15 | + register: jest.fn(), |
| 16 | + }, |
| 17 | + workspace: { |
| 18 | + showErrorMessage(err: Error) { |
| 19 | + throw err; |
| 20 | + }, |
| 21 | + showWarningMessage: jest.fn(), |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + mockEditor.edit.mockClear(); |
| 26 | + mockEditor.scrollToCursorPosition.mockClear(); |
| 27 | + mockEditor.selectWordsContainingCursors.mockClear(); |
| 28 | + }); |
| 29 | + |
| 30 | + const mockEditor = { |
| 31 | + selectedRanges: [new Range(2, 3)], |
| 32 | + selectedText: "selectedText", |
| 33 | + document: { |
| 34 | + length: 10, |
| 35 | + uri: "currentDocURI", |
| 36 | + path: "/path", |
| 37 | + getTextInRange() { |
| 38 | + return ""; |
| 39 | + }, |
| 40 | + eol: "\n", |
| 41 | + }, |
| 42 | + edit: jest.fn(), |
| 43 | + selectWordsContainingCursors: jest.fn(), |
| 44 | + scrollToCursorPosition: jest.fn(), |
| 45 | + }; |
| 46 | + |
| 47 | + function getCommand( |
| 48 | + languageClient: LanguageClient, |
| 49 | + register: (client: LanguageClient) => Disposable |
| 50 | + ): (...args: Array<any>) => Promise<void> { |
| 51 | + register(languageClient); |
| 52 | + expect(nova.commands.register).toHaveBeenCalledWith( |
| 53 | + "apexskier.typescript.commands.organizeImports", |
| 54 | + expect.any(Function) |
| 55 | + ); |
| 56 | + const command = (nova.commands.register as jest.Mock).mock.calls[0][1]; |
| 57 | + (nova.commands.register as jest.Mock).mockClear(); |
| 58 | + return command; |
| 59 | + } |
| 60 | + |
| 61 | + it("asks the server to organize imports, then resets your selection", async () => { |
| 62 | + const mockLanguageClient = { |
| 63 | + sendRequest: jest.fn().mockImplementationOnce(() => { |
| 64 | + mockEditor.document.length = 14; |
| 65 | + }), |
| 66 | + }; |
| 67 | + const command = getCommand( |
| 68 | + (mockLanguageClient as any) as LanguageClient, |
| 69 | + registerOrganizeImports |
| 70 | + ); |
| 71 | + expect(mockEditor.selectedRanges).toEqual([new Range(2, 3)]); |
| 72 | + await command(mockEditor); |
| 73 | + |
| 74 | + expect(mockLanguageClient.sendRequest).toBeCalledTimes(1); |
| 75 | + expect(mockLanguageClient.sendRequest).toHaveBeenCalledWith( |
| 76 | + "workspace/executeCommand", |
| 77 | + { |
| 78 | + arguments: ["/path"], |
| 79 | + command: "_typescript.organizeImports", |
| 80 | + } |
| 81 | + ); |
| 82 | + expect(mockEditor.selectedRanges).toEqual([new Range(6, 7)]); |
| 83 | + expect(mockEditor.scrollToCursorPosition).toBeCalledTimes(1); |
| 84 | + }); |
| 85 | + |
| 86 | + it("doesn't reset scroll to a negative value", async () => { |
| 87 | + const mockLanguageClient = { |
| 88 | + sendRequest: jest.fn().mockImplementationOnce(() => { |
| 89 | + mockEditor.document.length = 0; |
| 90 | + }), |
| 91 | + }; |
| 92 | + const command = getCommand( |
| 93 | + (mockLanguageClient as any) as LanguageClient, |
| 94 | + registerOrganizeImports |
| 95 | + ); |
| 96 | + await command(mockEditor); |
| 97 | + expect(mockEditor.selectedRanges).toEqual([new Range(0, 0)]); |
| 98 | + expect(mockEditor.scrollToCursorPosition).toBeCalledTimes(1); |
| 99 | + }); |
| 100 | + |
| 101 | + it("warns if the document isn't saved", async () => { |
| 102 | + const mockLanguageClient = { |
| 103 | + sendRequest: jest.fn().mockReturnValueOnce(Promise.resolve(null)), |
| 104 | + }; |
| 105 | + mockEditor.document.path = ""; |
| 106 | + const command = getCommand( |
| 107 | + (mockLanguageClient as any) as LanguageClient, |
| 108 | + registerOrganizeImports |
| 109 | + ); |
| 110 | + await command(mockEditor); |
| 111 | + |
| 112 | + expect(nova.workspace.showWarningMessage).toBeCalledTimes(1); |
| 113 | + expect(nova.workspace.showWarningMessage).toHaveBeenCalledWith( |
| 114 | + "Please save this document before organizing imports." |
| 115 | + ); |
| 116 | + }); |
| 117 | +}); |
0 commit comments