-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76e111b
commit b7c8cfc
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
test/src/refresh_when_network_available_extension_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |