2022-08-16 15:08:51 +08:00
# Testing
2022-08-17 21:36:21 +08:00
> The directory structure of test files is consistent with the code files, making it easy for us to map a file with the corresponding test and check if the test is updated
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 21:36:21 +08:00
**Construct a 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 21:36:21 +08:00
**Get the number of nodes in the document**
2022-08-16 15:08:51 +08:00
```dart
final length = editor.documentLength;
print(length);
```
2022-08-17 21:36:21 +08:00
**Get the node of a defined 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 21:36:21 +08:00
**Simulate shortcut event inputs**
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 21:36:21 +08:00
**Simulate a 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 21:36:21 +08:00
For example, we are going to test `select_all_handler.dart`
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 21:36:21 +08:00
For more information about testing, such as simulating a click, please refer to [An introduction to widget testing ](https://docs.flutter.dev/cookbook/testing/widget/introduction )