appflowy-editor/test/legacy/undo_manager_test.dart

69 lines
2.2 KiB
Dart
Raw Normal View History

2022-09-13 17:33:26 +08:00
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/history/undo_manager.dart';
2022-09-13 17:33:26 +08:00
import 'package:flutter_test/flutter_test.dart';
void main() async {
setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
});
test('HistoryItem #1', () {
final editorState = EditorState.blank();
2022-09-13 17:33:26 +08:00
final historyItem = HistoryItem();
historyItem.add(DeleteOperation([0], [paragraphNode(text: '0')]));
historyItem.add(DeleteOperation([0], [paragraphNode(text: '1')]));
historyItem.add(DeleteOperation([0], [paragraphNode(text: '2')]));
2022-09-13 17:33:26 +08:00
final transaction = historyItem.toTransaction(editorState);
assert(isInsertAndPathEqual(transaction.operations[0], [0], '2'));
assert(isInsertAndPathEqual(transaction.operations[1], [0], '1'));
assert(isInsertAndPathEqual(transaction.operations[2], [0], '0'));
});
test('HistoryItem #2', () {
final editorState = EditorState.blank();
2022-09-13 17:33:26 +08:00
final historyItem = HistoryItem();
historyItem.add(DeleteOperation([0], [paragraphNode(text: '0')]));
historyItem.add(
const UpdateOperation([0], {'subType': 'number'}, {'subType': null}),
);
historyItem.add(DeleteOperation([0], [paragraphNode(), paragraphNode()]));
historyItem.add(DeleteOperation([0], [paragraphNode()]));
2022-09-13 17:33:26 +08:00
final transaction = historyItem.toTransaction(editorState);
assert(isInsertAndPathEqual(transaction.operations[0], [0]));
assert(isInsertAndPathEqual(transaction.operations[1], [0]));
assert(transaction.operations[2] is UpdateOperation);
assert(isInsertAndPathEqual(transaction.operations[3], [0], '0'));
});
2023-03-24 13:59:03 +01:00
test('HistoryItem sealed', () {
2023-03-24 13:59:03 +01:00
final historyItem = HistoryItem();
historyItem.add(DeleteOperation([0], [paragraphNode(text: '0')]));
2023-03-24 13:59:03 +01:00
historyItem.seal();
expect(historyItem.sealed, true);
});
2022-09-13 17:33:26 +08:00
}
bool isInsertAndPathEqual(Operation operation, Path path, [String? content]) {
if (operation is! InsertOperation) {
return false;
}
2022-10-09 23:53:18 +08:00
if (!operation.path.equals(path)) {
2022-09-13 17:33:26 +08:00
return false;
}
final delta = operation.nodes.first.delta;
if (delta == null) {
2022-09-13 17:33:26 +08:00
return false;
}
if (content == null) {
return true;
}
return delta.toPlainText() == content;
2022-09-13 17:33:26 +08:00
}