Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Jun 20, 2023
1 parent 3b01ab5 commit 72a1269
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ class NumberedListBlockKeys {
}

Node numberedListNode({
Attributes? attributes,
Delta? delta,
Attributes? attributes,
int? number,
Iterable<Node>? children,
}) {
attributes ??= {'delta': (delta ?? Delta()).toJson()};
attributes ??= {
'delta': (delta ?? Delta()).toJson(),
NumberedListBlockKeys.number: number,
};
return Node(
type: NumberedListBlockKeys.type,
attributes: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../infra/testable_editor.dart';
import '../../util/util.dart';

void main() async {
group('numbered list component', () {
const text = 'Welcome to AppFlowy Editor 🔥!';
// 100. Welcome to AppFlowy Editor 🔥!
// 101. Welcome to AppFlowy Editor 🔥!
// 102. Welcome to AppFlowy Editor 🔥!
testWidgets('the number of the numbered list should be ascending',
(tester) async {
final editor = tester.editor
..addNode(
numberedListNode(delta: Delta()..insert(text), number: 100),
)
..addNode(
numberedListNode(delta: Delta()..insert(text)),
)
..addNode(
numberedListNode(delta: Delta()..insert(text), number: 200),
);
await editor.startTesting();

expect(find.text('100.', findRichText: true), findsOneWidget);
expect(find.text('101.', findRichText: true), findsOneWidget);
expect(find.text('102.', findRichText: true), findsOneWidget);
expect(find.text('200.', findRichText: true), findsNothing);

await editor.dispose();
});

// Before
// | <- insert new numbered list here
// 100. Welcome to AppFlowy Editor 🔥!
// 101. Welcome to AppFlowy Editor 🔥!
// After
// 1. Welcome to AppFlowy Editor 🔥!
// 2. Welcome to AppFlowy Editor 🔥!
// 3. Welcome to AppFlowy Editor 🔥!
testWidgets(
'insert a new numbered list before the existing one, and the number should keep ascending',
(tester) async {
final editor = tester.editor
..addParagraph(initialText: text)
..addNode(
numberedListNode(delta: Delta()..insert(text), number: 100),
)
..addNode(
numberedListNode(delta: Delta()..insert(text)),
);
await editor.startTesting();

expect(find.text('100.', findRichText: true), findsOneWidget);
expect(find.text('101.', findRichText: true), findsOneWidget);

final selection = Selection.collapse([0], 0);
await editor.updateSelection(selection);

await editor.ime.typeText('1.');
await editor.ime.typeText(' ');

expect(editor.nodeAtPath([0])!.type, NumberedListBlockKeys.type);
expect(find.text('1.', findRichText: true), findsOneWidget);
expect(find.text('2.', findRichText: true), findsOneWidget);
expect(find.text('3.', findRichText: true), findsOneWidget);
expect(find.text('100.', findRichText: true), findsNothing);
expect(find.text('101.', findRichText: true), findsNothing);

await editor.dispose();
});

// Before
// 1. Welcome to AppFlowy Editor 🔥!
// | <- delete this line
// 100. Welcome to AppFlowy Editor 🔥!
// 101. Welcome to AppFlowy Editor 🔥!
// After
// 1. Welcome to AppFlowy Editor 🔥!
// 2. Welcome to AppFlowy Editor 🔥!
// 3. Welcome to AppFlowy Editor 🔥!
testWidgets(
'delete the paragraph between two group of numbered lists, and the number of the following numbered list should keep ascending',
(tester) async {
final editor = tester.editor
..addNode(
numberedListNode(delta: Delta()..insert(text), number: 1),
)
..addEmptyParagraph()
..addNode(
numberedListNode(delta: Delta()..insert(text), number: 100),
)
..addNode(
numberedListNode(delta: Delta()..insert(text)),
);
await editor.startTesting();

expect(find.text('1.', findRichText: true), findsOneWidget);
expect(find.text('100.', findRichText: true), findsOneWidget);
expect(find.text('101.', findRichText: true), findsOneWidget);

final selection = Selection.collapse([1], 0);
await editor.updateSelection(selection);

await editor.pressKey(key: LogicalKeyboardKey.backspace);

expect(editor.documentRootLen, 3);
expect(find.text('1.', findRichText: true), findsOneWidget);
expect(find.text('2.', findRichText: true), findsOneWidget);
expect(find.text('3.', findRichText: true), findsOneWidget);
expect(find.text('100.', findRichText: true), findsNothing);
expect(find.text('101.', findRichText: true), findsNothing);

await editor.ime.typeText('\n');
await editor.ime.typeText('\n');
expect(editor.documentRootLen, 4);
expect(find.text('1.', findRichText: true), findsOneWidget);
expect(find.text('2.', findRichText: true), findsNothing);
expect(find.text('3.', findRichText: true), findsNothing);
expect(find.text('100.', findRichText: true), findsOneWidget);
expect(find.text('101.', findRichText: true), findsOneWidget);

await editor.dispose();
});
});
}
10 changes: 6 additions & 4 deletions test/new/infra/testable_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ class MockIMEInput {
// if the selection is collapsed, do insertion.
// else if the selection is not collapsed, do replacement.
if (selection.isCollapsed) {
return insertText(text);
await insertText(text);
} else {
return replaceText(text);
await replaceText(text);
}
}

Expand All @@ -250,7 +250,7 @@ class MockIMEInput {
if (delta == null) {
return;
}
return imeInput.apply([
await imeInput.apply([
TextEditingDeltaInsertion(
oldText: ' ${delta.toPlainText()}', // TODO: fix this workaround
textInserted: text,
Expand All @@ -261,6 +261,7 @@ class MockIMEInput {
composing: TextRange.empty,
)
]);
await tester.pumpAndSettle();
}

Future<void> replaceText(String text) async {
Expand All @@ -269,7 +270,7 @@ class MockIMEInput {
return;
}
final texts = editorState.getTextInSelection(selection).join('\n');
return imeInput.apply([
await imeInput.apply([
TextEditingDeltaReplacement(
oldText: ' $texts',
replacementText: text,
Expand All @@ -283,5 +284,6 @@ class MockIMEInput {
composing: TextRange.empty,
)
]);
await tester.pumpAndSettle();
}
}

0 comments on commit 72a1269

Please sign in to comment.