From d804b158fbdb7f9c1e26212db99137f9c57b9be0 Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Thu, 8 Aug 2024 12:40:37 +0330 Subject: [PATCH] Add test for closeDocument is false --- .../fleather/lib/src/widgets/controller.dart | 4 +- .../test/widgets/controller_test.dart | 55 ++++++++++++++----- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/packages/fleather/lib/src/widgets/controller.dart b/packages/fleather/lib/src/widgets/controller.dart index 5fa698a6..6ecb410a 100644 --- a/packages/fleather/lib/src/widgets/controller.dart +++ b/packages/fleather/lib/src/widgets/controller.dart @@ -319,11 +319,11 @@ class FleatherController extends ChangeNotifier { /// It creates a new empty [ParchmentDocument] and a clean edit history. /// The old [document] will be closed if [closeDocument] is true. /// - /// Calling this will notify all the listeners of this [TextEditingController] + /// Calling this will notify all the listeners of this [FleatherController] /// that they need to update (it calls [notifyListeners]). For this reason, /// this method should only be called between frames, e.g. in response to user /// actions, not during the build, layout, or paint phases. - void clear([bool closeDocument = true]) { + void clear({bool closeDocument = true}) { _throttleTimer?.cancel(); _toggledStyles = ParchmentStyle(); _selection = const TextSelection.collapsed(offset: 0); diff --git a/packages/fleather/test/widgets/controller_test.dart b/packages/fleather/test/widgets/controller_test.dart index 3e4a8367..b62c9691 100644 --- a/packages/fleather/test/widgets/controller_test.dart +++ b/packages/fleather/test/widgets/controller_test.dart @@ -274,20 +274,47 @@ void main() { // expect(controller.lastChangeSource, ChangeSource.local); }); - test('clear', () { - fakeAsync((async) { - controller.compose(Delta()..insert('word'), - selection: const TextSelection.collapsed(offset: 4)); - async.flushTimers(); - var notified = false; - controller.addListener(() => notified = true); - controller.clear(); - expect(controller.document.toDelta(), Delta()..insert('\n')); - expect(controller.selection, const TextSelection.collapsed(offset: 0)); - expect(controller.canUndo, isFalse); - expect(controller.canRedo, isFalse); - expect(controller.toggledStyles, ParchmentStyle()); - expect(notified, isTrue); + group('clear', () { + test('closes the document by default', () { + fakeAsync((async) { + final doc = controller.document; + controller.compose(Delta()..insert('word'), + selection: const TextSelection.collapsed(offset: 4)); + async.flushTimers(); + var notified = false; + controller.addListener(() => notified = true); + controller.clear(); + expect(identical(controller.document, doc), isFalse); + expect(controller.document.toDelta(), Delta()..insert('\n')); + expect(doc.isClosed, isTrue); + expect( + controller.selection, const TextSelection.collapsed(offset: 0)); + expect(controller.canUndo, isFalse); + expect(controller.canRedo, isFalse); + expect(controller.toggledStyles, ParchmentStyle()); + expect(notified, isTrue); + }); + }); + + test('closeDocument is false', () { + fakeAsync((async) { + final doc = controller.document; + controller.compose(Delta()..insert('word'), + selection: const TextSelection.collapsed(offset: 4)); + async.flushTimers(); + var notified = false; + controller.addListener(() => notified = true); + controller.clear(closeDocument: false); + expect(identical(controller.document, doc), isFalse); + expect(controller.document.toDelta(), Delta()..insert('\n')); + expect(doc.isClosed, isFalse); + expect( + controller.selection, const TextSelection.collapsed(offset: 0)); + expect(controller.canUndo, isFalse); + expect(controller.canRedo, isFalse); + expect(controller.toggledStyles, ParchmentStyle()); + expect(notified, isTrue); + }); }); });