2022-08-16 15:08:51 +08:00
# Testing
2022-08-17 17:02:54 +08:00
> 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.
2022-08-16 15:08:51 +08:00
2022-08-17 17:02:54 +08:00
## Testing Functions
2022-08-16 15:08:51 +08:00
2022-08-17 17:02:54 +08:00
**Construct document for testing**
2022-08-16 15:08:51 +08:00
```dart
const text = 'Welcome to Appflowy 😁';
2022-08-17 17:02:54 +08:00
// Get the instance of editor.
2022-08-16 15:08:51 +08:00
final editor = tester.editor;
2022-08-17 17:02:54 +08:00
// Insert empty text node.
2022-08-16 15:08:51 +08:00
editor.insertEmptyTextNode();
2022-08-17 17:02:54 +08:00
// Insert text node with string.
2022-08-16 15:08:51 +08:00
editor.insertTextNode(text);
2022-08-17 17:02:54 +08:00
// Insert text node with heading style.
2022-08-16 15:08:51 +08:00
editor.insertTextNode(text, attributes: {
StyleKey.subtype: StyleKey.heading,
StyleKey.heading: StyleKey.h1,
});
2022-08-17 17:02:54 +08:00
// Insert text node with bulleted list style and bold style.
2022-08-16 15:08:51 +08:00
editor.insertTextNode(
'',
attributes: {
StyleKey.subtype: StyleKey.bulletedList,
},
delta: Delta([
TextInsert(text, {StyleKey.bold: true}),
]),
);
```
2022-08-17 17:02:54 +08:00
**The `startTesting` function must be called before testing**.
2022-08-16 15:08:51 +08:00
```dart
await editor.startTesting();
```
2022-08-17 17:02:54 +08:00
**Get the number of nodes in document**
2022-08-16 15:08:51 +08:00
```dart
final length = editor.documentLength;
print(length);
```
2022-08-17 17:02:54 +08:00
**Get the node of the specified path**
2022-08-16 15:08:51 +08:00
```dart
final firstTextNode = editor.nodeAtPath([0]) as TextNode;
```
2022-08-17 17:02:54 +08:00
**Update selection**
2022-08-16 15:08:51 +08:00
```dart
await editor.updateSelection(
Selection.single(path: firstTextNode.path, startOffset: 0),
);
```
2022-08-17 17:02:54 +08:00
**Get the selection**
2022-08-16 15:08:51 +08:00
```dart
final selection = editor.documentSelection;
print(selection);
```
2022-08-17 17:02:54 +08:00
**Simulate shortcut event input**
2022-08-16 15:08:51 +08:00
```dart
2022-08-17 17:02:54 +08:00
// Command + A.
2022-08-16 15:08:51 +08:00
await editor.pressLogicKey(LogicalKeyboardKey.keyA, isMetaPressed: true);
2022-08-17 17:02:54 +08:00
// Command + shift + S.
2022-08-16 15:08:51 +08:00
await editor.pressLogicKey(
LogicalKeyboardKey.keyS,
isMetaPressed: true,
isShiftPressed: true,
);
```
2022-08-17 17:02:54 +08:00
**Simulate text input**
2022-08-16 15:08:51 +08:00
```dart
2022-08-17 17:02:54 +08:00
// Insert 'Hello World' at the beginning of the first node.
2022-08-16 15:08:51 +08:00
editor.insertText(firstTextNode, 'Hello World', 0);
```
2022-08-17 17:02:54 +08:00
**Get information about the text node**
2022-08-16 15:08:51 +08:00
```dart
2022-08-17 17:02:54 +08:00
// Get plain text.
2022-08-16 15:08:51 +08:00
final textAfterInserted = firstTextNode.toRawString();
print(textAfterInserted);
2022-08-17 17:02:54 +08:00
// Get attributes.
2022-08-16 15:08:51 +08:00
final attributes = firstTextNode.attributes;
print(attributes);
```
## Example
2022-08-17 17:02:54 +08:00
For example, we are going to test the file `select_all_handler.dart`
2022-08-16 15:08:51 +08:00
2022-08-17 17:02:54 +08:00
**Full code example**
2022-08-16 15:08:51 +08:00
```dart
2022-08-17 17:23:20 +08:00
import 'package:appflowy_editor/appflowy_editor.dart';
2022-08-16 15:08:51 +08:00
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 {
2022-08-17 17:02:54 +08:00
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),
),
);
2022-08-16 15:08:51 +08:00
});
2022-08-17 17:02:54 +08:00
});
2022-08-16 15:08:51 +08:00
}
```
2022-08-17 17:02:54 +08:00
For the rest of the information on testing, such as simulated clicks, please refer to [An introduction to widget testing ](https://docs.flutter.dev/cookbook/testing/widget/introduction )