Skip to content

Commit

Permalink
test: flight log test (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-athome committed Aug 14, 2023
1 parent c5d6b2a commit cf9fc38
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 11 deletions.
8 changes: 6 additions & 2 deletions lib/screens/flight_log/flight_log_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class _FlightLogModalState extends State<FlightLogModal> {
// fuel + fuel price
CupertinoFormSection(children: <Widget>[
CupertinoTextFormFieldRow(
key: const Key("flight_log_modal_form_fuel_price"),
key: const Key("input_flightLogModal_fuel"),
controller: _fuelController,
prefix: Text(AppLocalizations.of(context)!
.flightLogModal_label_fuel_cupertino),
Expand All @@ -209,6 +209,7 @@ class _FlightLogModalState extends State<FlightLogModal> {
),
// TODO convert to standalone form row widget (using a controller? Though material widget doesn't support it...)
CupertinoTextFormFieldRow(
key: const Key("input_flightLogModal_fuelPrice"),
controller: _fuelPriceController,
prefix: Text(AppLocalizations.of(context)!
.flightLogModal_label_fuel_cost_cupertino(
Expand Down Expand Up @@ -368,7 +369,7 @@ class _FlightLogModalState extends State<FlightLogModal> {
contentPadding: const EdgeInsets.fromLTRB(20, 5, 20, 5),
leading: const Icon(Icons.local_gas_station),
title: TextFormField(
key: const Key("flight_log_modal_form_fuel_price"),
key: const Key("input_flightLogModal_fuel"),
controller: _fuelController,
// TODO cursorColor: widget.model.backgroundColor,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
Expand All @@ -389,6 +390,7 @@ class _FlightLogModalState extends State<FlightLogModal> {
trailing: SizedBox(
width: MediaQuery.of(context).size.width * 0.4,
child: _MaterialFuelPriceSelector.totalCost(
key: const Key("input_flightLogModal_fuelPrice"),
textController: _fuelPriceController,
currencySymbol: _appConfig.fuelPriceCurrency,
),
Expand Down Expand Up @@ -442,6 +444,7 @@ class _FlightLogModalState extends State<FlightLogModal> {
);
trailingActions = [
PlatformTextButton(
key: const Key('button_flightLogModal_save'),
onPressed: () => _onSave(context),
cupertino: (_, __) => CupertinoTextButtonData(
// workaround for https://github.com/flutter/flutter/issues/32701
Expand All @@ -454,6 +457,7 @@ class _FlightLogModalState extends State<FlightLogModal> {
leadingAction = null;
trailingActions = [
PlatformIconButton(
key: const Key('button_flightLogModal_save'),
onPressed: () => _onSave(context),
icon: const Icon(Icons.check_sharp),
material: (_, __) => MaterialIconButtonData(
Expand Down
122 changes: 113 additions & 9 deletions test/screens/flight_log/flight_log_modal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:airborne/services/flight_log_services.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
Expand All @@ -13,15 +14,18 @@ import 'package:provider/provider.dart';
import 'flight_log_modal_test.mocks.dart';

@GenerateMocks([AppConfig, FlightLogBookService])
void main() {
void main() async {
const locale = Locale('en');
final lang = await AppLocalizations.delegate.load(locale);

Widget createSkeletonApp(FlightLogItem model) => MaterialApp(
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
locale: const Locale('en'),
locale: locale,
home: MultiProvider(
providers: [
_provideAppConfigForSampleAircraft(),
Expand All @@ -48,39 +52,139 @@ void main() {
await tester.pumpWidget(createSkeletonApp(item));

await tester.enterText(
find.byKey(const Key("flight_log_modal_form_fuel_price")), "42");
find.byKey(const Key("input_flightLogModal_fuelPrice")), "42");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);

await tester.enterText(
find.byKey(const Key("flight_log_modal_form_fuel_price")), "ABC");
find.byKey(const Key("input_flightLogModal_fuelPrice")), "ABC");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), false);

await tester.enterText(
find.byKey(const Key("flight_log_modal_form_fuel_price")), "43.2");
find.byKey(const Key("input_flightLogModal_fuelPrice")), "43.2");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuelPrice")), "43.28293");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuelPrice")), "43.28");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);

// FIXME this should fail on the locale of the test
await tester.enterText(
find.byKey(const Key("flight_log_modal_form_fuel_price")), "43,2");
find.byKey(const Key("input_flightLogModal_fuelPrice")), "43,2");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);
});

testWidgets('Fuel amount validation', (tester) async {
// TODO
FlightLogItem item = FlightLogItem(
null,
DateTime.now(),
'Sara',
'Fly@localhost',
'Fly@localhost',
1238,
1238,
null,
null,
null,
);
await tester.pumpWidget(createSkeletonApp(item));

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuel")), "42");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuel")), "ABC");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), false);

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuel")), "43.2");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuel")), "43.28");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuel")), "43.28");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuel")), "43.2802");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);

// FIXME this should fail on the locale of the test
await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuel")), "43,2");
await tester.pump();
expect(tester.state<FormState>(find.byType(Form)).validate(), true);
});

testWidgets('Fuel amount mandatory when fuel cost is greater than zero',
(tester) async {
// TODO
FlightLogItem item = FlightLogItem(
null,
DateTime.now(),
'Sara',
'Fly@localhost',
'Fly@localhost',
1238,
1238,
null,
null,
null,
);
await tester.pumpWidget(createSkeletonApp(item));

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuelPrice")), "102.38");
await tester.pump();
await tester.tap(find.byKey(const Key('button_flightLogModal_save')));
await tester.pump();
expect(find.byType(PlatformAlertDialog), findsOneWidget);
expect(find.text(lang.flightLogModal_error_invalid_fuel_empty),
findsOneWidget);
});

testWidgets('Fuel cost mandatory when fuel amount is greater than zero',
(tester) async {
// TODO
FlightLogItem item = FlightLogItem(
null,
DateTime.now(),
'Sara',
'Fly@localhost',
'Fly@localhost',
1238,
1238,
null,
null,
null,
);
await tester.pumpWidget(createSkeletonApp(item));

await tester.enterText(
find.byKey(const Key("input_flightLogModal_fuel")), "102.38");
await tester.pump();
await tester.tap(find.byKey(const Key('button_flightLogModal_save')));
await tester.pump();
expect(find.byType(PlatformAlertDialog), findsOneWidget);
expect(find.text(lang.flightLogModal_error_invalid_fuelCost_empty),
findsOneWidget);
});
});
}
Expand Down

0 comments on commit cf9fc38

Please sign in to comment.