Baseflow / Baseflow/flutter_cached_network_image

Make cached_network_image friendly with widget testing

Open
#814 2 comments 19 reactions 0 assignees View on GitHub
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.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.