Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 169ea16

Browse files
wldcordeirojasonLaster
authored andcommitted
Tests for editor utils (#5129)
1 parent 2dea27a commit 169ea16

File tree

10 files changed

+526
-43
lines changed

10 files changed

+526
-43
lines changed

src/utils/editor/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// @flow
66

77
export * from "./source-documents";
8-
export * from "./getTokenLocation.js";
8+
export * from "./get-token-location";
99
export * from "./source-search";
1010
export * from "../ui";
1111
export * from "./create-editor";
@@ -51,9 +51,10 @@ export function traverseResults(e, ctx, query, dir, modifiers) {
5151
}
5252
}
5353

54-
export function toEditorLine(sourceId: string, lineOrOffset: number): ?number {
54+
export function toEditorLine(sourceId: string, lineOrOffset: number): number {
5555
if (isWasm(sourceId)) {
56-
return wasmOffsetToLine(sourceId, lineOrOffset);
56+
// TODO ensure offset is always "mappable" to edit line.
57+
return wasmOffsetToLine(sourceId, lineOrOffset) || 0;
5758
}
5859

5960
return lineOrOffset ? lineOrOffset - 1 : 1;
@@ -104,9 +105,7 @@ export function toSourceLocation(
104105
};
105106
}
106107

107-
export function markText(editor: any, className, location: EditorRange) {
108-
const { start, end } = location;
109-
108+
export function markText(editor: any, className, { start, end }: EditorRange) {
110109
return editor.codeMirror.markText(
111110
{ ch: start.column, line: start.line },
112111
{ ch: end.column, line: end.line },

src/utils/editor/source-search.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ function updateCursor(cm, state, keepSelection) {
104104
}
105105
}
106106

107-
function getMatchIndex(count: number, currentIndex: number, rev: boolean) {
107+
export function getMatchIndex(
108+
count: number,
109+
currentIndex: number,
110+
rev: boolean
111+
) {
108112
if (!rev) {
109113
if (currentIndex == count - 1) {
110114
return 0;
@@ -210,7 +214,11 @@ function searchNext(ctx, rev, query, newQuery, modifiers) {
210214
* @memberof utils/source-search
211215
* @static
212216
*/
213-
function removeOverlay(ctx: any, query: string, modifiers: SearchModifiers) {
217+
export function removeOverlay(
218+
ctx: any,
219+
query: string,
220+
modifiers: SearchModifiers
221+
) {
214222
const state = getSearchState(ctx.cm, query, modifiers);
215223
ctx.cm.removeOverlay(state.overlay);
216224
const { line, ch } = ctx.cm.getCursor();
@@ -241,7 +249,7 @@ function clearSearch(cm, query: string, modifiers: SearchModifiers) {
241249
* @memberof utils/source-search
242250
* @static
243251
*/
244-
function find(
252+
export function find(
245253
ctx: any,
246254
query: string,
247255
keepSelection: boolean,
@@ -257,7 +265,7 @@ function find(
257265
* @memberof utils/source-search
258266
* @static
259267
*/
260-
function findNext(
268+
export function findNext(
261269
ctx: any,
262270
query: string,
263271
keepSelection: boolean,
@@ -272,7 +280,7 @@ function findNext(
272280
* @memberof utils/source-search
273281
* @static
274282
*/
275-
function findPrev(
283+
export function findPrev(
276284
ctx: any,
277285
query: string,
278286
keepSelection: boolean,
@@ -281,4 +289,4 @@ function findPrev(
281289
return doSearch(ctx, true, query, keepSelection, modifiers);
282290
}
283291

284-
export { buildQuery, find, findNext, findPrev, removeOverlay, getMatchIndex };
292+
export { buildQuery };
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`createEditor Adds codeFolding 1`] = `
4+
Object {
5+
"enableCodeFolding": true,
6+
"extraKeys": Object {
7+
"Cmd-F": false,
8+
"Cmd-G": false,
9+
"Esc": false,
10+
},
11+
"foldGutter": true,
12+
"gutters": Array [
13+
"breakpoints",
14+
"hit-markers",
15+
"CodeMirror-linenumbers",
16+
"CodeMirror-foldgutter",
17+
],
18+
"lineNumbers": true,
19+
"lineWrapping": false,
20+
"matchBrackets": true,
21+
"mode": "javascript",
22+
"readOnly": true,
23+
"showAnnotationRuler": true,
24+
"styleActiveLine": false,
25+
"theme": "mozilla",
26+
"value": " ",
27+
}
28+
`;
29+
30+
exports[`createEditor Returns a SourceEditor 1`] = `
31+
Object {
32+
"enableCodeFolding": false,
33+
"extraKeys": Object {
34+
"Cmd-F": false,
35+
"Cmd-G": false,
36+
"Esc": false,
37+
},
38+
"foldGutter": false,
39+
"gutters": Array [
40+
"breakpoints",
41+
"hit-markers",
42+
"CodeMirror-linenumbers",
43+
],
44+
"lineNumbers": true,
45+
"lineWrapping": false,
46+
"matchBrackets": true,
47+
"mode": "javascript",
48+
"readOnly": true,
49+
"showAnnotationRuler": true,
50+
"styleActiveLine": false,
51+
"theme": "mozilla",
52+
"value": " ",
53+
}
54+
`;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createEditor } from "../create-editor";
2+
import SourceEditor from "../source-editor";
3+
4+
import { features } from "../../prefs";
5+
6+
describe("createEditor", () => {
7+
test("Returns a SourceEditor", () => {
8+
const editor = createEditor();
9+
expect(editor).toBeInstanceOf(SourceEditor);
10+
expect(editor.opts).toMatchSnapshot();
11+
expect(editor.opts.gutters).not.toContain("CodeMirror-foldgutter");
12+
});
13+
14+
test("Adds codeFolding", () => {
15+
features.codeFolding = true;
16+
const editor = createEditor();
17+
expect(editor).toBeInstanceOf(SourceEditor);
18+
expect(editor.opts).toMatchSnapshot();
19+
expect(editor.opts.gutters).toContain("CodeMirror-foldgutter");
20+
});
21+
});

0 commit comments

Comments
 (0)