From 1f08a5ea8afeabaeec764e00288bfb91668f6b49 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Fri, 25 Mar 2022 14:14:33 +0100 Subject: [PATCH] test: fixed tests --- lib/game/view/widgets/game_over_dialog.dart | 150 ++++++++++-------- lib/l10n/arb/app_en.arb | 16 ++ .../view/widgets/game_over_dialog_test.dart | 84 ++++++++-- 3 files changed, 171 insertions(+), 79 deletions(-) diff --git a/lib/game/view/widgets/game_over_dialog.dart b/lib/game/view/widgets/game_over_dialog.dart index 13980e6b..5cfb2e3e 100644 --- a/lib/game/view/widgets/game_over_dialog.dart +++ b/lib/game/view/widgets/game_over_dialog.dart @@ -54,6 +54,7 @@ class _GameOverDialogViewState extends State { Widget build(BuildContext context) { final l10n = context.l10n; + // TODO(ruimiguel): refactor this view once UI design finished. return Dialog( child: SizedBox( width: 200, @@ -61,69 +62,44 @@ class _GameOverDialogViewState extends State { child: Center( child: Padding( padding: const EdgeInsets.all(10), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'Game Over', - style: Theme.of(context).textTheme.headline4, - ), - const SizedBox( - height: 20, - ), - Text( - 'Your score is ${widget.score}', - style: Theme.of(context).textTheme.headline6, - ), - const SizedBox( - height: 15, - ), - TextField( - controller: playerInitialsInputController, - textCapitalization: TextCapitalization.characters, - decoration: const InputDecoration( - border: OutlineInputBorder(), - hintText: 'Enter your initials', + child: SingleChildScrollView( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + l10n.gameOver, + style: Theme.of(context).textTheme.headline4, ), - maxLength: 3, - ), - const SizedBox( - height: 10, - ), - // TODO(ruimiguel): refactor this view once UI design finished. - BlocBuilder( - builder: (context, state) { - switch (state.status) { - case LeaderboardStatus.loading: - return TextButton( - onPressed: () { - context.read().add( - LeaderboardEntryAdded( - entry: LeaderboardEntryData( - playerInitials: - playerInitialsInputController.text - .toUpperCase(), - score: widget.score, - character: widget.theme.toType, - ), - ), - ); - }, - child: const Text('Add User'), - ); - case LeaderboardStatus.success: - return TextButton( - onPressed: () => Navigator.of(context).push( - LeaderboardPage.route(theme: widget.theme), - ), - child: Text(l10n.leaderboard), - ); - case LeaderboardStatus.error: - return const Text('error'); - } - }, - ), - ], + const SizedBox( + height: 20, + ), + Text( + '${l10n.yourScore} ${widget.score}', + style: Theme.of(context).textTheme.headline6, + ), + const SizedBox( + height: 15, + ), + TextField( + controller: playerInitialsInputController, + textCapitalization: TextCapitalization.characters, + decoration: InputDecoration( + border: const OutlineInputBorder(), + hintText: l10n.enterInitials, + ), + maxLength: 3, + ), + const SizedBox( + height: 10, + ), + _GameOverDialogActions( + score: widget.score, + theme: widget.theme, + playerInitialsInputController: + playerInitialsInputController, + ), + ], + ), ), ), ), @@ -131,3 +107,53 @@ class _GameOverDialogViewState extends State { ); } } + +class _GameOverDialogActions extends StatelessWidget { + const _GameOverDialogActions({ + Key? key, + required this.score, + required this.theme, + required this.playerInitialsInputController, + }) : super(key: key); + + final int score; + final CharacterTheme theme; + final TextEditingController playerInitialsInputController; + + @override + Widget build(BuildContext context) { + final l10n = context.l10n; + + return BlocBuilder( + builder: (context, state) { + switch (state.status) { + case LeaderboardStatus.loading: + return TextButton( + onPressed: () { + context.read().add( + LeaderboardEntryAdded( + entry: LeaderboardEntryData( + playerInitials: + playerInitialsInputController.text.toUpperCase(), + score: score, + character: theme.toType, + ), + ), + ); + }, + child: Text(l10n.addUser), + ); + case LeaderboardStatus.success: + return TextButton( + onPressed: () => Navigator.of(context).push( + LeaderboardPage.route(theme: theme), + ), + child: Text(l10n.leaderboard), + ); + case LeaderboardStatus.error: + return Text(l10n.error); + } + }, + ); + } +} diff --git a/lib/l10n/arb/app_en.arb b/lib/l10n/arb/app_en.arb index 235c8f2e..aa56e015 100644 --- a/lib/l10n/arb/app_en.arb +++ b/lib/l10n/arb/app_en.arb @@ -51,5 +51,21 @@ "retry": "Retry", "@retry": { "description": "Text displayed on the retry button leaders board page" + }, + "addUser": "Add User", + "@addUser": { + "description": "Text displayed on the add user button at ending dialog" + }, + "error": "Error", + "@error": { + "description": "Text displayed on the ending dialog when there is any error on sending user" + }, + "yourScore": "Your score is", + "@yourScore": { + "description": "Text displayed on the ending dialog when game finishes to show the final score" + }, + "enterInitials": "Enter your initials", + "@enterInitials": { + "description": "Text displayed on the ending dialog when game finishes to ask the user for his initials" } } \ No newline at end of file diff --git a/test/game/view/widgets/game_over_dialog_test.dart b/test/game/view/widgets/game_over_dialog_test.dart index 5768555e..69cf555c 100644 --- a/test/game/view/widgets/game_over_dialog_test.dart +++ b/test/game/view/widgets/game_over_dialog_test.dart @@ -1,7 +1,9 @@ // ignore_for_file: prefer_const_constructors import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:leaderboard_repository/leaderboard_repository.dart'; import 'package:mockingjay/mockingjay.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball/l10n/l10n.dart'; @@ -24,34 +26,82 @@ void main() { }); group('GameOverDialogView', () { - testWidgets('route returns a valid navigation route', (tester) async { - await expectNavigatesToRoute( - tester, - LeaderboardPage.route( - theme: DashTheme(), + late LeaderboardBloc leaderboardBloc; + + setUp(() { + leaderboardBloc = MockLeaderboardBloc(); + }); + + testWidgets('renders input text view when bloc emits [loading]', + (tester) async { + final l10n = await AppLocalizations.delegate.load(Locale('en')); + + when(() => leaderboardBloc.state) + .thenReturn(LeaderboardState.initial()); + + await tester.pumpApp( + BlocProvider.value( + value: leaderboardBloc, + child: GameOverDialogView( + score: 10000, + theme: DashTheme(), + ), ), ); - expect(find.text(l10n.gameOver), findsOneWidget); - expect(find.text(l10n.leaderboard), findsOneWidget); + + expect(find.text(l10n.addUser), findsOneWidget); }); - testWidgets('tapping on leaderboard button navigates to LeaderBoardPage', - (tester) async { + testWidgets('renders error view when bloc emits [error]', (tester) async { final l10n = await AppLocalizations.delegate.load(Locale('en')); - final navigator = MockNavigator(); - when(() => navigator.push(any())).thenAnswer((_) async {}); + + when(() => leaderboardBloc.state).thenReturn( + LeaderboardState.initial().copyWith(status: LeaderboardStatus.error), + ); await tester.pumpApp( - const GameOverDialogView( - score: 1000, - theme: DashTheme(), + BlocProvider.value( + value: leaderboardBloc, + child: GameOverDialogView( + score: 10000, + theme: DashTheme(), + ), ), - navigator: navigator, ); - await tester.tap(find.widgetWithText(TextButton, l10n.leaderboard)); + expect(find.text(l10n.error), findsOneWidget); + }); + + testWidgets('renders success view when bloc emits [success]', + (tester) async { + final l10n = await AppLocalizations.delegate.load(Locale('en')); + + when(() => leaderboardBloc.state).thenReturn( + LeaderboardState( + status: LeaderboardStatus.success, + ranking: LeaderboardRanking(ranking: 1, outOf: 2), + leaderboard: [ + LeaderboardEntry( + rank: '1', + playerInitials: 'ABC', + score: 5000, + character: DashTheme().characterAsset, + ), + ], + ), + ); + + await tester.pumpApp( + BlocProvider.value( + value: leaderboardBloc, + child: GameOverDialogView( + score: 10000, + theme: DashTheme(), + ), + ), + ); - verify(() => navigator.push(any())).called(1); + expect(find.text(l10n.leaderboard), findsOneWidget); }); }); });