feat(pinball_flame): add readBloc extension (#374)

pull/382/head
Felix Angelov 3 years ago committed by GitHub
parent 4f5ef13702
commit 480b08d3c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,8 @@
// ignore_for_file: public_member_api_docs // ignore_for_file: public_member_api_docs
import 'package:bloc/bloc.dart';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame_bloc/flame_bloc.dart';
class FlameProvider<T> extends Component { class FlameProvider<T> extends Component {
FlameProvider.value( FlameProvider.value(
@ -63,3 +65,15 @@ extension ReadFlameProvider on Component {
return providers.first.provider; return providers.first.provider;
} }
} }
extension ReadFlameBlocProvider on Component {
B readBloc<B extends BlocBase<S>, S>() {
final providers = ancestors().whereType<FlameBlocProvider<B, S>>();
assert(
providers.isNotEmpty,
'No FlameBlocProvider<$B, $S> available on the component tree',
);
return providers.first.bloc;
}
}

@ -7,7 +7,9 @@ environment:
sdk: ">=2.16.0 <3.0.0" sdk: ">=2.16.0 <3.0.0"
dependencies: dependencies:
bloc: ^8.0.0
flame: ^1.1.1 flame: ^1.1.1
flame_bloc: ^1.4.0
flame_forge2d: flame_forge2d:
git: git:
url: https://github.com/flame-engine/flame url: https://github.com/flame-engine/flame

@ -1,11 +1,15 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'package:bloc/bloc.dart';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_flame/pinball_flame.dart';
class _FakeCubit extends Fake implements Cubit<Object> {}
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(FlameGame.new); 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,
);
},
);
},
);
} }

Loading…
Cancel
Save