Baseflow / Baseflow/flutter_cached_network_image
Make cached_network_image friendly with widget testing
- Dominant language
- Dart
- Stars
- 2.6k
- Forks
- 731
- Avg merge
- 3d 9h
- Merged PRs (30d)
- 1
Description
## 🏗 Enhancement Proposal
Currently widget testing for CachedNetworkImage is very difficult and there are some packages for that like https://pub.dev/packages/network_image_mock but I think that could be more easy.
### Pitch
```dart
class DefaultCacheManager extends CacheManager with ImageCacheManager {
static const key = 'libCachedImageData';
static DefaultCacheManager? _instance;
static DefaultCacheManager? _mockCacheManager;
factory DefaultCacheManager() {
_instance ??= _mockCacheManager ?? DefaultCacheManager._();
return _instance!;
}
DefaultCacheManager._() : super(Config(key));
}
@visibleForTesting
Future mockDefaultCacheManager(
Future Function() cb,
DefaultCacheManager mockCacheManager,
) {
DefaultCacheManager._mockCacheManager = mockCacheManager;
await cb();
DefaultCacheManager._mockCacheManager = null;
DefaultCacheManager._instance = null;
}
```
then in our tests we can use
```dart
testWidgets('my test',
(tester) async {
final mock = ....;
return mockDefaultCacheManager(
() async {
/// OUR TEST CODE
}
);
},
);
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.