Skip to content

Commit 8b57a89

Browse files
committed
test: make getSuggestionsMock more general by accepting a request
1 parent c555996 commit 8b57a89

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

test/adapters/autocomplete-adapter.test.ts

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ function createRequest({
2828
}
2929

3030
describe("AutoCompleteAdapter", () => {
31+
let server: ActiveServer
32+
let autoCompleteAdapter: AutoCompleteAdapter
33+
3134
function createActiveServerSpy(): ActiveServer {
3235
return {
3336
capabilities: { completionProvider: {} },
@@ -38,6 +41,27 @@ describe("AutoCompleteAdapter", () => {
3841
}
3942
}
4043

44+
type getSuggestionParams = Parameters<typeof autoCompleteAdapter.getSuggestions>
45+
46+
/** Function that stubs `server.connection.completion` and returns the `autoCompleteAdapter.getSuggestions(...)` */
47+
async function getSuggestionsMock(
48+
items: CompletionItem[],
49+
request: getSuggestionParams[1],
50+
onDidConvertCompletionItem?: getSuggestionParams[2],
51+
minimumWordLength?: getSuggestionParams[3],
52+
): Promise<ac.AnySuggestion[]> {
53+
const resultsSandBox = sinon.createSandbox()
54+
resultsSandBox.stub(server.connection, "completion").resolves(items)
55+
const results = autoCompleteAdapter.getSuggestions(
56+
server,
57+
request,
58+
onDidConvertCompletionItem,
59+
minimumWordLength,
60+
)
61+
resultsSandBox.restore()
62+
return results
63+
}
64+
4165
const completionItems: CompletionItem[] = [
4266
{
4367
label: "thisHasFiltertext",
@@ -73,45 +97,21 @@ describe("AutoCompleteAdapter", () => {
7397
const request = createRequest({ prefix: "lab" })
7498

7599
describe("getSuggestions", () => {
76-
let server: ActiveServer
77-
let autoCompleteAdapter: AutoCompleteAdapter
78-
79-
type getSuggestionParams = Parameters<typeof autoCompleteAdapter.getSuggestions>
80-
81-
/** Function that stubs `server.connection.completion` and returns the `autoCompleteAdapter.getSuggestions(...)` */
82-
async function getSuggestionsMock(
83-
items: CompletionItem[],
84-
requestParams: Parameters<typeof createRequest>[0],
85-
onDidConvertCompletionItem?: getSuggestionParams[2],
86-
minimumWordLength?: getSuggestionParams[3],
87-
): Promise<ac.AnySuggestion[]> {
88-
const resultsSandBox = sinon.createSandbox()
89-
resultsSandBox.stub(server.connection, "completion").resolves(items)
90-
const results = autoCompleteAdapter.getSuggestions(
91-
server,
92-
createRequest(requestParams),
93-
onDidConvertCompletionItem,
94-
minimumWordLength,
95-
)
96-
resultsSandBox.restore()
97-
return results
98-
}
99-
100100
beforeEach(() => {
101101
server = createActiveServerSpy()
102102
autoCompleteAdapter = new AutoCompleteAdapter()
103103
})
104104

105105
it("gets AutoComplete suggestions via LSP given an AutoCompleteRequest", async () => {
106-
const results = await getSuggestionsMock(completionItems, { prefix: "" })
106+
const results = await getSuggestionsMock(completionItems, createRequest({ prefix: "" }))
107107
expect(results.length).equals(completionItems.length)
108108
})
109109

110110
it("provides a filtered selection based on the filterKey", async () => {
111-
const results = await getSuggestionsMock(completionItems, { prefix: "lab" })
112-
expect(results.length).equals(2)
113-
expect(results.some((r) => r.displayText === "thisHasFiltertext")).to.be.true
114-
expect(results.some((r) => r.displayText === "label3")).to.be.true
111+
const resultsLab = await getSuggestionsMock(completionItems, createRequest({ prefix: "lab" }))
112+
expect(resultsLab.length).equals(2)
113+
expect(resultsLab.some((r) => r.displayText === "thisHasFiltertext")).to.be.true
114+
expect(resultsLab.some((r) => r.displayText === "label3")).to.be.true
115115
})
116116

117117
it("uses the sortText property to arrange completions when there is no prefix", async () => {
@@ -120,7 +120,7 @@ describe("AutoCompleteAdapter", () => {
120120
{ label: "b" },
121121
{ label: "c", sortText: "a" },
122122
]
123-
const results = await getSuggestionsMock(sortedItems, { prefix: "" })
123+
const results = await getSuggestionsMock(sortedItems, createRequest({ prefix: "" }))
124124

125125
expect(results.length).equals(sortedItems.length)
126126
expect(results[0].displayText).equals("c")
@@ -129,7 +129,7 @@ describe("AutoCompleteAdapter", () => {
129129
})
130130

131131
it("uses the filterText property to arrange completions when there is a prefix", async () => {
132-
const results = await getSuggestionsMock(completionItems, { prefix: "lab" })
132+
const results = await getSuggestionsMock(completionItems, createRequest({ prefix: "lab" }))
133133
expect(results.length).equals(2)
134134
expect(results[0].displayText).equals("label3") // shorter than 'labrador', so expected to be first
135135
expect(results[1].displayText).equals("thisHasFiltertext")
@@ -225,9 +225,6 @@ describe("AutoCompleteAdapter", () => {
225225
},
226226
]
227227

228-
let server: ActiveServer
229-
let autoCompleteAdapter: AutoCompleteAdapter
230-
231228
beforeEach(() => {
232229
server = createActiveServerSpy()
233230
autoCompleteAdapter = new AutoCompleteAdapter()

0 commit comments

Comments
 (0)