test: fixed tests

pull/98/head
RuiAlonso 4 years ago
parent d628771360
commit 1f08a5ea8a

@ -54,6 +54,7 @@ class _GameOverDialogViewState extends State<GameOverDialogView> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final l10n = context.l10n; final l10n = context.l10n;
// TODO(ruimiguel): refactor this view once UI design finished.
return Dialog( return Dialog(
child: SizedBox( child: SizedBox(
width: 200, width: 200,
@ -61,18 +62,19 @@ class _GameOverDialogViewState extends State<GameOverDialogView> {
child: Center( child: Center(
child: Padding( child: Padding(
padding: const EdgeInsets.all(10), padding: const EdgeInsets.all(10),
child: SingleChildScrollView(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Text( Text(
'Game Over', l10n.gameOver,
style: Theme.of(context).textTheme.headline4, style: Theme.of(context).textTheme.headline4,
), ),
const SizedBox( const SizedBox(
height: 20, height: 20,
), ),
Text( Text(
'Your score is ${widget.score}', '${l10n.yourScore} ${widget.score}',
style: Theme.of(context).textTheme.headline6, style: Theme.of(context).textTheme.headline6,
), ),
const SizedBox( const SizedBox(
@ -81,17 +83,48 @@ class _GameOverDialogViewState extends State<GameOverDialogView> {
TextField( TextField(
controller: playerInitialsInputController, controller: playerInitialsInputController,
textCapitalization: TextCapitalization.characters, textCapitalization: TextCapitalization.characters,
decoration: const InputDecoration( decoration: InputDecoration(
border: OutlineInputBorder(), border: const OutlineInputBorder(),
hintText: 'Enter your initials', hintText: l10n.enterInitials,
), ),
maxLength: 3, maxLength: 3,
), ),
const SizedBox( const SizedBox(
height: 10, height: 10,
), ),
// TODO(ruimiguel): refactor this view once UI design finished. _GameOverDialogActions(
BlocBuilder<LeaderboardBloc, LeaderboardState>( score: widget.score,
theme: widget.theme,
playerInitialsInputController:
playerInitialsInputController,
),
],
),
),
),
),
),
);
}
}
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<LeaderboardBloc, LeaderboardState>(
builder: (context, state) { builder: (context, state) {
switch (state.status) { switch (state.status) {
case LeaderboardStatus.loading: case LeaderboardStatus.loading:
@ -101,33 +134,26 @@ class _GameOverDialogViewState extends State<GameOverDialogView> {
LeaderboardEntryAdded( LeaderboardEntryAdded(
entry: LeaderboardEntryData( entry: LeaderboardEntryData(
playerInitials: playerInitials:
playerInitialsInputController.text playerInitialsInputController.text.toUpperCase(),
.toUpperCase(), score: score,
score: widget.score, character: theme.toType,
character: widget.theme.toType,
), ),
), ),
); );
}, },
child: const Text('Add User'), child: Text(l10n.addUser),
); );
case LeaderboardStatus.success: case LeaderboardStatus.success:
return TextButton( return TextButton(
onPressed: () => Navigator.of(context).push<void>( onPressed: () => Navigator.of(context).push<void>(
LeaderboardPage.route(theme: widget.theme), LeaderboardPage.route(theme: theme),
), ),
child: Text(l10n.leaderboard), child: Text(l10n.leaderboard),
); );
case LeaderboardStatus.error: case LeaderboardStatus.error:
return const Text('error'); return Text(l10n.error);
} }
}, },
),
],
),
),
),
),
); );
} }
} }

@ -51,5 +51,21 @@
"retry": "Retry", "retry": "Retry",
"@retry": { "@retry": {
"description": "Text displayed on the retry button leaders board page" "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"
} }
} }

@ -1,7 +1,9 @@
// ignore_for_file: prefer_const_constructors // ignore_for_file: prefer_const_constructors
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:leaderboard_repository/leaderboard_repository.dart';
import 'package:mockingjay/mockingjay.dart'; import 'package:mockingjay/mockingjay.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart'; import 'package:pinball/l10n/l10n.dart';
@ -24,34 +26,82 @@ void main() {
}); });
group('GameOverDialogView', () { group('GameOverDialogView', () {
testWidgets('route returns a valid navigation route', (tester) async { late LeaderboardBloc leaderboardBloc;
await expectNavigatesToRoute<LeaderboardPage>(
tester, setUp(() {
LeaderboardPage.route( 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(), 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', testWidgets('renders error view when bloc emits [error]', (tester) async {
(tester) async {
final l10n = await AppLocalizations.delegate.load(Locale('en')); final l10n = await AppLocalizations.delegate.load(Locale('en'));
final navigator = MockNavigator();
when(() => navigator.push<void>(any())).thenAnswer((_) async {}); when(() => leaderboardBloc.state).thenReturn(
LeaderboardState.initial().copyWith(status: LeaderboardStatus.error),
);
await tester.pumpApp( await tester.pumpApp(
const GameOverDialogView( BlocProvider.value(
score: 1000, value: leaderboardBloc,
child: GameOverDialogView(
score: 10000,
theme: DashTheme(), 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,
),
],
),
);
verify(() => navigator.push<void>(any())).called(1); await tester.pumpApp(
BlocProvider.value(
value: leaderboardBloc,
child: GameOverDialogView(
score: 10000,
theme: DashTheme(),
),
),
);
expect(find.text(l10n.leaderboard), findsOneWidget);
}); });
}); });
}); });

Loading…
Cancel
Save