mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
docs: fix testing.md
This commit is contained in:
parent
8e1f2e3e02
commit
71c84568d8
@ -1,25 +1,24 @@
|
||||
# Testing
|
||||
|
||||
目前测试文件的目录结构与代码文件的目录结构是保持一致的,这样方便我们查找新增文件的测试情况,以及方便检索对应文件的测试代码路径。
|
||||
> 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.
|
||||
|
||||
## 提供的测试方法
|
||||
## Testing Functions
|
||||
|
||||
|
||||
构造测试的文档数据
|
||||
**Construct document for testing**
|
||||
```dart
|
||||
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);
|
||||
// 插入样式heading的文本节点
|
||||
// Insert text node with heading style.
|
||||
editor.insertTextNode(text, attributes: {
|
||||
StyleKey.subtype: StyleKey.heading,
|
||||
StyleKey.heading: StyleKey.h1,
|
||||
});
|
||||
// 插入样式bulleted list的加粗的文本节点
|
||||
// Insert text node with bulleted list style and bold style.
|
||||
editor.insertTextNode(
|
||||
'',
|
||||
attributes: {
|
||||
@ -31,41 +30,41 @@ editor.insertTextNode(
|
||||
);
|
||||
```
|
||||
|
||||
在测试前必须调用
|
||||
**The `startTesting` function must be called before testing**.
|
||||
```dart
|
||||
await editor.startTesting();
|
||||
```
|
||||
|
||||
获取当前渲染的节点数量
|
||||
**Get the number of nodes in document**
|
||||
```dart
|
||||
final length = editor.documentLength;
|
||||
print(length);
|
||||
```
|
||||
|
||||
获取节点
|
||||
**Get the node of the specified path**
|
||||
```dart
|
||||
// 获取上述文档结构中的第一个文本节点
|
||||
final firstTextNode = editor.nodeAtPath([0]) as TextNode;
|
||||
```
|
||||
|
||||
更新选区信息
|
||||
**Update selection**
|
||||
```dart
|
||||
await editor.updateSelection(
|
||||
Selection.single(path: firstTextNode.path, startOffset: 0),
|
||||
);
|
||||
```
|
||||
|
||||
获取选区信息
|
||||
**Get the selection**
|
||||
```dart
|
||||
final selection = editor.documentSelection;
|
||||
print(selection);
|
||||
```
|
||||
|
||||
模拟快捷键输入
|
||||
**Simulate shortcut event input**
|
||||
```dart
|
||||
// 输入 command + A
|
||||
// Command + A.
|
||||
await editor.pressLogicKey(LogicalKeyboardKey.keyA, isMetaPressed: true);
|
||||
// 输入 command + shift + S
|
||||
// Command + shift + S.
|
||||
await editor.pressLogicKey(
|
||||
LogicalKeyboardKey.keyS,
|
||||
isMetaPressed: true,
|
||||
@ -73,26 +72,26 @@ await editor.pressLogicKey(
|
||||
);
|
||||
```
|
||||
|
||||
模拟文字输入
|
||||
**Simulate text input**
|
||||
```dart
|
||||
// 在第一个节点的最起始位置插入'Hello World'
|
||||
// Insert 'Hello World' at the beginning of the first node.
|
||||
editor.insertText(firstTextNode, 'Hello World', 0);
|
||||
```
|
||||
|
||||
获取文本节点的信息
|
||||
**Get information about the text node**
|
||||
```dart
|
||||
// 获取纯文字
|
||||
// Get plain text.
|
||||
final textAfterInserted = firstTextNode.toRawString();
|
||||
print(textAfterInserted);
|
||||
// 获取文字的描述信息
|
||||
// Get attributes.
|
||||
final attributes = firstTextNode.attributes;
|
||||
print(attributes);
|
||||
```
|
||||
|
||||
## Example
|
||||
例如,目前需要测试 select_all_handler.dart 的文件
|
||||
For example, we are going to test the file `select_all_handler.dart`
|
||||
|
||||
完整的例子
|
||||
**Full code example**
|
||||
```dart
|
||||
import 'package:flowy_editor/appflowy_editor.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@ -106,24 +105,25 @@ void main() async {
|
||||
|
||||
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);
|
||||
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),
|
||||
),
|
||||
);
|
||||
expect(
|
||||
editor.documentSelection,
|
||||
Selection(
|
||||
start: Position(path: [0], offset: 0),
|
||||
end: Position(path: [lines - 1], offset: text.length),
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
其余关于测试的,例如模拟点击等信息请参考 [An introduction to widget testing](https://docs.flutter.dev/cookbook/testing/widget/introduction)
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user