test(localizations): write a few tests for FlutterQuillLocalizationsWidget

pull/1954/head
Ellet 10 months ago
parent 26b04443f7
commit 7d6731ca40
  1. 19
      lib/src/l10n/extensions/localizations.dart
  2. 78
      test/widgets/editor_test.dart

@ -4,18 +4,23 @@ import '../generated/quill_localizations.dart' as generated;
typedef FlutterQuillLocalizations = generated.FlutterQuillLocalizations;
class MissingFlutterQuillLocalizationException extends UnimplementedError {
MissingFlutterQuillLocalizationException();
@override
String? get message =>
'FlutterQuillLocalizations instance is required and could not found. '
'Ensure that you are wrapping the current widget with '
'FlutterQuillLocalizationsWidget or add '
'FlutterQuillLocalizations.delegate to the localizationsDelegates '
'in your App widget (e.,g WidgetsApp, MaterialApp). If you believe this is a bug, consider reporting it.';
}
extension LocalizationsExt on BuildContext {
/// Require the [FlutterQuillLocalizations] instance
///
/// `loc` is short for `localizations`
FlutterQuillLocalizations get loc {
return FlutterQuillLocalizations.of(this) ??
(throw UnimplementedError(
"The instance of FlutterQuillLocalizations.of(context) is null and it's"
' required, please make sure you wrapping the current widget with '
'FlutterQuillLocalizationsWidget or add '
'FlutterQuillLocalizations.delegate to the localizationsDelegates '
'in your App widget, please consider reporting this as a bug',
));
(throw MissingFlutterQuillLocalizationException());
}
}

@ -3,6 +3,7 @@ import 'dart:convert' show jsonDecode;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:flutter_quill/translations.dart';
import 'package:flutter_quill_test/flutter_quill_test.dart';
import 'package:flutter_test/flutter_test.dart';
@ -137,5 +138,82 @@ void main() {
await tester.tap(find.byIcon(Icons.copy));
expect(didCopy, isTrue);
});
testWidgets(
'make sure QuillEditorOpenSearchAction does not throw an exception',
(tester) async {
final editorFocusNode = FocusNode();
await tester.pumpWidget(
MaterialApp(
home: QuillEditor.basic(
configurations: QuillEditorConfigurations(controller: controller),
focusNode: editorFocusNode,
),
),
);
// Required, otherwise the action shortcuts won't be invoked.
editorFocusNode.requestFocus();
await tester.sendKeyDownEvent(LogicalKeyboardKey.control);
await tester.sendKeyEvent(LogicalKeyboardKey.keyF);
await tester.sendKeyUpEvent(LogicalKeyboardKey.control);
await tester.pump();
final exception = tester.takeException();
expect(
exception,
isNot(
isInstanceOf<MissingFlutterQuillLocalizationException>(),
),
);
expect(exception, isNull);
},
);
testWidgets(
'should throw FlutterQuillLocalizationsWidget if the delegate not provided',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (context) => Text(context.loc.font),
),
),
);
final exception = tester.takeException();
expect(exception, isNotNull);
expect(
exception,
isA<MissingFlutterQuillLocalizationException>(),
);
},
);
testWidgets(
'should not throw FlutterQuillLocalizationsWidget if the delegate is provided',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: FlutterQuillLocalizationsWidget(
child: Builder(
builder: (context) => Text(context.loc.font),
),
),
),
);
final exception = tester.takeException();
expect(exception, isNull);
expect(
exception,
isNot(isA<MissingFlutterQuillLocalizationException>()),
);
},
);
});
}

Loading…
Cancel
Save