From b7c8cfc49d4d66453ac86a9ccf03f2274b969a73 Mon Sep 17 00:00:00 2001 From: Tanguy Mossion Date: Fri, 5 Jul 2024 16:58:43 +0200 Subject: [PATCH] wip tests --- lib/src/connectivity_provider.dart | 5 ++ ...resh_when_network_available_extension.dart | 4 +- ...when_network_available_extension_test.dart | 69 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 lib/src/connectivity_provider.dart create mode 100644 test/src/refresh_when_network_available_extension_test.dart diff --git a/lib/src/connectivity_provider.dart b/lib/src/connectivity_provider.dart new file mode 100644 index 0000000..39917e8 --- /dev/null +++ b/lib/src/connectivity_provider.dart @@ -0,0 +1,5 @@ +import 'package:connectivity_plus/connectivity_plus.dart'; +import 'package:riverpod/riverpod.dart'; + +/// A provider that exposes a [Connectivity] instance. +final connectivityProvider = Provider((ref) => Connectivity()); diff --git a/lib/src/refresh_when_network_available_extension.dart b/lib/src/refresh_when_network_available_extension.dart index 63951a9..d401008 100644 --- a/lib/src/refresh_when_network_available_extension.dart +++ b/lib/src/refresh_when_network_available_extension.dart @@ -1,5 +1,6 @@ import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:riverpod/riverpod.dart'; +import 'package:riverpod_community_extensions/src/connectivity_provider.dart'; /// Adds network-aware refresh functionality to AutoDisposeRef objects. /// This extension uses the connectivity_plus package to listen for network @@ -34,7 +35,8 @@ extension RefreshWhenNetworkAvailableExtension on AutoDisposeRef { /// } /// ``` void refreshWhenNetworkAvailable() { - final connectivityStream = Connectivity().onConnectivityChanged; + final connectivityStream = + watch(connectivityProvider).onConnectivityChanged; var isNetworkAvailable = false; const validResults = [ diff --git a/test/src/refresh_when_network_available_extension_test.dart b/test/src/refresh_when_network_available_extension_test.dart new file mode 100644 index 0000000..c11fdb3 --- /dev/null +++ b/test/src/refresh_when_network_available_extension_test.dart @@ -0,0 +1,69 @@ +import 'dart:async'; + +import 'package:connectivity_plus/connectivity_plus.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:riverpod/riverpod.dart'; +import 'package:riverpod_community_extensions/src/connectivity_provider.dart'; +import 'package:riverpod_community_extensions/src/refresh_when_network_available_extension.dart'; + +class MockConnectivity extends Mock implements Connectivity {} + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + group('RefreshWhenNetworkAvailableExtension Tests', () { + late MockConnectivity mockConnectivity; + late StreamController> connectivityController; + late ProviderContainer container; + late AutoDisposeProvider myProvider; + + var numberOfFetchDataCalls = 0; + + int fetchData() { + numberOfFetchDataCalls++; + return 42; + } + + setUp(() { + numberOfFetchDataCalls = 0; + mockConnectivity = MockConnectivity(); + connectivityController = StreamController>(); + when(() => mockConnectivity.onConnectivityChanged) + .thenAnswer((_) => connectivityController.stream); + + myProvider = Provider.autoDispose((ref) { + ref.refreshWhenNetworkAvailable(); + return fetchData(); + }); + + container = ProviderContainer( + overrides: [connectivityProvider.overrideWithValue(mockConnectivity)], + )..listen(myProvider, (_, __) {}); + }); + + tearDown(() { + connectivityController.close(); + container.dispose(); + }); + + test('Should not refresh when connectivity does not change to available', + () async { + expect(numberOfFetchDataCalls, 1); + + // Simulate network being offline + connectivityController.add([ConnectivityResult.none]); + + expect(numberOfFetchDataCalls, 1); + }); + + test('Should refresh when connectivity changes to available', () async { + expect(numberOfFetchDataCalls, 1); + + // Simulate network being online + connectivityController.add([ConnectivityResult.wifi]); + + expect(numberOfFetchDataCalls, 2); + }); + }); +}