forked from FirebaseExtended/firepad
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcursor.spec.ts
69 lines (60 loc) · 2.43 KB
/
cursor.spec.ts
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
import { Cursor } from "../src/cursor";
import { TextOperation } from "../src/text-operation";
describe("Cursor", () => {
describe(".fromJSON", () => {
it("should create Cursor object from JSON", () => {
const cursorData = { position: 5, selectionEnd: 9 };
const cursor = Cursor.fromJSON(cursorData);
expect(cursor).toEqual(new Cursor(5, 9));
});
});
describe("#equals", () => {
it("should check for equality with another cursor with same co-ordinates", () => {
const cursor = new Cursor(5, 9);
const otherCursor = new Cursor(5, 9);
expect(cursor.equals(otherCursor)).toEqual(true);
});
it("should check for equality with another cursor with different co-ordinates", () => {
const cursor = new Cursor(5, 9);
const otherCursor = new Cursor(15, 18);
expect(cursor.equals(otherCursor)).toEqual(false);
});
it("should check for equality with invalid value", () => {
const cursor = new Cursor(5, 9);
expect(cursor.equals(null)).toEqual(false);
});
});
describe("#compose", () => {
it("should return final cursor", () => {
const cursor = new Cursor(5, 9);
const nextCursor = new Cursor(15, 18);
expect(cursor.compose(nextCursor)).toEqual(nextCursor);
});
});
describe("#transform", () => {
it("should update cursor based on incoming retain operation", () => {
const cursor = new Cursor(0, 0);
const operation = new TextOperation().retain(5, null);
const nextCursor = cursor.transform(operation);
expect(nextCursor).toEqual(new Cursor(0, 0));
});
it("should update cursor based on incoming insert operation", () => {
const cursor = new Cursor(0, 1);
const operation = new TextOperation().insert("Hello World", null); // 11 letters inserted.
const nextCursor = cursor.transform(operation);
expect(nextCursor).toEqual(new Cursor(11, 12));
});
it("should update cursor based on incoming delete operation", () => {
const cursor = new Cursor(12, 11);
const operation = new TextOperation().delete("Hello World"); // 11 letters deleted.
const nextCursor = cursor.transform(operation);
expect(nextCursor).toEqual(new Cursor(1, 0));
});
});
describe("#toJSON", () => {
it("should convert Cursor object into JSON", () => {
const cursor = new Cursor(6, 13);
expect(cursor.toJSON()).toEqual({ position: 6, selectionEnd: 13 });
});
});
});