Debian Codegraph @

frontend/test/editor.test.ts

raw
1 import { setDiagnosticsEffect } from "@codemirror/lint";
2 import { EditorState } from "@codemirror/state";
3 import { test } from "uvu";
4 import assert from "uvu/assert";
5
6 import {
7 replaceContents,
8 scrollToLine,
9 setErrors,
10 } from "../src/codemirror/editor-transactions";
11
12 test("replace editor contents", () => {
13 const state = EditorState.create({ doc: "test\n" });
14 assert.equal(state.sliceDoc(), "test\n");
15 const transaction = state.update(replaceContents(state, "asdfasdf\n"));
16 assert.equal(transaction.docChanged, true);
17 assert.equal(transaction.state.sliceDoc(), "asdfasdf\n");
18 });
19
20 test("scroll to line", () => {
21 const state = EditorState.create({ doc: "test\ntest\ntest\n" });
22
23 const second_line = state.update(scrollToLine(state, 2));
24 assert.equal(second_line.docChanged, false);
25 assert.ok(second_line.selection);
26 assert.equal(second_line.state.selection.main.from, 5);
27
28 const last_line = state.update(scrollToLine(state, state.doc.lines));
29 assert.equal(last_line.docChanged, false);
30 assert.ok(last_line.selection);
31 assert.equal(last_line.state.selection.main.from, 15);
32
33 const after_end = state.update(scrollToLine(state, state.doc.lines + 1));
34 assert.equal(after_end.docChanged, false);
35 assert.equal(after_end.selection, undefined);
36
37 const line_zero = state.update(scrollToLine(state, 0));
38 assert.equal(line_zero.docChanged, false);
39 assert.equal(line_zero.selection, undefined);
40 });
41
42 test("set errors", () => {
43 const state = EditorState.create({ doc: "test\ntest\ntest\n" });
44
45 const transaction = state.update(
46 setErrors(state, [
47 { type: "type", message: "first error", source: null },
48 {
49 type: "type",
50 message: "second error",
51 source: { lineno: 100, filename: "asdf" },
52 },
53 {
54 type: "type",
55 message: "third error",
56 source: { lineno: 2, filename: "asdf" },
57 },
58 ]),
59 );
60 const effect = transaction.effects[0];
61 assert.ok(effect);
62 assert.ok(effect.is(setDiagnosticsEffect));
63 assert.equal(effect.value, [
64 {
65 from: 0,
66 to: 4,
67 severity: "error",
68 message: "first error",
69 },
70 {
71 from: 15,
72 to: 15,
73 severity: "error",
74 message: "second error",
75 },
76 {
77 from: 5,
78 to: 9,
79 severity: "error",
80 message: "third error",
81 },
82 ]);
83 });
84
85 test.run();