-
Notifications
You must be signed in to change notification settings - Fork 269
/
code-context-builder-spec.js
77 lines (70 loc) · 2.4 KB
/
code-context-builder-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"use babel" // TODO
/* eslint-disable no-invalid-this */ import CodeContextBuilder from "../lib/code-context-builder"
describe("CodeContextBuilder", () => {
beforeEach(() => {
this.editorMock = {
getTitle() {},
getPath() {},
getText() {},
getLastSelection() {
return {
isEmpty() {
return false
},
}
},
getGrammar() {
return { name: "JavaScript" }
},
getLastCursor() {},
save() {},
}
spyOn(this.editorMock, "getTitle").andReturn("file.js")
spyOn(this.editorMock, "getPath").andReturn("path/to/file.js")
spyOn(this.editorMock, "getText").andReturn('console.log("hello")\n')
this.codeContextBuilder = new CodeContextBuilder()
})
describe("initCodeContext", () => {
it("sets correct text source for empty selection", () => {
const selection = {
isEmpty() {
return true
},
}
spyOn(this.editorMock, "getLastSelection").andReturn(selection)
const codeContext = this.codeContextBuilder.initCodeContext(this.editorMock)
expect(codeContext.textSource).toEqual(this.editorMock)
expect(codeContext.filename).toEqual("file.js")
expect(codeContext.filepath).toEqual("path/to/file.js")
})
it("sets correct text source for non-empty selection", () => {
const selection = {
isEmpty() {
return false
},
}
spyOn(this.editorMock, "getLastSelection").andReturn(selection)
const codeContext = this.codeContextBuilder.initCodeContext(this.editorMock)
expect(codeContext.textSource).toEqual(selection)
expect(codeContext.selection).toEqual(selection)
})
it("sets correct lang", () => {
const codeContext = this.codeContextBuilder.initCodeContext(this.editorMock)
expect(codeContext.lang).toEqual("JavaScript")
})
})
describe("buildCodeContext", () =>
["Selection Based", "Line Number Based"].map((argType) =>
it(`sets lineNumber with screenRow + 1 when ${argType}`, () => {
const cursor = {
getScreenRow() {
return 1
},
}
spyOn(this.editorMock, "getLastCursor").andReturn(cursor)
const codeContext = this.codeContextBuilder.buildCodeContext(this.editorMock, argType)
expect(codeContext.argType).toEqual(argType)
expect(codeContext.lineNumber).toEqual(2)
})
))
})