From 480b08d3c25b52d06eb525d81abc388976cce26b Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Fri, 6 May 2022 17:48:00 -0500 Subject: [PATCH] feat(pinball_flame): add readBloc extension (#374) --- .../pinball_flame/lib/src/flame_provider.dart | 14 ++++++++ packages/pinball_flame/pubspec.yaml | 2 ++ .../test/src/flame_provider_test.dart | 33 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/packages/pinball_flame/lib/src/flame_provider.dart b/packages/pinball_flame/lib/src/flame_provider.dart index 35afb0a5..85a1004c 100644 --- a/packages/pinball_flame/lib/src/flame_provider.dart +++ b/packages/pinball_flame/lib/src/flame_provider.dart @@ -1,6 +1,8 @@ // ignore_for_file: public_member_api_docs +import 'package:bloc/bloc.dart'; import 'package:flame/components.dart'; +import 'package:flame_bloc/flame_bloc.dart'; class FlameProvider extends Component { FlameProvider.value( @@ -63,3 +65,15 @@ extension ReadFlameProvider on Component { return providers.first.provider; } } + +extension ReadFlameBlocProvider on Component { + B readBloc, S>() { + final providers = ancestors().whereType>(); + assert( + providers.isNotEmpty, + 'No FlameBlocProvider<$B, $S> available on the component tree', + ); + + return providers.first.bloc; + } +} diff --git a/packages/pinball_flame/pubspec.yaml b/packages/pinball_flame/pubspec.yaml index 125e7757..4639a080 100644 --- a/packages/pinball_flame/pubspec.yaml +++ b/packages/pinball_flame/pubspec.yaml @@ -7,7 +7,9 @@ environment: sdk: ">=2.16.0 <3.0.0" dependencies: + bloc: ^8.0.0 flame: ^1.1.1 + flame_bloc: ^1.4.0 flame_forge2d: git: url: https://github.com/flame-engine/flame diff --git a/packages/pinball_flame/test/src/flame_provider_test.dart b/packages/pinball_flame/test/src/flame_provider_test.dart index cfc10613..71d69e0c 100644 --- a/packages/pinball_flame/test/src/flame_provider_test.dart +++ b/packages/pinball_flame/test/src/flame_provider_test.dart @@ -1,11 +1,15 @@ // ignore_for_file: cascade_invocations +import 'package:bloc/bloc.dart'; import 'package:flame/components.dart'; import 'package:flame/game.dart'; +import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pinball_flame/pinball_flame.dart'; +class _FakeCubit extends Fake implements Cubit {} + void main() { TestWidgetsFlutterBinding.ensureInitialized(); final flameTester = FlameTester(FlameGame.new); @@ -100,4 +104,33 @@ void main() { ); }, ); + + group( + 'ReadFlameBlocProvider', + () { + flameTester.test('loads provider', (game) async { + final component = Component(); + final bloc = _FakeCubit(); + final provider = FlameBlocProvider<_FakeCubit, Object>.value( + value: bloc, + children: [component], + ); + await game.ensureAdd(provider); + expect(component.readBloc<_FakeCubit, Object>(), equals(bloc)); + }); + + flameTester.test( + 'throws assertionError when no provider is found', + (game) async { + final component = Component(); + await game.ensureAdd(component); + + expect( + () => component.readBloc<_FakeCubit, Object>(), + throwsAssertionError, + ); + }, + ); + }, + ); }