diff --git a/README.md b/README.md index d31e9a58..9fe48b01 100644 --- a/README.md +++ b/README.md @@ -391,6 +391,22 @@ tables, and mentions. Conversion can be performed in vanilla Dart (i.e., server- It is a complete Dart part of the popular and mature [quill-delta-to-html](https://www.npmjs.com/package/quill-delta-to-html) Typescript/Javascript package. +## Testing + +To aid in testing applications using the editor an extension to the flutter `WidgetTester` is provided which includes methods to simplify interacting with the editor in test cases. + +Import the test utilities in your test file: + +```dart +import 'package:flutter_quill/flutter_quill_test.dart'; +``` + +and then enter text using `quillEnterText`: + +```dart +await tester.quillEnterText(find.byType(QuillEditor), 'test\n'); +``` + ## Sponsors diff --git a/lib/flutter_quill_test.dart b/lib/flutter_quill_test.dart new file mode 100644 index 00000000..988e4e82 --- /dev/null +++ b/lib/flutter_quill_test.dart @@ -0,0 +1,3 @@ +library flutter_quill_test; + +export 'src/test/widget_tester_extension.dart'; diff --git a/test/widget_tester_extension.dart b/lib/src/test/widget_tester_extension.dart similarity index 75% rename from test/widget_tester_extension.dart rename to lib/src/test/widget_tester_extension.dart index af716d27..21bb75ab 100644 --- a/test/widget_tester_extension.dart +++ b/lib/src/test/widget_tester_extension.dart @@ -1,8 +1,10 @@ import 'package:flutter/material.dart'; -import 'package:flutter_quill/src/widgets/editor.dart'; -import 'package:flutter_quill/src/widgets/raw_editor.dart'; import 'package:flutter_test/flutter_test.dart'; +import '../widgets/editor.dart'; +import '../widgets/raw_editor.dart'; + +/// Extends extension QuillEnterText on WidgetTester { /// Give the QuillEditor widget specified by [finder] the focus. Future quillGiveFocus(Finder finder) { @@ -29,12 +31,18 @@ extension QuillEnterText on WidgetTester { Future quillEnterText(Finder finder, String text) async { return TestAsyncUtils.guard(() async { await quillGiveFocus(finder); - await updateEditingValue(finder, text); + await quillUpdateEditingValue(finder, text); await idle(); }); } - Future updateEditingValue(Finder finder, String text) async { + /// Update the text editing value of the QuillEditor widget specified by + /// [finder] with [text], as if it had been provided by the onscreen keyboard. + /// + /// The widget specified by [finder] must already have focus and be a + /// [QuillEditor] or have a [QuillEditor] descendant. For example + /// `find.byType(QuillEditor)`. + Future quillUpdateEditingValue(Finder finder, String text) async { return TestAsyncUtils.guard(() async { final editor = state( find.descendant( diff --git a/pubspec.yaml b/pubspec.yaml index 4ca8ee66..db62be2b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,7 +25,7 @@ dependencies: platform: ^3.1.0 pasteboard: ^0.2.0 -dev_dependencies: + # Dependencies for testing utilities flutter_test: sdk: flutter diff --git a/test/bug_fix_test.dart b/test/bug_fix_test.dart index e3796b18..ecbcad2b 100644 --- a/test/bug_fix_test.dart +++ b/test/bug_fix_test.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:flutter_quill/flutter_quill.dart' hide Text; +import 'package:flutter_quill/flutter_quill.dart'; +import 'package:flutter_quill/flutter_quill_test.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'widget_tester_extension.dart'; void main() { group('Bug fix', () { diff --git a/test/widgets/editor_test.dart b/test/widgets/editor_test.dart index 8c266b66..3fd425fc 100644 --- a/test/widgets/editor_test.dart +++ b/test/widgets/editor_test.dart @@ -2,12 +2,10 @@ import 'dart:convert' show jsonDecode; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'package:flutter_quill/src/widgets/controller.dart'; -import 'package:flutter_quill/src/widgets/editor.dart'; +import 'package:flutter_quill/flutter_quill.dart'; +import 'package:flutter_quill/flutter_quill_test.dart'; import 'package:flutter_test/flutter_test.dart'; -import '../widget_tester_extension.dart'; - void main() { late QuillController controller;