forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-expression.spec.js
More file actions
162 lines (143 loc) · 4.73 KB
/
Copy pathget-expression.spec.js
File metadata and controls
162 lines (143 loc) · 4.73 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import CodeMirror from "codemirror";
import { getExpressionFromCoords } from "../get-expression";
describe("get-expression", () => {
let isCreateTextRangeDefined;
beforeAll(() => {
if ((document.body: any).createTextRange) {
isCreateTextRangeDefined = true;
} else {
isCreateTextRangeDefined = false;
// CodeMirror needs createTextRange
// https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/5
(document.body: any).createTextRange = () => ({
getBoundingClientRect: jest.fn(),
getClientRects: () => ({})
});
}
});
afterAll(() => {
if (!isCreateTextRangeDefined) {
delete (document.body: any).createTextRange;
}
});
describe("getExpressionFromCoords", () => {
it("returns null when location.line is greater than the lineCount", () => {
const cm = CodeMirror(document.body, {
value: "let Line1;\n" + "let Line2;\n",
mode: "javascript"
});
const result = getExpressionFromCoords(cm, {
line: 3,
column: 1
});
expect(result).toBeNull();
});
it("gets the expression using CodeMirror.getTokenAt", () => {
const codemirrorMock = {
lineCount: () => 100,
getTokenAt: jest.fn(() => ({ start: 0, end: 0 })),
doc: {
getLine: () => ""
}
};
getExpressionFromCoords(codemirrorMock, { line: 1, column: 1 });
expect(codemirrorMock.getTokenAt).toHaveBeenCalled();
});
it("requests the correct line and column from codeMirror", () => {
const codemirrorMock = {
lineCount: () => 100,
getTokenAt: jest.fn(() => ({ start: 0, end: 1 })),
doc: {
getLine: jest.fn(() => "")
}
};
getExpressionFromCoords(codemirrorMock, { line: 20, column: 5 });
// getExpressionsFromCoords uses one based line indexing
// CodeMirror uses zero based line indexing
expect(codemirrorMock.getTokenAt).toHaveBeenCalledWith({
line: 19,
ch: 5
});
expect(codemirrorMock.doc.getLine).toHaveBeenCalledWith(19);
});
it("when called with column 0 returns null", () => {
const cm = CodeMirror(document.body, {
value: "foo bar;\n",
mode: "javascript"
});
const result = getExpressionFromCoords(cm, {
line: 1,
column: 0
});
expect(result).toBeNull();
});
it("gets the expression when first token on the line", () => {
const cm = CodeMirror(document.body, {
value: "foo bar;\n",
mode: "javascript"
});
const result = getExpressionFromCoords(cm, {
line: 1,
column: 1
});
if (!result) {
throw new Error("no result");
}
expect(result.expression).toEqual("foo");
expect(result.location.start).toEqual({ line: 1, column: 0 });
expect(result.location.end).toEqual({ line: 1, column: 3 });
});
it("includes previous tokens in the expression", () => {
const cm = CodeMirror(document.body, {
value: "foo.bar;\n",
mode: "javascript"
});
const result = getExpressionFromCoords(cm, {
line: 1,
column: 5
});
if (!result) {
throw new Error("no result");
}
expect(result.expression).toEqual("foo.bar");
expect(result.location.start).toEqual({ line: 1, column: 0 });
expect(result.location.end).toEqual({ line: 1, column: 7 });
});
it("includes multiple previous tokens in the expression", () => {
const cm = CodeMirror(document.body, {
value: "foo.bar.baz;\n",
mode: "javascript"
});
const result = getExpressionFromCoords(cm, {
line: 1,
column: 10
});
if (!result) {
throw new Error("no result");
}
expect(result.expression).toEqual("foo.bar.baz");
expect(result.location.start).toEqual({ line: 1, column: 0 });
expect(result.location.end).toEqual({ line: 1, column: 11 });
});
it("does not include tokens not part of the expression", () => {
const cm = CodeMirror(document.body, {
value: "foo bar.baz;\n",
mode: "javascript"
});
const result = getExpressionFromCoords(cm, {
line: 1,
column: 10
});
if (!result) {
throw new Error("no result");
}
expect(result.expression).toEqual("bar.baz");
expect(result.location.start).toEqual({ line: 1, column: 4 });
expect(result.location.end).toEqual({ line: 1, column: 11 });
});
});
});