test: fixed tests and coverage

pull/359/head
RuiAlonso 3 years ago
parent b13ad6a06d
commit 79a7440dc3

@ -38,6 +38,20 @@ void main() {
], ],
); );
blocTest<GameBloc, GameState>(
'GameRestarted restarts the game',
build: GameBloc.new,
act: (bloc) => bloc.add(const GameRestarted()),
expect: () => [
isA<GameState>()
..having(
(state) => state.status,
'status',
GameStatus.replaying,
),
],
);
group('RoundLost', () { group('RoundLost', () {
blocTest<GameBloc, GameState>( blocTest<GameBloc, GameState>(
'decreases number of rounds ' 'decreases number of rounds '

@ -98,6 +98,19 @@ void main() {
}); });
}); });
group('GameRestarted', () {
test('can be instantiated', () {
expect(const GameRestarted(), isNotNull);
});
test('supports value equality', () {
expect(
GameRestarted(),
equals(const GameRestarted()),
);
});
});
group('SparkyTurboChargeActivated', () { group('SparkyTurboChargeActivated', () {
test('can be instantiated', () { test('can be instantiated', () {
expect(const SparkyTurboChargeActivated(), isNotNull); expect(const SparkyTurboChargeActivated(), isNotNull);

@ -3,6 +3,7 @@
import 'dart:async'; import 'dart:async';
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart'; import 'package:flame/input.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
@ -20,7 +21,8 @@ import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_flame/pinball_flame.dart';
import 'package:pinball_theme/pinball_theme.dart' as theme; import 'package:pinball_theme/pinball_theme.dart' as theme;
class _TestGame extends Forge2DGame with HasKeyboardHandlerComponents { class _TestGame extends Forge2DGame
with HasKeyboardHandlerComponents, HasTappables {
final character = theme.DashTheme(); final character = theme.DashTheme();
@override @override
@ -33,6 +35,7 @@ class _TestGame extends Forge2DGame with HasKeyboardHandlerComponents {
character.leaderboardIcon.keyName, character.leaderboardIcon.keyName,
Assets.images.backbox.marquee.keyName, Assets.images.backbox.marquee.keyName,
Assets.images.backbox.displayDivider.keyName, Assets.images.backbox.displayDivider.keyName,
Assets.images.backbox.button.share.keyName,
]); ]);
} }
@ -93,6 +96,24 @@ class _MockAppLocalizations extends Mock implements AppLocalizations {
@override @override
String get loading => ''; String get loading => '';
@override
String get shareYourScore => '';
@override
String get andChallengeYourFriends => '';
@override
String get share => '';
@override
String get gotoIO => '';
@override
String get learnMore => '';
@override
String get firebaseOrOpenSource => '';
} }
void main() { void main() {
@ -193,21 +214,23 @@ void main() {
); );
flameTester.test( flameTester.test(
'adds InitialsSubmissionSuccessDisplay on InitialsSuccessState', 'adds InfoDisplay on InitialsSuccessState',
(game) async { (game) async {
final state = InitialsSuccessState(
score: 100,
initials: 'AAA',
character: theme.AndroidTheme(),
);
whenListen( whenListen(
bloc, bloc,
Stream.value(InitialsSuccessState()), Stream.value(state),
initialState: InitialsSuccessState(), initialState: state,
); );
final backbox = Backbox.test(bloc: bloc); final backbox = Backbox.test(bloc: bloc);
await game.pump(backbox); await game.pump(backbox);
expect( expect(
game game.descendants().whereType<InfoDisplay>().length,
.descendants()
.whereType<InitialsSubmissionSuccessDisplay>()
.length,
equals(1), equals(1),
); );
}, },

@ -56,7 +56,11 @@ void main() {
), ),
expect: () => [ expect: () => [
LoadingState(), LoadingState(),
InitialsSuccessState(), InitialsSuccessState(
score: 10,
initials: 'AAA',
character: DashTheme(),
),
], ],
); );
@ -88,5 +92,27 @@ void main() {
], ],
); );
}); });
blocTest<BackboxBloc, BackboxState>(
'adds ShareState on ScoreShareRequested',
setUp: () {
leaderboardRepository = _MockLeaderboardRepository();
},
build: () => BackboxBloc(leaderboardRepository: leaderboardRepository),
act: (bloc) => bloc.add(
ScoreShareRequested(
score: 100,
initials: 'AAA',
character: AndroidTheme(),
),
),
expect: () => [
ShareState(
score: 100,
initials: 'AAA',
character: AndroidTheme(),
),
],
);
}); });
} }

