From b9050d4c08a1aab067a7e9968ffbbed5919c1e6d Mon Sep 17 00:00:00 2001 From: Allison Ryan Date: Wed, 4 May 2022 20:22:19 -0500 Subject: [PATCH] refactor: switched method --- .../behaviors/skill_shot_blinking_behavior.dart | 6 +----- .../skill_shot/cubit/skill_shot_cubit.dart | 15 +++++++++------ .../skill_shot_blinking_behavior_test.dart | 8 ++++---- .../skill_shot/cubit/skill_shot_cubit_test.dart | 12 ++++++++---- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/pinball_components/lib/src/components/skill_shot/behaviors/skill_shot_blinking_behavior.dart b/packages/pinball_components/lib/src/components/skill_shot/behaviors/skill_shot_blinking_behavior.dart index d56c68ee..ea62fc25 100644 --- a/packages/pinball_components/lib/src/components/skill_shot/behaviors/skill_shot_blinking_behavior.dart +++ b/packages/pinball_components/lib/src/components/skill_shot/behaviors/skill_shot_blinking_behavior.dart @@ -33,11 +33,7 @@ class SkillShotBlinkingBehavior extends TimerComponent void onTick() { super.onTick(); if (_blinks != _maxBlinks * 2) { - if (parent.bloc.state.spriteState == SkillShotSpriteState.lit) { - parent.bloc.onBlinkedOn(); - } else { - parent.bloc.onBlinkedOff(); - } + parent.bloc.switched(); _blinks++; } else { _blinks = 0; diff --git a/packages/pinball_components/lib/src/components/skill_shot/cubit/skill_shot_cubit.dart b/packages/pinball_components/lib/src/components/skill_shot/cubit/skill_shot_cubit.dart index f5817c54..b9491385 100644 --- a/packages/pinball_components/lib/src/components/skill_shot/cubit/skill_shot_cubit.dart +++ b/packages/pinball_components/lib/src/components/skill_shot/cubit/skill_shot_cubit.dart @@ -17,12 +17,15 @@ class SkillShotCubit extends Cubit { ); } - void onBlinkedOff() { - emit(state.copyWith(spriteState: SkillShotSpriteState.lit)); - } - - void onBlinkedOn() { - emit(state.copyWith(spriteState: SkillShotSpriteState.dimmed)); + void switched() { + switch (state.spriteState) { + case SkillShotSpriteState.lit: + emit(state.copyWith(spriteState: SkillShotSpriteState.dimmed)); + break; + case SkillShotSpriteState.dimmed: + emit(state.copyWith(spriteState: SkillShotSpriteState.lit)); + break; + } } void onBlinkingFinished() { diff --git a/packages/pinball_components/test/src/components/skill_shot/behaviors/skill_shot_blinking_behavior_test.dart b/packages/pinball_components/test/src/components/skill_shot/behaviors/skill_shot_blinking_behavior_test.dart index acd01fad..e2d00f61 100644 --- a/packages/pinball_components/test/src/components/skill_shot/behaviors/skill_shot_blinking_behavior_test.dart +++ b/packages/pinball_components/test/src/components/skill_shot/behaviors/skill_shot_blinking_behavior_test.dart @@ -21,7 +21,7 @@ void main() { 'SkillShotBlinkingBehavior', () { flameTester.testGameWidget( - 'calls onBlinkedOn after 0.15 seconds when isBlinking and lit', + 'calls switched after 0.15 seconds when isBlinking and lit', setUp: (game, tester) async { final behavior = SkillShotBlinkingBehavior(); final bloc = _MockSkillShotCubit(); @@ -46,12 +46,12 @@ void main() { game.update(0.15); await streamController.close(); - verify(bloc.onBlinkedOn).called(1); + verify(bloc.switched).called(1); }, ); flameTester.testGameWidget( - 'calls onBlinkedOff after 0.15 seconds when isBlinking and dimmed', + 'calls switched after 0.15 seconds when isBlinking and dimmed', setUp: (game, tester) async { final behavior = SkillShotBlinkingBehavior(); final bloc = _MockSkillShotCubit(); @@ -76,7 +76,7 @@ void main() { game.update(0.15); await streamController.close(); - verify(bloc.onBlinkedOff).called(1); + verify(bloc.switched).called(1); }, ); diff --git a/packages/pinball_components/test/src/components/skill_shot/cubit/skill_shot_cubit_test.dart b/packages/pinball_components/test/src/components/skill_shot/cubit/skill_shot_cubit_test.dart index 52fbc867..b165db99 100644 --- a/packages/pinball_components/test/src/components/skill_shot/cubit/skill_shot_cubit_test.dart +++ b/packages/pinball_components/test/src/components/skill_shot/cubit/skill_shot_cubit_test.dart @@ -21,9 +21,9 @@ void main() { ); blocTest( - 'onBlinkedOff emits lit', + 'switched emits lit when dimmed', build: SkillShotCubit.new, - act: (bloc) => bloc.onBlinkedOff(), + act: (bloc) => bloc.switched(), expect: () => [ isA().having( (state) => state.spriteState, @@ -34,9 +34,13 @@ void main() { ); blocTest( - 'onBlinkedOn emits dimmed', + 'switched emits dimmed when lit', build: SkillShotCubit.new, - act: (bloc) => bloc.onBlinkedOn(), + seed: () => SkillShotState( + spriteState: SkillShotSpriteState.lit, + isBlinking: false, + ), + act: (bloc) => bloc.switched(), expect: () => [ isA().having( (state) => state.spriteState,