CHRISTOPANANJICKAL / fast_cached_network_image

A flutter package to cache network image fastly without native dependencies.
MIT License
28 stars 35 forks source link

Fast Cached Network Image usage in Widget tests #53

Open andrewpmoore opened 3 months ago

andrewpmoore commented 3 months ago

Hi Thanks for the great package, really handy, there's just one issue I currently have which is that I run into issues when using widget tests that include using the library.

It'd be great is there was some 'init' option which just mocks the whole thing as at the moment the hive database causes the following error: No implementation found for method getApplicationDocumentsDirectory

CHRISTOPANANJICKAL commented 3 months ago

Can you please post a sample code that recreates this error?

andrewpmoore commented 3 months ago

Hi, This is a cut down version of what I'm trying to do. Have a testWidget that I can run tests on

Here's the code

import 'package:fast_cached_network_image/fast_cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider/path_provider.dart';

void main() {
  testWidgets('Widget test', (WidgetTester tester) async {
    String storageLocation = (await getApplicationDocumentsDirectory()).path;
    await FastCachedImageConfig.init(subDir: storageLocation, clearCacheAfter: const Duration(days: 15));

    await tester.pumpWidget(const ProviderScope(child: MaterialApp(
      home: Scaffold(
        body: FastCachedImage(
          url: 'https://cdn.pixabay.com/photo/2024/04/01/05/18/green-8667981_1280.jpg',
          height: 250,
          fadeInDuration: Duration(milliseconds: 500),
          alignment: Alignment.center,
        ),
      ),
    )));

  });
}

The init has trouble because it's not on a platform and I think causes issues initializing hive. Before using the package I could use https://pub.dev/packages/mocktail_image_network to mock images for tests (it instead of trying to load an image, just returns a transparent pixel), but with FastCachedNetworkImage it won't work without the .init call, but that doesn't work.

My thought was maybe there could be an init that's a mock that doesn't really do anything and then the FastCachedNetworkImage could do similar to the mocktail image network and not really make a network call and just return an empty image