The directory structure of test files is consistent with the code files, makes it easy for us to judge the test status of the new added files and to retrieve the test code path of the corresponding file.
Construct document for testing
const text = 'Welcome to Appflowy 😁';
// Get the instance of editor.
final editor = tester.editor;
// Insert empty text node.
editor.insertEmptyTextNode();
// Insert text node with string.
editor.insertTextNode(text);
// Insert text node with heading style.
editor.insertTextNode(text, attributes: {
StyleKey.subtype: StyleKey.heading,
StyleKey.heading: StyleKey.h1,
});
// Insert text node with bulleted list style and bold style.
editor.insertTextNode(
'',
attributes: {
StyleKey.subtype: StyleKey.bulletedList,
},
delta: Delta([
TextInsert(text, {StyleKey.bold: true}),
]),
);
The startTesting
function must be called before testing.
await editor.startTesting();
Get the number of nodes in document
final length = editor.documentLength;
print(length);
Get the node of the specified path
// 获取上述文档结构中的第一个文本节点
final firstTextNode = editor.nodeAtPath([0]) as TextNode;
Update selection
await editor.updateSelection(
Selection.single(path: firstTextNode.path, startOffset: 0),
);
Get the selection
final selection = editor.documentSelection;
print(selection);
Simulate shortcut event input
// Command + A.
await editor.pressLogicKey(LogicalKeyboardKey.keyA, isMetaPressed: true);
// Command + shift + S.
await editor.pressLogicKey(
LogicalKeyboardKey.keyS,
isMetaPressed: true,
isShiftPressed: true,
);
Simulate text input
// Insert 'Hello World' at the beginning of the first node.
editor.insertText(firstTextNode, 'Hello World', 0);
Get information about the text node
// Get plain text.
final textAfterInserted = firstTextNode.toRawString();
print(textAfterInserted);
// Get attributes.
final attributes = firstTextNode.attributes;
print(attributes);
For example, we are going to test the file select_all_handler.dart
Full code example
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../infra/test_editor.dart';
void main() async {
setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
});
group('select_all_handler_test.dart', () {
testWidgets('Presses Command + A in the document', (tester) async {
const lines = 100;
const text = 'Welcome to Appflowy 😁';
final editor = tester.editor;
for (var i = 0; i < lines; i++) {
editor.insertTextNode(text);
}
await editor.startTesting();
await editor.pressLogicKey(LogicalKeyboardKey.keyA, isMetaPressed: true);
expect(
editor.documentSelection,
Selection(
start: Position(path: [0], offset: 0),
end: Position(path: [lines - 1], offset: text.length),
),
);
});
});
}
For the rest of the information on testing, such as simulated clicks, please refer to An introduction to widget testing