operation_test.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'dart:collection';
  2. import 'package:flowy_editor/src/document/node.dart';
  3. import 'package:flutter_test/flutter_test.dart';
  4. import 'package:flowy_editor/src/operation/operation.dart';
  5. import 'package:flowy_editor/src/operation/transaction_builder.dart';
  6. import 'package:flowy_editor/src/editor_state.dart';
  7. import 'package:flowy_editor/src/document/state_tree.dart';
  8. void main() {
  9. TestWidgetsFlutterBinding.ensureInitialized();
  10. group('transform path', () {
  11. test('transform path changed', () {
  12. expect(transformPath([0, 1], [0, 1]), [0, 2]);
  13. expect(transformPath([0, 1], [0, 2]), [0, 3]);
  14. expect(transformPath([0, 1], [0, 2, 7, 8, 9]), [0, 3, 7, 8, 9]);
  15. expect(transformPath([0, 1, 2], [0, 0, 7, 8, 9]), [0, 0, 7, 8, 9]);
  16. });
  17. test("transform path not changed", () {
  18. expect(transformPath([0, 1, 2], [0, 0, 7, 8, 9]), [0, 0, 7, 8, 9]);
  19. expect(transformPath([0, 1, 2], [0, 1]), [0, 1]);
  20. expect(transformPath([1, 1], [1, 0]), [1, 0]);
  21. });
  22. test("transform path delta", () {
  23. expect(transformPath([0, 1], [0, 1], 5), [0, 6]);
  24. });
  25. });
  26. group('transform operation', () {
  27. test('insert + insert', () {
  28. final t = transformOperation(
  29. InsertOperation([0, 1],
  30. [Node(type: "node", attributes: {}, children: LinkedList())]),
  31. InsertOperation([0, 1],
  32. [Node(type: "node", attributes: {}, children: LinkedList())]));
  33. expect(t.path, [0, 2]);
  34. });
  35. test('delete + delete', () {
  36. final t = transformOperation(
  37. DeleteOperation([0, 1],
  38. [Node(type: "node", attributes: {}, children: LinkedList())]),
  39. DeleteOperation([0, 2],
  40. [Node(type: "node", attributes: {}, children: LinkedList())]));
  41. expect(t.path, [0, 1]);
  42. });
  43. });
  44. test('transform transaction builder', () {
  45. final item1 = Node(type: "node", attributes: {}, children: LinkedList());
  46. final item2 = Node(type: "node", attributes: {}, children: LinkedList());
  47. final item3 = Node(type: "node", attributes: {}, children: LinkedList());
  48. final root = Node(
  49. type: "root",
  50. attributes: {},
  51. children: LinkedList()
  52. ..addAll([
  53. item1,
  54. item2,
  55. item3,
  56. ]));
  57. final state = EditorState(document: StateTree(root: root));
  58. expect(item1.path, [0]);
  59. expect(item2.path, [1]);
  60. expect(item3.path, [2]);
  61. final tb = TransactionBuilder(state);
  62. tb.deleteNode(item1);
  63. tb.deleteNode(item2);
  64. tb.deleteNode(item3);
  65. final transaction = tb.finish();
  66. expect(transaction.operations[0].path, [0]);
  67. expect(transaction.operations[1].path, [0]);
  68. expect(transaction.operations[2].path, [0]);
  69. });
  70. group("toJson", () {
  71. test("insert", () {
  72. final root = Node(type: "root", attributes: {}, children: LinkedList());
  73. final state = EditorState(document: StateTree(root: root));
  74. final item1 = Node(type: "node", attributes: {}, children: LinkedList());
  75. final tb = TransactionBuilder(state);
  76. tb.insertNode([0], item1);
  77. final transaction = tb.finish();
  78. expect(transaction.toJson(), {
  79. "operations": [
  80. {
  81. "type": "insert-operation",
  82. "path": [0],
  83. "nodes": [item1.toJson()],
  84. }
  85. ]
  86. });
  87. });
  88. test("delete", () {
  89. final item1 = Node(type: "node", attributes: {}, children: LinkedList());
  90. final root = Node(
  91. type: "root",
  92. attributes: {},
  93. children: LinkedList()
  94. ..addAll([
  95. item1,
  96. ]));
  97. final state = EditorState(document: StateTree(root: root));
  98. final tb = TransactionBuilder(state);
  99. tb.deleteNode(item1);
  100. final transaction = tb.finish();
  101. expect(transaction.toJson(), {
  102. "operations": [
  103. {
  104. "type": "delete-operation",
  105. "path": [0],
  106. "nodes": [item1.toJson()],
  107. }
  108. ],
  109. });
  110. });
  111. });
  112. }