import 'package:bloc_test/bloc_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pinball/game/game.dart'; void main() { group('GameBloc', () { test('initial state has 3 rounds and empty score', () { final gameBloc = GameBloc(); expect(gameBloc.state.score, equals(0)); expect(gameBloc.state.rounds, equals(3)); }); group('RoundLost', () { blocTest( 'decreases number of rounds ' 'when there are already available rounds', build: GameBloc.new, act: (bloc) { bloc.add(const RoundLost()); }, expect: () => [ const GameState( score: 0, multiplier: 1, rounds: 2, bonusHistory: [], ), ], ); blocTest( 'apply multiplier to score ' 'when round is lost', build: GameBloc.new, seed: () => const GameState( score: 5, multiplier: 3, rounds: 2, bonusHistory: [], ), act: (bloc) { bloc.add(const RoundLost()); }, expect: () => [ isA() ..having((state) => state.score, 'score', 15) ..having((state) => state.rounds, 'rounds', 1), ], ); blocTest( 'resets multiplier ' 'when round is lost', build: GameBloc.new, seed: () => const GameState( score: 5, multiplier: 3, rounds: 2, bonusHistory: [], ), act: (bloc) { bloc.add(const RoundLost()); }, expect: () => [ isA() ..having((state) => state.multiplier, 'multiplier', 1) ..having((state) => state.rounds, 'rounds', 1), ], ); }); group('Scored', () { blocTest( 'increases score ' 'when game is not over', build: GameBloc.new, act: (bloc) => bloc ..add(const Scored(points: 2)) ..add(const Scored(points: 3)), expect: () => [ isA() ..having((state) => state.score, 'score', 2) ..having((state) => state.isGameOver, 'isGameOver', false), isA() ..having((state) => state.score, 'score', 5) ..having((state) => state.isGameOver, 'isGameOver', false), ], ); blocTest( "doesn't increase score " 'when game is over', build: GameBloc.new, act: (bloc) { for (var i = 0; i < bloc.state.rounds; i++) { bloc.add(const RoundLost()); } bloc.add(const Scored(points: 2)); }, expect: () => [ isA() ..having((state) => state.score, 'score', 0) ..having((state) => state.rounds, 'rounds', 2) ..having((state) => state.isGameOver, 'isGameOver', false), isA() ..having((state) => state.score, 'score', 0) ..having((state) => state.rounds, 'rounds', 1) ..having((state) => state.isGameOver, 'isGameOver', false), isA() ..having((state) => state.score, 'score', 0) ..having((state) => state.rounds, 'rounds', 0) ..having((state) => state.isGameOver, 'isGameOver', true), ], ); }); group('MultiplierIncreased', () { blocTest( 'increases multiplier ' 'when multiplier is below 6 and game is not over', build: GameBloc.new, act: (bloc) => bloc ..add(const MultiplierIncreased()) ..add(const MultiplierIncreased()), expect: () => [ isA() ..having((state) => state.score, 'score', 0) ..having((state) => state.multiplier, 'multiplier', 2) ..having((state) => state.isGameOver, 'isGameOver', false), isA() ..having((state) => state.score, 'score', 0) ..having((state) => state.multiplier, 'multiplier', 3) ..having((state) => state.isGameOver, 'isGameOver', false), ], ); blocTest( "doesn't increase multiplier " 'when multiplier is 6 and game is not over', build: GameBloc.new, seed: () => const GameState( score: 0, multiplier: 6, rounds: 3, bonusHistory: [], ), act: (bloc) => bloc..add(const MultiplierIncreased()), expect: () => const [], ); blocTest( "doesn't increase multiplier " 'when game is over', build: GameBloc.new, act: (bloc) { for (var i = 0; i < bloc.state.rounds; i++) { bloc.add(const RoundLost()); } bloc.add(const MultiplierIncreased()); }, expect: () => [ isA() ..having((state) => state.score, 'score', 0) ..having((state) => state.multiplier, 'multiplier', 1) ..having((state) => state.isGameOver, 'isGameOver', false), isA() ..having((state) => state.score, 'score', 0) ..having((state) => state.multiplier, 'multiplier', 1) ..having((state) => state.isGameOver, 'isGameOver', false), isA() ..having((state) => state.score, 'score', 0) ..having((state) => state.multiplier, 'multiplier', 1) ..having((state) => state.isGameOver, 'isGameOver', true), ], ); }); group( 'BonusActivated', () { blocTest( 'adds bonus to history', build: GameBloc.new, act: (bloc) => bloc ..add(const BonusActivated(GameBonus.googleWord)) ..add(const BonusActivated(GameBonus.dashNest)), expect: () => [ isA() ..having( (state) => state.bonusHistory, 'bonusHistory', [GameBonus.googleWord], ), isA() ..having( (state) => state.bonusHistory, 'bonusHistory', [GameBonus.googleWord, GameBonus.dashNest], ), ], ); }, ); group('SparkyTurboChargeActivated', () { blocTest( 'adds game bonus', build: GameBloc.new, act: (bloc) => bloc..add(const SparkyTurboChargeActivated()), expect: () => [ isA() ..having( (state) => state.bonusHistory, 'bonusHistory', [GameBonus.sparkyTurboCharge], ), ], ); }); }); }