operation_test.dart 4.0 KB

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