|
| 1 | +import { TextEditor } from "atom" |
| 2 | +import { shell } from "electron" |
| 3 | +import * as sinon from "sinon" |
| 4 | +import { expect } from "chai" |
| 5 | +import { join, dirname } from "path" |
| 6 | +import * as ShowDocumentAdapter from "../../lib/adapters/show-document-adapter" |
| 7 | +import { LanguageClientConnection, ShowDocumentParams } from "../../lib/languageclient" |
| 8 | +import Convert from "../../lib/convert" |
| 9 | +import { createSpyConnection } from "../helpers" |
| 10 | + |
| 11 | +describe("ShowDocumentAdapter", () => { |
| 12 | + describe("can attach to a server", () => { |
| 13 | + it("subscribes to onShowDocument", async () => { |
| 14 | + const connection = createSpyConnection() |
| 15 | + const lcc = new LanguageClientConnection(connection) |
| 16 | + |
| 17 | + const spy = sinon.spy() |
| 18 | + lcc["_onRequest"] = spy |
| 19 | + |
| 20 | + ShowDocumentAdapter.attach(lcc) |
| 21 | + expect((lcc["_onRequest"] as sinon.SinonSpy).calledOnce).to.be.true |
| 22 | + const spyArgs = spy.firstCall.args |
| 23 | + expect(spyArgs[0]).to.deep.equal({ method: "window/showDocument" }) |
| 24 | + expect(spyArgs[1]).to.equal(ShowDocumentAdapter.showDocument) |
| 25 | + }) |
| 26 | + |
| 27 | + it("onRequest connection is called", async () => { |
| 28 | + const connection = createSpyConnection() |
| 29 | + const lcc = new LanguageClientConnection(connection) |
| 30 | + |
| 31 | + const spy = sinon.spy() |
| 32 | + connection.onRequest = spy |
| 33 | + |
| 34 | + ShowDocumentAdapter.attach(lcc) |
| 35 | + expect((connection.onRequest as sinon.SinonSpy).calledOnce).to.be.true |
| 36 | + const spyArgs = spy.firstCall.args |
| 37 | + expect(spyArgs[0]).to.equal("window/showDocument") |
| 38 | + expect(typeof spyArgs[1]).to.equal("function") |
| 39 | + }) |
| 40 | + }) |
| 41 | + describe("can show documents", () => { |
| 42 | + describe("shows the document inside Atom", async () => { |
| 43 | + const helloPath = join(dirname(__dirname), "fixtures", "hello.js") |
| 44 | + |
| 45 | + async function canShowDocumentInAtom(params: ShowDocumentParams) { |
| 46 | + const { success } = await ShowDocumentAdapter.showDocument(params) |
| 47 | + expect(success).to.be.true |
| 48 | + |
| 49 | + const editor = atom.workspace.getTextEditors()[0] |
| 50 | + expect(editor instanceof TextEditor).to.be.true |
| 51 | + |
| 52 | + expect(editor!.getPath()).includes(helloPath) |
| 53 | + expect(editor!.getText()).includes(`atom.notifications.addSuccess("Hello World")`) |
| 54 | + |
| 55 | + return editor |
| 56 | + } |
| 57 | + |
| 58 | + beforeEach(() => { |
| 59 | + atom.workspace.getTextEditors().forEach((ed) => { |
| 60 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 61 | + //@ts-ignore |
| 62 | + ed.destroy() |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + it("shows the document inside Atom for the given URI", async () => { |
| 67 | + const params: ShowDocumentParams = { |
| 68 | + uri: helloPath, |
| 69 | + } |
| 70 | + await canShowDocumentInAtom(params) |
| 71 | + }) |
| 72 | + |
| 73 | + it("takes the focus", async () => { |
| 74 | + const params: ShowDocumentParams = { |
| 75 | + uri: helloPath, |
| 76 | + takeFocus: true, |
| 77 | + } |
| 78 | + const editor = await canShowDocumentInAtom(params) |
| 79 | + expect(atom.workspace.getActivePane()?.getItems()[0]).equal(editor) |
| 80 | + }) |
| 81 | + |
| 82 | + it("selects the given selection range", async () => { |
| 83 | + const selectionLSRange = { start: { line: 1, character: 30 }, end: { line: 1, character: 43 } } // `"Hello World"` in JavaScript file |
| 84 | + const params: ShowDocumentParams = { |
| 85 | + uri: helloPath, |
| 86 | + selection: selectionLSRange, |
| 87 | + } |
| 88 | + const editor = await canShowDocumentInAtom(params) |
| 89 | + expect(editor.getSelectedBufferRange()).deep.equal(Convert.lsRangeToAtomRange(selectionLSRange)) |
| 90 | + expect(editor.getSelectedText()).equal(`"Hello World"`) |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + describe("shows document in external programs", () => { |
| 95 | + it("shows the document in external programs for the given URI", async () => { |
| 96 | + const params: ShowDocumentParams = { |
| 97 | + uri: "http://www.github.com", |
| 98 | + external: true, |
| 99 | + } |
| 100 | + const spy = sinon.spy() |
| 101 | + shell.openExternal = spy |
| 102 | + |
| 103 | + const { success } = await ShowDocumentAdapter.showDocument(params) |
| 104 | + expect(success).to.be.true |
| 105 | + |
| 106 | + expect((shell.openExternal as sinon.SinonSpy).calledOnce).to.be.true |
| 107 | + const spyArgs = spy.firstCall.args |
| 108 | + expect(spyArgs[0]).to.equal("http://www.github.com") |
| 109 | + }) |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
0 commit comments