Skip to content

Commit

Permalink
Update autoformats_test.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Aug 25, 2024
1 parent 584e87d commit cb8fa8a
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions packages/fleather/test/widgets/autoformats_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockAutoFormat extends Mock implements AutoFormat {}

class MockAutoFormatResult extends Mock implements AutoFormatResult {}

void main() {
late AutoFormats autoformats;

Expand All @@ -16,15 +12,10 @@ void main() {
});

test('Can use custom formats with fallbacks', () {
final customAutoformat = MockAutoFormat();
final document = ParchmentDocument();
const position = 5;
const data = 'Test';
when(() => customAutoformat.apply(any(), any(), any()))
.thenReturn(MockAutoFormatResult());
final formats = AutoFormats.fallback([customAutoformat]);
formats.run(document, position, data);
verify(() => customAutoformat.apply(document, position, data));
final formats = AutoFormats.fallback([FakeAutoFormat('.')]);
expect(formats.run(document, 0, '.'), isTrue);
expect(document.toDelta(), Delta()..insert('Fake\n'));
});

group('Link detection', () {
Expand Down Expand Up @@ -283,3 +274,28 @@ void main() {
});
});
}

class FakeAutoFormat extends AutoFormat {
final String trigger;

FakeAutoFormat(this.trigger);

@override
AutoFormatResult? apply(
ParchmentDocument document, int position, String data) {
if (data == trigger) {
final change = Delta()
..retain(position)
..insert('Fake');
document.compose(change, ChangeSource.local);
return AutoFormatResult(
change: change,
undo: Delta()
..retain(position)
..delete(4),
undoPositionCandidate: position,
);
}
return null;
}
}

0 comments on commit cb8fa8a

Please sign in to comment.