Skip to content

Commit

Permalink
Publish 1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed Feb 7, 2020
1 parent 9927de1 commit 74df147
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 16 deletions.
58 changes: 46 additions & 12 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.2 - 07 February 2020

* Remove `assert(child != null)` and `@required child` in `BlocProvider` constructor

## 1.1.1 - 07 February 2020

* Add `DisposeCallbackBaseBloc`
Expand Down
5 changes: 2 additions & 3 deletions lib/flutter_bloc_pattern.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ class BlocProvider<T extends BaseBloc> extends StatefulWidget {
const BlocProvider({
Key key,
@required this.initBloc,
@required this.child,
}) : assert(child != null),
super(key: key);
this.child,
}) : super(key: key);

@override
_BlocProviderState<T> createState() => _BlocProviderState<T>();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_bloc_pattern
description: Bloc provider. Provides bloc to descendant widget (O(1)), and the bloc is disposed appropriately by state which the bloc_provider holds internally.
version: 1.1.1
version: 1.1.2
homepage: https://github.com/hoc081098/flutter_bloc_pattern.git
repository: https://github.com/hoc081098/flutter_bloc_pattern.git
issue_tracker: https://github.com/hoc081098/flutter_bloc_pattern/issues
Expand Down
89 changes: 89 additions & 0 deletions test/flutter_bloc_pattern_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import 'package:rxdart/rxdart.dart';

class MockBloc extends Mock implements BaseBloc {}

class BlocA extends Mock implements BaseBloc {}

class BlocB extends Mock implements BaseBloc {}

class BlocC extends Mock implements DisposeCallbackBaseBloc {}

class ValueBuilder<T extends BaseBloc> extends Mock {
T call();
}
Expand Down Expand Up @@ -133,4 +139,87 @@ void main() {
);
});
});

group('BlocProviders', () {
testWidgets('Empty bloc providers returns child', (tester) async {
await tester.pumpWidget(
BlocProviders(
child: Text(
'Hello',
textDirection: TextDirection.ltr,
),
blocProviders: [],
),
);

expect(find.text('Hello'), findsOneWidget);
});

testWidgets('Children can only access parent providers', (tester) async {
final isBlocProviderError = throwsA(isA<BlocProviderError>());

final k1 = GlobalKey();
final k2 = GlobalKey();
final k3 = GlobalKey();
final blocA = BlocA();
final blocB = BlocB();
final blocC = BlocC();
final keyChild = GlobalKey();

final p1 = BlocProvider<BlocA>(key: k1, initBloc: () => blocA);
final p2 = BlocProvider<BlocB>(key: k2, initBloc: () => blocB);
final p3 = BlocProvider<BlocC>(key: k3, initBloc: () => blocC);

await tester.pumpWidget(
BlocProviders(
blocProviders: [p1, p2, p3],
child: Text(
'Foo',
key: keyChild,
textDirection: TextDirection.ltr,
),
),
);

expect(find.text('Foo'), findsOneWidget);

// p1 cannot access to p1, p2 and p3
expect(
() => BlocProvider.of<BlocA>(k1.currentContext),
isBlocProviderError,
);
expect(
() => BlocProvider.of<BlocB>(k1.currentContext),
isBlocProviderError,
);
expect(
() => BlocProvider.of<BlocC>(k1.currentContext),
isBlocProviderError,
);

// p2 can access only p1
expect(BlocProvider.of<BlocA>(k2.currentContext), blocA);
expect(
() => BlocProvider.of<BlocB>(k2.currentContext),
isBlocProviderError,
);
expect(
() => BlocProvider.of<BlocC>(k2.currentContext),
isBlocProviderError,
);

// p3 can access both p1 and p2
expect(BlocProvider.of<BlocA>(k3.currentContext), blocA);
expect(BlocProvider.of<BlocB>(k3.currentContext), blocB);
expect(
() => BlocProvider.of<BlocC>(k3.currentContext),
isBlocProviderError,
);

// the child can access them all
expect(BlocProvider.of<BlocA>(keyChild.currentContext), blocA);
expect(BlocProvider.of<BlocB>(keyChild.currentContext), blocB);
expect(BlocProvider.of<BlocC>(keyChild.currentContext), blocC);
});
});
}

0 comments on commit 74df147

Please sign in to comment.