Skip to content

Commit

Permalink
test(ConnectivityStreamProvider): provides connectivity stream with e…
Browse files Browse the repository at this point in the history
…xpected values
  • Loading branch information
tanguymossion committed Aug 21, 2024
1 parent 0f9784d commit 8365531
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/src/refresh_when_network_available_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<ConnectivityResult>>.fromIterable([
[ConnectivityResult.none],
[ConnectivityResult.mobile],
[ConnectivityResult.wifi],
]);

when(() => mockConnectivity.onConnectivityChanged)
.thenAnswer((_) => connectivityStream);

final container = ProviderContainer(
overrides: [
connectivityStreamProvider.overrideWith(
(StreamProviderRef<List<ConnectivityResult>> ref) =>
mockConnectivity.onConnectivityChanged.distinct(),
),
],
);

final emittedValues = <List<ConnectivityResult>>[];

// Listen to the connectivity stream provider
final listener = container.listen<AsyncValue<List<ConnectivityResult>>>(
connectivityStreamProvider, (previous, next) {
if (next.hasValue) {
emittedValues.add(next.value!);
}
});

// Wait for the stream to emit all values
await Future<void>.delayed(const Duration(milliseconds: 100));

listener.close();

expect(emittedValues, [
[ConnectivityResult.none],
[ConnectivityResult.mobile],
[ConnectivityResult.wifi],
]);
});
});
}

0 comments on commit 8365531

Please sign in to comment.