Skip to content

Commit

Permalink
wip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tanguymossion committed Jul 5, 2024
1 parent 76e111b commit b7c8cfc
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/src/connectivity_provider.dart
Original file line number Diff line number Diff line change
@@ -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());
4 changes: 3 additions & 1 deletion lib/src/refresh_when_network_available_extension.dart
Original file line number Diff line number Diff line change
@@ -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<T> objects.
/// This extension uses the connectivity_plus package to listen for network
Expand Down Expand Up @@ -34,7 +35,8 @@ extension RefreshWhenNetworkAvailableExtension<T> on AutoDisposeRef<T> {
/// }
/// ```
void refreshWhenNetworkAvailable() {
final connectivityStream = Connectivity().onConnectivityChanged;
final connectivityStream =
watch(connectivityProvider).onConnectivityChanged;

var isNetworkAvailable = false;
const validResults = [
Expand Down
69 changes: 69 additions & 0 deletions test/src/refresh_when_network_available_extension_test.dart
Original file line number Diff line number Diff line change
@@ -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<List<ConnectivityResult>> connectivityController;
late ProviderContainer container;
late AutoDisposeProvider<int> myProvider;

var numberOfFetchDataCalls = 0;

int fetchData() {
numberOfFetchDataCalls++;
return 42;
}

setUp(() {
numberOfFetchDataCalls = 0;
mockConnectivity = MockConnectivity();
connectivityController = StreamController<List<ConnectivityResult>>();
when(() => mockConnectivity.onConnectivityChanged)
.thenAnswer((_) => connectivityController.stream);

myProvider = Provider.autoDispose<int>((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);
});
});
}

0 comments on commit b7c8cfc

Please sign in to comment.