flutter_cached_network_image icon indicating copy to clipboard operation
flutter_cached_network_image copied to clipboard

Make cached_network_image friendly with widget testing

Open darwin-morocho opened this issue 1 year ago • 2 comments

🏗 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

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<void> mockDefaultCacheManager(
  Future Function() cb,
  DefaultCacheManager mockCacheManager,
) {
  DefaultCacheManager._mockCacheManager = mockCacheManager;
  await cb();
  DefaultCacheManager._mockCacheManager = null;
  DefaultCacheManager._instance = null;
}

then in our tests we can use

testWidgets('my test', 
   (tester) async {
    final mock = ....;
    return mockDefaultCacheManager(
      () async {
        /// OUR TEST CODE
      }
    );
    },
);

darwin-morocho avatar Jan 06 '23 17:01 darwin-morocho

Very necessary for me. There is no way to make a MockCacheManager and all goldens tests fall with an error. Let's open the BaseCacheManager for export, or will we make an implementation for testing specifically and open it?

meg4cyberc4t avatar Oct 07 '23 08:10 meg4cyberc4t