@ -122,5 +122,86 @@ void main() {
); );
}); });
}); });
group('ScoreShareRequested', () {
test('can be instantiated', () {
expect(
ScoreShareRequested(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
isNotNull,
);
});
test('supports value comparison', () {
expect(
ScoreShareRequested(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
equals(
ScoreShareRequested(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
),
);
expect(
ScoreShareRequested(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
isNot(
equals(
ScoreShareRequested(
score: 1,
initials: 'AAA',
character: AndroidTheme(),
),
),
),
);
expect(
ScoreShareRequested(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
isNot(
equals(
ScoreShareRequested(
score: 0,
initials: 'AAA',
character: SparkyTheme(),
),
),
),
);
expect(
ScoreShareRequested(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
isNot(
equals(
ScoreShareRequested(
score: 0,
initials: 'BBB',
character: AndroidTheme(),
),
),
),
);
});
});
}); });
} }

@ -95,11 +95,31 @@ void main() {
group('InitialsSuccessState', () { group('InitialsSuccessState', () {
test('can be instantiated', () { test('can be instantiated', () {
expect(InitialsSuccessState(), isNotNull); expect(
InitialsSuccessState(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
isNotNull,
);
}); });
test('supports value comparison', () { test('supports value comparison', () {
expect(InitialsSuccessState(), equals(InitialsSuccessState())); expect(
InitialsSuccessState(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
equals(
InitialsSuccessState(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
),
);
}); });
group('InitialsFailureState', () { group('InitialsFailureState', () {
@ -111,6 +131,36 @@ void main() {
expect(InitialsFailureState(), equals(InitialsFailureState())); expect(InitialsFailureState(), equals(InitialsFailureState()));
}); });
}); });
group('ShareState', () {
test('can be instantiated', () {
expect(
ShareState(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
isNotNull,
);
});
test('supports value comparison', () {
expect(
ShareState(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
equals(
ShareState(
score: 0,
initials: 'AAA',
character: AndroidTheme(),
),
),
);
});
});
}); });
}); });
} }

@ -0,0 +1,77 @@
// ignore_for_file: cascade_invocations
import 'package:flame/game.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pinball/game/bloc/game_bloc.dart';
import 'package:pinball/game/components/backbox/displays/info_display.dart';
import 'package:pinball/l10n/l10n.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
class _TestGame extends Forge2DGame with HasTappables {
@override
Future<void> onLoad() async {
await super.onLoad();
images.prefix = '';
await images.loadAll(
[
Assets.images.backbox.button.share.keyName,
],
);
}
Future<void> pump(InfoDisplay component) {
return ensureAdd(
FlameBlocProvider<GameBloc, GameState>.value(
value: GameBloc(),
children: [
FlameProvider.value(
_MockAppLocalizations(),
children: [component],
),
],
),
);
}
}
class _MockAppLocalizations extends Mock implements AppLocalizations {
@override
String get shareYourScore => '';
@override
String get andChallengeYourFriends => '';
@override
String get share => '';
@override
String get gotoIO => '';
@override
String get learnMore => '';
@override
String get firebaseOrOpenSource => '';
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(_TestGame.new);
group('InfoDisplay', () {
flameTester.test(
'loads correctly',
(game) async {
final component = InfoDisplay();
await game.pump(component);
expect(game.descendants(), contains(component));
},
);
});
}

@ -0,0 +1,45 @@
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pinball/game/game.dart';
import 'package:pinball/start_game/bloc/start_game_bloc.dart';
import '../../../helpers/helpers.dart';
class _MockStartGameBloc extends Mock implements StartGameBloc {}
void main() {
group('ReplayButtonOverlay', () {
late StartGameBloc startGameBloc;
setUp(() async {
await mockFlameImages();
startGameBloc = _MockStartGameBloc();
whenListen(
startGameBloc,
Stream.value(const StartGameState.initial()),
initialState: const StartGameState.initial(),
);
});
testWidgets('renders correctly', (tester) async {
await tester.pumpApp(const ReplayButtonOverlay());
expect(find.text('Replay'), findsOneWidget);
});
testWidgets('adds ReplayTapped event to StartGameBloc when taped',
(tester) async {
await tester.pumpApp(
const ReplayButtonOverlay(),
startGameBloc: startGameBloc,
);
await tester.tap(find.text('Replay'));
await tester.pump();
verify(() => startGameBloc.add(const ReplayTapped())).called(1);
});
});
}

