From 83655310d4f5af8be15529bb36cc0d08de170abd Mon Sep 17 00:00:00 2001 From: Tanguy Mossion Date: Wed, 21 Aug 2024 13:58:30 +0200 Subject: [PATCH] test(ConnectivityStreamProvider): provides connectivity stream with expected values --- ...when_network_available_extension_test.dart | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test/src/refresh_when_network_available_extension_test.dart b/test/src/refresh_when_network_available_extension_test.dart index fb671bd..31c5229 100644 --- a/test/src/refresh_when_network_available_extension_test.dart +++ b/test/src/refresh_when_network_available_extension_test.dart @@ -74,4 +74,50 @@ void main() { expect(numberOfFetchDataCalls, 2); }); }); + + group('ConnectivityStreamProvider tests', () { + test('provides connectivity stream with expected values', () async { + final mockConnectivity = MockConnectivity(); + + // Simulate the connectivity stream emitting the following values: + final connectivityStream = Stream>.fromIterable([ + [ConnectivityResult.none], + [ConnectivityResult.mobile], + [ConnectivityResult.wifi], + ]); + + when(() => mockConnectivity.onConnectivityChanged) + .thenAnswer((_) => connectivityStream); + + final container = ProviderContainer( + overrides: [ + connectivityStreamProvider.overrideWith( + (StreamProviderRef> ref) => + mockConnectivity.onConnectivityChanged.distinct(), + ), + ], + ); + + final emittedValues = >[]; + + // Listen to the connectivity stream provider + final listener = container.listen>>( + connectivityStreamProvider, (previous, next) { + if (next.hasValue) { + emittedValues.add(next.value!); + } + }); + + // Wait for the stream to emit all values + await Future.delayed(const Duration(milliseconds: 100)); + + listener.close(); + + expect(emittedValues, [ + [ConnectivityResult.none], + [ConnectivityResult.mobile], + [ConnectivityResult.wifi], + ]); + }); + }); }