operation_test.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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([0, 1],
  29. [Node(type: "node", attributes: {}, children: LinkedList())]),
  30. InsertOperation([0, 1],
  31. [Node(type: "node", attributes: {}, children: LinkedList())]));
  32. expect(t.path, [0, 2]);
  33. });
  34. test('delete + delete', () {
  35. final t = transformOperation(
  36. DeleteOperation([0, 1],
  37. [Node(type: "node", attributes: {}, children: LinkedList())]),
  38. DeleteOperation([0, 2],
  39. [Node(type: "node", attributes: {}, children: LinkedList())]));
  40. expect(t.path, [0, 1]);
  41. });
  42. });
  43. test('transform transaction builder', () {
  44. final item1 = Node(type: "node", attributes: {}, children: LinkedList());
  45. final item2 = Node(type: "node", attributes: {}, children: LinkedList());
  46. final item3 = Node(type: "node", attributes: {}, children: LinkedList());
  47. final root = Node(
  48. type: "root",
  49. attributes: {},
  50. children: LinkedList()
  51. ..addAll([
  52. item1,
  53. item2,
  54. item3,
  55. ]));
  56. final state = EditorState(document: StateTree(root: root));
  57. expect(item1.path, [0]);
  58. expect(item2.path, [1]);
  59. expect(item3.path, [2]);
  60. final tb = TransactionBuilder(state);
  61. tb.deleteNode(item1);
  62. tb.deleteNode(item2);
  63. tb.deleteNode(item3);
  64. final transaction = tb.finish();
  65. expect(transaction.operations[0].path, [0]);
  66. expect(transaction.operations[1].path, [0]);
  67. expect(transaction.operations[2].path, [0]);
  68. });
  69. group("toJson", () {
  70. test("insert", () {
  71. final root = Node(type: "root", attributes: {}, children: LinkedList());
  72. final state = EditorState(document: StateTree(root: root));
  73. final item1 = Node(type: "node", attributes: {}, children: LinkedList());
  74. final tb = TransactionBuilder(state);
  75. tb.insertNode([0], item1);
  76. final transaction = tb.finish();
  77. expect(transaction.toJson(), {
  78. "operations": [
  79. {
  80. "type": "insert-operation",
  81. "path": [0],
  82. "nodes": [item1.toJson()],
  83. }
  84. ],
  85. });
  86. });
  87. test("delete", () {
  88. final item1 = Node(type: "node", attributes: {}, children: LinkedList());
  89. final root = Node(
  90. type: "root",
  91. attributes: {},
  92. children: LinkedList()
  93. ..addAll([
  94. item1,
  95. ]));
  96. final state = EditorState(document: StateTree(root: root));
  97. final tb = TransactionBuilder(state);
  98. tb.deleteNode(item1);
  99. final transaction = tb.finish();
  100. expect(transaction.toJson(), {
  101. "operations": [
  102. {
  103. "type": "delete-operation",
  104. "path": [0],
  105. "nodes": [item1.toJson()],
  106. }
  107. ],
  108. });
  109. });
  110. });
  111. }