Skip to content

Commit

Permalink
Add test for closeDocument is false
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Aug 8, 2024
1 parent 7e4dcd3 commit d804b15
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/fleather/lib/src/widgets/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
55 changes: 41 additions & 14 deletions packages/fleather/test/widgets/controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

Expand Down

0 comments on commit d804b15

Please sign in to comment.