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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/* 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/>. */
import {
toEditorLine,
toEditorPosition,
toSourceLine,
scrollToPosition,
markText,
lineAtHeight,
getSourceLocationFromMouseEvent,
forEachLine,
removeLineClass,
clearLineClass,
getTextForLine,
getCursorLine,
} from "../index";
import { makeMockSource } from "../../test-mockup";
describe("toEditorLine", () => {
it("returns an editor line", () => {
const testId = "test-123";
const line = 30;
expect(toEditorLine(testId, line)).toEqual(29);
});
});
describe("toEditorPosition", () => {
it("returns an editor position", () => {
const loc = { source: { id: "source" }, line: 100, column: 25 };
expect(toEditorPosition(loc)).toEqual({
line: 99,
column: 25,
});
});
});
describe("toSourceLine", () => {
it("returns a source line", () => {
const testId = "test-123";
const line = 30;
expect(toSourceLine(testId, line)).toEqual(31);
});
});
const codeMirror = {
doc: {
iter: jest.fn((_, __, cb) => cb()),
},
lineCount: jest.fn(() => 100),
getLine: jest.fn(() => "something"),
getCursor: jest.fn(() => ({ line: 3 })),
getScrollerElement: jest.fn(() => ({
offsetWidth: 100,
offsetHeight: 100,
})),
getScrollInfo: () => ({
top: 0,
right: 0,
bottom: 0,
left: 0,
clientHeight: 100,
clientWidth: 100,
}),
removeLineClass: jest.fn(),
operation: jest.fn(cb => cb()),
charCoords: jest.fn(() => ({
top: 100,
right: 50,
bottom: 100,
left: 50,
})),
coordsChar: jest.fn(() => ({ line: 6, ch: 30 })),
lineAtHeight: jest.fn(() => 300),
markText: jest.fn(),
scrollTo: jest.fn(),
defaultCharWidth: jest.fn(() => 8),
defaultTextHeight: jest.fn(() => 16),
};
const editor = { codeMirror };
describe("scrollToPosition", () => {
it("calls codemirror APIs charCoords, getScrollerElement, scrollTo", () => {
scrollToPosition(codeMirror, 60, 123);
expect(codeMirror.charCoords).toHaveBeenCalledWith(
{ line: 60, ch: 123 },
"local"
);
expect(codeMirror.scrollTo).toHaveBeenCalledWith(0, 50);
});
});
describe("markText", () => {
it("calls codemirror API markText & returns marker", () => {
const loc = {
start: { line: 10, column: 0 },
end: { line: 30, column: 50 },
};
markText(editor, "test-123", loc);
expect(codeMirror.markText).toHaveBeenCalledWith(
{ ch: loc.start.column, line: loc.start.line },
{ ch: loc.end.column, line: loc.end.line },
{ className: "test-123" }
);
});
});
describe("lineAtHeight", () => {
it("calls codemirror API lineAtHeight", () => {
const e = { clientX: 30, clientY: 60 };
expect(lineAtHeight(editor, "test-123", e)).toEqual(301);
expect(editor.codeMirror.lineAtHeight).toHaveBeenCalledWith(e.clientY);
});
});
describe("getSourceLocationFromMouseEvent", () => {
it("calls codemirror API coordsChar & returns location", () => {
const source = makeMockSource(undefined, "test-123");
const e = { clientX: 30, clientY: 60 };
expect(getSourceLocationFromMouseEvent(editor, source, e)).toEqual({
source,
line: 7,
column: 31,
sourceActorId: undefined,
sourceActor: null,
});
expect(editor.codeMirror.coordsChar).toHaveBeenCalledWith({
left: 30,
top: 60,
});
});
});
describe("forEachLine", () => {
it("calls codemirror API operation && doc.iter across a doc", () => {
const test = jest.fn();
forEachLine(codeMirror, test);
expect(codeMirror.operation).toHaveBeenCalled();
expect(codeMirror.doc.iter).toHaveBeenCalledWith(0, 100, test);
});
});
describe("removeLineClass", () => {
it("calls codemirror API removeLineClass", () => {
const line = 3;
const className = "test-class";
removeLineClass(codeMirror, line, className);
expect(codeMirror.removeLineClass).toHaveBeenCalledWith(
line,
"wrap",
className
);
});
});
describe("clearLineClass", () => {
it("Uses forEachLine & removeLineClass to clear class on all lines", () => {
codeMirror.operation.mockClear();
codeMirror.doc.iter.mockClear();
codeMirror.removeLineClass.mockClear();
clearLineClass(codeMirror, "test-class");
expect(codeMirror.operation).toHaveBeenCalled();
expect(codeMirror.doc.iter).toHaveBeenCalledWith(
0,
100,
expect.any(Function)
);
expect(codeMirror.removeLineClass).toHaveBeenCalled();
});
});
describe("getTextForLine", () => {
it("calls codemirror API getLine & returns line text", () => {
getTextForLine(codeMirror, 3);
expect(codeMirror.getLine).toHaveBeenCalledWith(2);
});
});
describe("getCursorLine", () => {
it("calls codemirror API getCursor & returns line number", () => {
getCursorLine(codeMirror);
expect(codeMirror.getCursor).toHaveBeenCalled();
});
});
|