test: migrate test/core

This commit is contained in:
Lucas.Xu 2023-05-01 10:36:34 +08:00
commit 4f18b26715
4 changed files with 143 additions and 151 deletions

View file

@ -409,6 +409,10 @@ extension TextTransaction on Transaction {
} else { } else {
deleteNode(node); deleteNode(node);
if (i == nodes.length - 1) { if (i == nodes.length - 1) {
final delta = nodes.last.delta;
if (delta == null) {
continue;
}
final newDelta = Delta() final newDelta = Delta()
..insert(texts[0]) ..insert(texts[0])
..addAll( ..addAll(
@ -455,12 +459,12 @@ extension TextTransaction on Transaction {
); );
path = path.next; path = path.next;
} else { } else {
final node = nodes[i];
final delta = node.delta;
if (delta == null) {
continue;
}
if (i < nodes.length - 1) { if (i < nodes.length - 1) {
final node = nodes[i];
final delta = node.delta;
if (delta == null) {
continue;
}
replaceText( replaceText(
node, node,
0, 0,
@ -470,6 +474,10 @@ extension TextTransaction on Transaction {
path = path.next; path = path.next;
} else { } else {
if (i == texts.length - 1) { if (i == texts.length - 1) {
final delta = nodes.last.delta;
if (delta == null) {
continue;
}
final mewDelta = Delta() final mewDelta = Delta()
..insert(text) ..insert(text)
..addAll( ..addAll(

View file

@ -0,0 +1,143 @@
// import 'package:appflowy_editor/appflowy_editor.dart';
// import 'package:flutter_test/flutter_test.dart';
// import '../../infra/test_editor.dart';
// void main() {
// group('command_extension.dart', () {
// testWidgets('getTextInSelection w/ multiple nodes', (tester) async {
// final editor = tester.editor
// ..insertTextNode(
// 'Welcome',
// )
// ..insertTextNode(
// 'to',
// )
// ..insertTextNode(
// 'Appflowy 😁',
// );
// await editor.startTesting();
// final selection = Selection(
// start: Position(path: [2], offset: 5),
// end: Position(path: [0], offset: 5),
// );
// await editor.updateSelection(selection);
// final textNodes = editor
// .editorState.service.selectionService.currentSelectedNodes
// .whereType<TextNode>()
// .toList(growable: false);
// final text = editor.editorState.getTextInSelection(
// textNodes.normalized,
// selection.normalized,
// );
// expect(text, ['me', 'to', 'Appfl']);
// });
// testWidgets('getTextInSelection where selection.isSingle', (tester) async {
// final editor = tester.editor
// ..insertTextNode(
// 'Welcome',
// )
// ..insertTextNode(
// 'to',
// )
// ..insertTextNode(
// 'Appflowy 😁',
// );
// await editor.startTesting();
// final selection = Selection(
// start: Position(path: [0], offset: 3),
// end: Position(path: [0]),
// );
// await editor.updateSelection(selection);
// final textNodes = editor
// .editorState.service.selectionService.currentSelectedNodes
// .whereType<TextNode>()
// .toList(growable: false);
// final text = editor.editorState.getTextInSelection(
// textNodes.normalized,
// selection.normalized,
// );
// expect(text, ['Wel']);
// });
// testWidgets('getNode throws if node and path are null', (tester) async {
// final editor = tester.editor;
// await editor.startTesting();
// expect(() => editor.editorState.getNode(), throwsA(isA<Exception>()));
// });
// testWidgets('getNode by path', (tester) async {
// final editor = tester.editor
// ..insertTextNode(
// 'Welcome',
// )
// ..insertTextNode(
// 'to',
// )
// ..insertTextNode(
// 'Appflowy 😁',
// );
// await editor.startTesting();
// final node = editor.editorState.getNode(path: [0]) as TextNode;
// expect(node.type, 'text');
// expect((node.delta.first as TextInsert).text, 'Welcome');
// });
// testWidgets('getTextNode throws if textNode and path are null',
// (tester) async {
// final editor = tester.editor;
// await editor.startTesting();
// expect(() => editor.editorState.getTextNode(), throwsA(isA<Exception>()));
// });
// testWidgets('getTextNode by path', (tester) async {
// final editor = tester.editor
// ..insertTextNode(
// 'Welcome',
// )
// ..insertTextNode(
// 'to',
// )
// ..insertTextNode(
// 'Appflowy 😁',
// );
// await editor.startTesting();
// final node = editor.editorState.getTextNode(path: [1]);
// expect(node.type, 'text');
// expect((node.delta.first as TextInsert).text, 'to');
// });
// testWidgets(
// 'getSelection throws if selection and selectionService.currentSelection are null',
// (tester) async {
// final editor = tester.editor;
// await editor.startTesting();
// expect(
// () => editor.editorState.getSelection(null),
// throwsA(isA<Exception>()),
// );
// });
// });
// }

View file

@ -0,0 +1,218 @@
// import 'package:appflowy_editor/appflowy_editor.dart';
// import 'package:flutter_test/flutter_test.dart';
// import '../infra/test_editor.dart';
// void main() {
// group('TextCommands extension tests', () {
// testWidgets('insertText', (tester) async {
// final editor = tester.editor
// ..insertEmptyTextNode()
// ..insertTextNode('World');
// await editor.startTesting();
// editor.editorState.insertText(0, 'Hello', path: [0]);
// await tester.pumpAndSettle();
// expect(
// (editor.editorState.getTextNode(path: [0]).delta.first as TextInsert)
// .text,
// 'Hello',
// );
// });
// testWidgets('insertTextAtCurrentSelection', (tester) async {
// final editor = tester.editor..insertTextNode('Helrld!');
// await editor.startTesting();
// final selection = Selection(
// start: Position(path: [0], offset: 3),
// end: Position(path: [0], offset: 3),
// );
// await editor.updateSelection(selection);
// editor.editorState.insertTextAtCurrentSelection('lo Wo');
// await tester.pumpAndSettle();
// expect(
// (editor.editorState.getTextNode(path: [0]).delta.first as TextInsert)
// .text,
// 'Hello World!',
// );
// });
// testWidgets('formatText', (tester) async {
// final editor = tester.editor..insertTextNode('Hello');
// await editor.startTesting();
// final selection = Selection(
// start: Position(path: [0]),
// end: Position(path: [0], offset: 5),
// );
// await editor.updateSelection(selection);
// editor.editorState
// .formatText(null, {BuiltInAttributeKey.bold: true}, path: [0]);
// await tester.pumpAndSettle();
// final textNode = editor.editorState.getTextNode(path: [0]);
// final textInsert = textNode.delta.first as TextInsert;
// expect(textInsert.text, 'Hello');
// expect(textInsert.attributes?[BuiltInAttributeKey.bold], true);
// });
// testWidgets('formatTextWithBuiltInAttribute w/ Partial Style Key',
// (tester) async {
// final editor = tester.editor..insertTextNode('Hello');
// await editor.startTesting();
// final selection = Selection(
// start: Position(path: [0]),
// end: Position(path: [0], offset: 5),
// );
// await editor.updateSelection(selection);
// editor.editorState.formatTextWithBuiltInAttribute(
// BuiltInAttributeKey.underline,
// {BuiltInAttributeKey.underline: true},
// path: [0],
// );
// await tester.pumpAndSettle();
// final textNode = editor.editorState.getTextNode(path: [0]);
// final textInsert = textNode.delta.first as TextInsert;
// expect(textInsert.text, 'Hello');
// expect(textInsert.attributes?[BuiltInAttributeKey.underline], true);
// });
// testWidgets('formatTextWithBuiltInAttribute w/ Global Style Key',
// (tester) async {
// final editor = tester.editor
// ..insertTextNode(
// 'Hello',
// /// Formatting global style over another global style,
// /// will remove the existing one before adding the new one
// attributes: {BuiltInAttributeKey.checkbox: true},
// );
// await editor.startTesting();
// editor.editorState.formatTextWithBuiltInAttribute(
// BuiltInAttributeKey.heading,
// {BuiltInAttributeKey.heading: BuiltInAttributeKey.h1},
// path: [0],
// );
// await tester.pumpAndSettle();
// final textNode = editor.editorState.getTextNode(path: [0]);
// final textInsert = textNode.delta.first as TextInsert;
// expect(textInsert.text, 'Hello');
// expect(textNode.attributes.heading, BuiltInAttributeKey.h1);
// expect(textNode.attributes['subtype'], BuiltInAttributeKey.heading);
// });
// testWidgets('formatTextToCheckbox', (tester) async {
// final editor = tester.editor..insertTextNode('TextNode to Checkbox');
// await editor.startTesting();
// editor.editorState.formatTextToCheckbox(false, path: [0]);
// await tester.pumpAndSettle();
// final checkboxNode = editor.editorState.getNode(path: [0]);
// expect(checkboxNode.attributes.check, false);
// expect(checkboxNode.attributes['subtype'], BuiltInAttributeKey.checkbox);
// });
// testWidgets('formatLinkInText', (tester) async {
// const href = "https://appflowy.io/";
// final editor = tester.editor..insertTextNode('TextNode to Link');
// await editor.startTesting();
// final selection = Selection(
// start: Position(path: [0]),
// end: Position(path: [0], offset: 5),
// );
// await editor.updateSelection(selection);
// editor.editorState.formatLinkInText(href, path: [0]);
// await tester.pumpAndSettle();
// final textNode = editor.editorState.getTextNode(path: [0]);
// final textInsert = textNode.delta.first as TextInsert;
// expect(textInsert.attributes?[BuiltInAttributeKey.href], href);
// });
// testWidgets('insertNewLine', (tester) async {
// final editor = tester.editor;
// await editor.startTesting();
// expect(editor.documentLength, 0);
// editor.editorState.insertNewLine(path: [0]);
// await tester.pumpAndSettle();
// expect(editor.documentLength, 1);
// });
// testWidgets('insertNewLine without path', (tester) async {
// final editor = tester.editor..insertTextNode('Hello World');
// await editor.startTesting();
// expect(editor.documentLength, 1);
// final selection = Selection(
// start: Position(path: [0], offset: 5),
// end: Position(path: [0], offset: 5),
// );
// await editor.updateSelection(selection);
// editor.editorState.insertNewLine(path: null);
// await tester.pumpAndSettle();
// expect(editor.documentLength, 2);
// final textNode = editor.editorState.getTextNode(path: [0]);
// final textInsert = textNode.delta.first as TextInsert;
// expect(textInsert.text, 'Hello World');
// });
// testWidgets('insertNewLineAtCurrentSelection', (tester) async {
// final editor = tester.editor..insertTextNode('HelloWorld');
// await editor.startTesting();
// final selection = Selection(
// start: Position(path: [0], offset: 5),
// end: Position(path: [0], offset: 5),
// );
// await editor.updateSelection(selection);
// expect(editor.documentLength, 1);
// editor.editorState.insertNewLineAtCurrentSelection();
// await tester.pumpAndSettle();
// expect(editor.documentLength, 2);
// final firstTextNode = editor.editorState.getTextNode(path: [0]);
// final firstTextInsert = firstTextNode.delta.first as TextInsert;
// expect(firstTextInsert.text, 'Hello');
// final secondTextNode = editor.editorState.getTextNode(path: [1]);
// final secondTextInsert = secondTextNode.delta.first as TextInsert;
// expect(secondTextInsert.text, 'World');
// });
// });
// }

View file

@ -26,8 +26,9 @@ void main() async {
..replaceTexts(nodes, selection, texts); ..replaceTexts(nodes, selection, texts);
await editorState.apply(transaction); await editorState.apply(transaction);
expect(editorState.document.root.children.length, 4);
nodes = editorState.getNodesInSelection(selection); nodes = editorState.getNodesInSelection(selection);
expect(editorState.document.root.children.length, 4);
expect(nodes[0].delta?.toPlainText(), '0123ABC'); expect(nodes[0].delta?.toPlainText(), '0123ABC');
expect(nodes[1].delta?.toPlainText(), 'ABC'); expect(nodes[1].delta?.toPlainText(), 'ABC');
expect(nodes[2].delta?.toPlainText(), 'ABC'); expect(nodes[2].delta?.toPlainText(), 'ABC');
@ -56,7 +57,6 @@ void main() async {
await editorState.apply(transaction); await editorState.apply(transaction);
expect(editorState.document.root.children.length, 4); expect(editorState.document.root.children.length, 4);
expect(editorState.getNodeAtPath([0])?.delta?.toPlainText(), '0123ABC'); expect(editorState.getNodeAtPath([0])?.delta?.toPlainText(), '0123ABC');
expect(editorState.getNodeAtPath([1])?.delta?.toPlainText(), 'ABC'); expect(editorState.getNodeAtPath([1])?.delta?.toPlainText(), 'ABC');
expect(editorState.getNodeAtPath([2])?.delta?.toPlainText(), 'ABC'); expect(editorState.getNodeAtPath([2])?.delta?.toPlainText(), 'ABC');
@ -71,6 +71,7 @@ void main() async {
final editorState = EditorState(document: document); final editorState = EditorState(document: document);
expect(editorState.document.root.children.length, 5); expect(editorState.document.root.children.length, 5);
final selection = Selection( final selection = Selection(
start: Position(path: [0], offset: 4), start: Position(path: [0], offset: 4),
end: Position(path: [4], offset: 4), end: Position(path: [4], offset: 4),
@ -84,155 +85,138 @@ void main() async {
await editorState.apply(transaction); await editorState.apply(transaction);
expect(editorState.document.root.children.length, 1); expect(editorState.document.root.children.length, 1);
expect( expect(
editorState.getNodeAtPath([0])?.delta?.toPlainText(), editorState.getNodeAtPath([0])?.delta?.toPlainText(),
'0123ABC456789789', '0123ABC456789789',
); );
}); });
// testWidgets('test replaceTexts, textNodes.length < texts.length', test('test replaceTexts, textNodes.length < texts.length', () async {
// (tester) async { final document = Document.blank().addParagraphs(
// TestWidgetsFlutterBinding.ensureInitialized(); 3,
initialText: '0123456789',
);
final editorState = EditorState(document: document);
// final editor = tester.editor expect(editorState.document.root.children.length, 3);
// ..insertTextNode('0123456789')
// ..insertTextNode('0123456789')
// ..insertTextNode('0123456789');
// await editor.startTesting();
// await tester.pumpAndSettle();
// expect(editor.documentLength, 3); final selection = Selection(
start: Position(path: [0], offset: 4),
end: Position(path: [2], offset: 4),
);
final transaction = editorState.transaction;
final nodes = editorState.getNodesInSelection(selection);
// final selection = Selection( final texts = ['ABC', 'ABC', 'ABC', 'ABC'];
// start: Position(path: [0], offset: 4), transaction.replaceTexts(nodes, selection, texts);
// end: Position(path: [2], offset: 4), await editorState.apply(transaction);
// );
// final transaction = editor.editorState.transaction;
// var textNodes = [0, 1, 2]
// .map((e) => editor.nodeAtPath([e])!)
// .whereType<TextNode>()
// .toList(growable: false);
// final texts = ['ABC', 'ABC', 'ABC', 'ABC'];
// transaction.replaceTexts(textNodes, selection, texts);
// editor.editorState.apply(transaction);
// await tester.pumpAndSettle();
// expect(editor.documentLength, 4); expect(editorState.document.root.children.length, 4);
// textNodes = [0, 1, 2, 3] expect(editorState.getNodeAtPath([0])?.delta?.toPlainText(), '0123ABC');
// .map((e) => editor.nodeAtPath([e])!) expect(editorState.getNodeAtPath([1])?.delta?.toPlainText(), 'ABC');
// .whereType<TextNode>() expect(editorState.getNodeAtPath([2])?.delta?.toPlainText(), 'ABC');
// .toList(growable: false); expect(editorState.getNodeAtPath([3])?.delta?.toPlainText(), 'ABC456789');
// expect(textNodes[0].toPlainText(), '0123ABC'); });
// expect(textNodes[1].toPlainText(), 'ABC');
// expect(textNodes[2].toPlainText(), 'ABC');
// expect(textNodes[3].toPlainText(), 'ABC456789');
// });
// testWidgets('test replaceTexts, textNodes.length << texts.length', test('test replaceTexts, textNodes.length << texts.length', () async {
// (tester) async { final document = Document.blank().addParagraphs(
// TestWidgetsFlutterBinding.ensureInitialized(); 1,
initialText: 'Welcome to AppFlowy!',
);
final editorState = EditorState(document: document);
// final editor = tester.editor..insertTextNode('Welcome to AppFlowy!'); expect(editorState.document.root.children.length, 1);
// await editor.startTesting();
// await tester.pumpAndSettle();
// expect(editor.documentLength, 1); // select 'to'
final selection = Selection(
start: Position(path: [0], offset: 8),
end: Position(path: [0], offset: 10),
);
final transaction = editorState.transaction;
var nodes = editorState.getNodesInSelection(selection);
final texts = ['ABC1', 'ABC2', 'ABC3', 'ABC4', 'ABC5'];
transaction.replaceTexts(nodes, selection, texts);
await editorState.apply(transaction);
// // select 'to' expect(editorState.document.root.children.length, 5);
// final selection = Selection( expect(
// start: Position(path: [0], offset: 8), editorState.getNodeAtPath([0])?.delta?.toPlainText(),
// end: Position(path: [0], offset: 10), 'Welcome ABC1',
// ); );
// final transaction = editor.editorState.transaction; expect(editorState.getNodeAtPath([1])?.delta?.toPlainText(), 'ABC2');
// var textNodes = [0] expect(editorState.getNodeAtPath([2])?.delta?.toPlainText(), 'ABC3');
// .map((e) => editor.nodeAtPath([e])!) expect(editorState.getNodeAtPath([3])?.delta?.toPlainText(), 'ABC4');
// .whereType<TextNode>() expect(
// .toList(growable: false); editorState.getNodeAtPath([4])?.delta?.toPlainText(),
// final texts = ['ABC1', 'ABC2', 'ABC3', 'ABC4', 'ABC5']; 'ABC5 AppFlowy!',
// transaction.replaceTexts(textNodes, selection, texts); );
// editor.editorState.apply(transaction); });
// await tester.pumpAndSettle();
// expect(editor.documentLength, 5); test('test selection propagates if non-selected node is deleted', () async {
// textNodes = [0, 1, 2, 3, 4] final document = Document.blank()
// .map((e) => editor.nodeAtPath([e])!) .addParagraphs(
// .whereType<TextNode>() 1,
// .toList(growable: false); initialText: 'Welcome to AppFlowy!',
// expect(textNodes[0].toPlainText(), 'Welcome ABC1'); )
// expect(textNodes[1].toPlainText(), 'ABC2'); .addParagraphs(
// expect(textNodes[2].toPlainText(), 'ABC3'); 1,
// expect(textNodes[3].toPlainText(), 'ABC4'); initialText: 'Testing selection on this',
// expect(textNodes[4].toPlainText(), 'ABC5 AppFlowy!'); );
// }); final editorState = EditorState(document: document);
// testWidgets('test selection propagates if non-selected node is deleted', expect(editorState.document.root.children.length, 2);
// (tester) async {
// TestWidgetsFlutterBinding.ensureInitialized();
// final editor = tester.editor editorState.selection = Selection.single(
// ..insertTextNode('Welcome to AppFlowy!') path: [0],
// ..insertTextNode('Testing selection on this'); startOffset: 0,
endOffset: 20,
);
// await editor.startTesting(); final transaction = editorState.transaction;
// await tester.pumpAndSettle(); transaction.deleteNode(editorState.getNodeAtPath([1])!);
await editorState.apply(transaction);
// expect(editor.documentLength, 2); expect(editorState.document.root.children.length, 1);
expect(
editorState.selection,
Selection.single(
path: [0],
startOffset: 0,
endOffset: 20,
),
);
});
// await editor.updateSelection( test('test selection does not propagate if selected node is deleted',
// Selection.single( () async {
// path: [0], final document = Document.blank()
// startOffset: 0, .addParagraphs(
// endOffset: 20, 1,
// ), initialText: 'Welcome to AppFlowy!',
// ); )
// await tester.pumpAndSettle(); .addParagraphs(
1,
initialText: 'Testing selection on this',
);
final editorState = EditorState(document: document);
// final transaction = editor.editorState.transaction; expect(editorState.document.root.children.length, 2);
// transaction.deleteNode(editor.nodeAtPath([1])!);
// editor.editorState.apply(transaction);
// await tester.pumpAndSettle();
// expect(editor.documentLength, 1); editorState.selection = Selection.single(
// expect( path: [0],
// editor.editorState.cursorSelection, startOffset: 0,
// Selection.single( endOffset: 20,
// path: [0], );
// startOffset: 0,
// endOffset: 20,
// ),
// );
// });
// testWidgets('test selection does not propagate if selected node is deleted', final transaction = editorState.transaction;
// (tester) async { transaction.deleteNode(editorState.getNodeAtPath([0])!);
// TestWidgetsFlutterBinding.ensureInitialized(); await editorState.apply(transaction);
// final editor = tester.editor expect(editorState.document.root.children.length, 1);
// ..insertTextNode('Welcome to AppFlowy!') expect(
// ..insertTextNode('Testing selection on this'); editorState.selection,
null,
// await editor.startTesting(); );
// await tester.pumpAndSettle(); });
// expect(editor.documentLength, 2);
// await editor.updateSelection(
// Selection.single(
// path: [0],
// startOffset: 0,
// endOffset: 20,
// ),
// );
// await tester.pumpAndSettle();
// final transaction = editor.editorState.transaction;
// transaction.deleteNode(editor.nodeAtPath([0])!);
// editor.editorState.apply(transaction);
// await tester.pumpAndSettle();
// expect(editor.documentLength, 1);
// expect(editor.editorState.cursorSelection, null);
// });
}); });
} }