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) {
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<GameOverDialogView> {
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<LeaderboardBloc, LeaderboardState>(
builder: (context, state) {
switch (state.status) {
case LeaderboardStatus.loading:
return TextButton(
onPressed: () {
context.read<LeaderboardBloc>().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<void>(
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<GameOverDialogView> {
);
}
}
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) {
switch (state.status) {
case LeaderboardStatus.loading:
return TextButton(
onPressed: () {
context.read<LeaderboardBloc>().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<void>(
LeaderboardPage.route(theme: theme),
),
child: Text(l10n.leaderboard),
);
case LeaderboardStatus.error:
return Text(l10n.error);
}
},
);
}
}

@ -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"
}
}

@ -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<LeaderboardPage>(
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<void>(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<void>(any())).called(1);
expect(find.text(l10n.leaderboard), findsOneWidget);
});
});
});

Loading…
Cancel
Save