@ -5,7 +5,7 @@ import 'package:pinball/start_game/bloc/start_game_bloc.dart';
void main() { void main() {
group('StartGameBloc', () { group('StartGameBloc', () {
blocTest<StartGameBloc, StartGameState>( blocTest<StartGameBloc, StartGameState>(
'on PlayTapped changes status to selectCharacter', 'on PlayTapped changes status to selectCharacter and restarted false',
build: StartGameBloc.new, build: StartGameBloc.new,
act: (bloc) => bloc.add(const PlayTapped()), act: (bloc) => bloc.add(const PlayTapped()),
expect: () => [ expect: () => [
@ -15,6 +15,18 @@ void main() {
], ],
); );
blocTest<StartGameBloc, StartGameState>(
'on ReplayTapped changes status to selectCharacter and restarted enabled',
build: StartGameBloc.new,
act: (bloc) => bloc.add(const ReplayTapped()),
expect: () => [
const StartGameState(
status: StartGameStatus.selectCharacter,
restarted: true,
)
],
);
blocTest<StartGameBloc, StartGameState>( blocTest<StartGameBloc, StartGameState>(
'on CharacterSelected changes status to howToPlay', 'on CharacterSelected changes status to howToPlay',
build: StartGameBloc.new, build: StartGameBloc.new,

@ -12,6 +12,13 @@ void main() {
); );
}); });
test('ReplayTapped supports value equality', () {
expect(
ReplayTapped(),
equals(ReplayTapped()),
);
});
test('CharacterSelected supports value equality', () { test('CharacterSelected supports value equality', () {
expect( expect(
CharacterSelected(), CharacterSelected(),

@ -25,10 +25,43 @@ void main() {
expect(testState, secondState); expect(testState, secondState);
}); });
test('supports copyWith', () { group('copyWith', () {
final secondState = testState.copyWith(); test(
'copies correctly '
'when no argument specified',
() {
const state = StartGameState(
status: StartGameStatus.initial,
);
expect(
state.copyWith(),
equals(state),
);
},
);
expect(testState, secondState); test(
'copies correctly '
'when all arguments specified',
() {
const state = StartGameState(
status: StartGameStatus.initial,
);
final otherState = StartGameState(
status: StartGameStatus.play,
restarted: true,
);
expect(state, isNot(equals(otherState)));
expect(
state.copyWith(
status: otherState.status,
restarted: otherState.restarted,
),
equals(otherState),
);
},
);
}); });
test('has correct props', () { test('has correct props', () {
@ -36,6 +69,7 @@ void main() {
testState.props, testState.props,
equals([ equals([
StartGameStatus.selectCharacter, StartGameStatus.selectCharacter,
false,
]), ]),
); );
}); });

@ -46,12 +46,14 @@ void main() {
}); });
testWidgets( testWidgets(
'calls onGameStarted event', 'calls only GameStarted event on play',
(tester) async { (tester) async {
whenListen( whenListen(
startGameBloc, startGameBloc,
Stream.value( Stream.value(
const StartGameState(status: StartGameStatus.selectCharacter), const StartGameState(
status: StartGameStatus.selectCharacter,
),
), ),
initialState: const StartGameState.initial(), initialState: const StartGameState.initial(),
); );
@ -64,10 +66,38 @@ void main() {
startGameBloc: startGameBloc, startGameBloc: startGameBloc,
); );
verifyNever(() => gameBloc.add(const GameRestarted()));
verify(() => gameBloc.add(const GameStarted())).called(1); verify(() => gameBloc.add(const GameStarted())).called(1);
}, },
); );
testWidgets(
'calls only GameRestarted event on replay',
(tester) async {
whenListen(
startGameBloc,
Stream.value(
const StartGameState(
status: StartGameStatus.selectCharacter,
restarted: true,
),
),
initialState: const StartGameState.initial(),
);
await tester.pumpApp(
const StartGameListener(
child: SizedBox.shrink(),
),
gameBloc: gameBloc,
startGameBloc: startGameBloc,
);
verifyNever(() => gameBloc.add(const GameStarted()));
verify(() => gameBloc.add(const GameRestarted())).called(1);
},
);
testWidgets( testWidgets(
'shows SelectCharacter dialog', 'shows SelectCharacter dialog',
(tester) async { (tester) async {

Loading…
Cancel
Save