forked from AppFlowy-IO/appflowy-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_test.dart
139 lines (133 loc) · 4.11 KB
/
operation_test.dart
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
import 'dart:collection';
import 'package:appflowy_editor/src/core/document/node.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:appflowy_editor/src/core/transform/operation.dart';
import 'package:appflowy_editor/src/editor_state.dart';
import 'package:appflowy_editor/src/core/document/document.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('transform path', () {
test('transform path changed', () {
expect(transformPath([0, 1], [0, 1]), [0, 2]);
expect(transformPath([0, 1], [0, 2]), [0, 3]);
expect(transformPath([0, 1], [0, 2, 7, 8, 9]), [0, 3, 7, 8, 9]);
expect(transformPath([0, 1, 2], [0, 0, 7, 8, 9]), [0, 0, 7, 8, 9]);
});
test("transform path not changed", () {
expect(transformPath([0, 1, 2], [0, 0, 7, 8, 9]), [0, 0, 7, 8, 9]);
expect(transformPath([0, 1, 2], [0, 1]), [0, 1]);
expect(transformPath([1, 1], [1, 0]), [1, 0]);
});
test("transform path delta", () {
expect(transformPath([0, 1], [0, 1], 5), [0, 6]);
});
});
group('transform operation', () {
test('insert + insert', () {
final t = transformOperation(
InsertOperation(
[0, 1],
[Node(type: "node", attributes: {}, children: LinkedList())],
),
InsertOperation(
[0, 1],
[Node(type: "node", attributes: {}, children: LinkedList())],
),
);
expect(t!.path, [0, 2]);
});
test('delete + delete', () {
final t = transformOperation(
DeleteOperation(
[0, 1],
[Node(type: "node", attributes: {}, children: LinkedList())],
),
DeleteOperation(
[0, 2],
[Node(type: "node", attributes: {}, children: LinkedList())],
),
);
expect(t!.path, [0, 1]);
});
test('delete two nodes with same parent', () {
final t = transformOperation(
DeleteOperation(
[0],
[Node(type: "node", attributes: {}, children: LinkedList())],
),
DeleteOperation(
[0, 2],
[Node(type: "node", attributes: {}, children: LinkedList())],
),
);
expect(t, null);
});
});
test('transform transaction builder', () {
final item1 = Node(type: "node");
final item2 = Node(type: "node");
final item3 = Node(type: "node");
final root = Node(
type: 'page',
attributes: {},
children: [
item1,
item2,
item3,
],
);
final state = EditorState(document: Document(root: root));
expect(item1.path, [0]);
expect(item2.path, [1]);
expect(item3.path, [2]);
final transaction = state.transaction;
transaction.deleteNode(item1);
transaction.deleteNode(item2);
transaction.deleteNode(item3);
state.apply(transaction);
expect(transaction.operations[0].path, [0]);
expect(transaction.operations[1].path, [0]);
expect(transaction.operations[2].path, [0]);
});
group("toJson", () {
test("insert", () {
final root = Node(type: "root", attributes: {}, children: LinkedList());
final state = EditorState(document: Document(root: root));
final item1 = Node(type: "node", attributes: {}, children: LinkedList());
final transaction = state.transaction;
transaction.insertNode([0], item1);
state.apply(transaction);
expect(transaction.toJson(), {
"operations": [
{
"op": "insert",
"path": [0],
"nodes": [item1.toJson()],
}
],
});
});
test("delete", () {
final item1 = Node(type: "node", attributes: {}, children: LinkedList());
final root = Node(
type: "root",
children: [
item1,
],
);
final state = EditorState(document: Document(root: root));
final transaction = state.transaction;
transaction.deleteNode(item1);
state.apply(transaction);
expect(transaction.toJson(), {
"operations": [
{
"op": "delete",
"path": [0],
"nodes": [item1.toJson()],
}
],
});
});
});
}