Skip to content

Commit

Permalink
test(auto_refresh): add tests for refreshWhenReturningToForeground ex…
Browse files Browse the repository at this point in the history
…tension
  • Loading branch information
giboin committed Jul 4, 2024
1 parent a125a51 commit 4a8c2d0
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions test/src/auto_refresh_extension_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpods_community_extensions/src/auto_refresh_extension.dart';
Expand Down Expand Up @@ -72,4 +73,61 @@ void main() {
expect(numberOfFetchDataCalls, 3);
});
});

group('refreshWhenReturningToForeground', () {
var numberOfFetchDataCalls = 0;

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

final myProvider = Provider.autoDispose<int>((ref) {
ref.refreshWhenReturningToForeground();
return fetchData();
});

late ProviderContainer container;

setUp(() {
numberOfFetchDataCalls = 0;
container = ProviderContainer()..listen(myProvider, (_, __) {});
});

tearDown(() {
container.dispose();
});

testWidgets('can refresh when returning to foreground', (tester) async {
// The value should be fetched initially
expect(numberOfFetchDataCalls, 1);

tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
await tester.pumpAndSettle();
expect(numberOfFetchDataCalls, 1);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pumpAndSettle();
expect(numberOfFetchDataCalls, 2);

tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
await tester.pumpAndSettle();
expect(numberOfFetchDataCalls, 2);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pumpAndSettle();
expect(numberOfFetchDataCalls, 3);
});

testWidgets('can be properly disposed', (tester) async {
// The value should be fetched initially
expect(numberOfFetchDataCalls, 1);

container.dispose();
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
await tester.pumpAndSettle();
expect(numberOfFetchDataCalls, 1);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pumpAndSettle();
expect(numberOfFetchDataCalls, 1);
});
});
}

0 comments on commit 4a8c2d0

Please sign in to comment.