mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-05 23:44:02 +01:00
Test that the ingredient filters persist and reset
This commit is contained in:
@@ -22,6 +22,8 @@ import 'package:shared_preferences_platform_interface/in_memory_shared_preferenc
|
||||
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
||||
import 'package:wger/core/search_options.dart';
|
||||
import 'package:wger/core/shared_preferences.dart';
|
||||
import 'package:wger/features/nutrition/models/ingredient.dart';
|
||||
import 'package:wger/features/nutrition/models/ingredient_filters.dart';
|
||||
import 'package:wger/features/nutrition/providers/ingredient_filters_notifier.dart';
|
||||
|
||||
void main() {
|
||||
@@ -30,6 +32,17 @@ void main() {
|
||||
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
|
||||
});
|
||||
|
||||
/// The filters as a fresh start of the app reads them back from the prefs.
|
||||
Future<IngredientFilters> reloaded() =>
|
||||
ProviderContainer.test().read(ingredientFiltersProvider.future);
|
||||
|
||||
/// A notifier on a fresh container, its initial state already loaded.
|
||||
Future<(ProviderContainer, IngredientFiltersNotifier)> loadedNotifier() async {
|
||||
final container = ProviderContainer.test();
|
||||
await container.read(ingredientFiltersProvider.future);
|
||||
return (container, container.read(ingredientFiltersProvider.notifier));
|
||||
}
|
||||
|
||||
test('changing vegan filter updates provider and saves value', () async {
|
||||
// Arrange
|
||||
final container = ProviderContainer.test();
|
||||
@@ -52,10 +65,87 @@ void main() {
|
||||
expect(filters.searchLanguage, SearchLanguage.all);
|
||||
|
||||
expect(await PreferenceHelper.instance.getIngredientVeganFilter(), true);
|
||||
expect(await PreferenceHelper.instance.getIngredientVeganFilter(), true);
|
||||
expect(await PreferenceHelper.instance.getIngredientVegetarianFilter(), true);
|
||||
expect(
|
||||
await PreferenceHelper.instance.getIngredientSearchLanguage(),
|
||||
SearchLanguage.all,
|
||||
);
|
||||
|
||||
// and a restart reads all three back
|
||||
final restored = await reloaded();
|
||||
expect(restored.isVegan, true);
|
||||
expect(restored.isVegetarian, true);
|
||||
expect(restored.searchLanguage, SearchLanguage.all);
|
||||
});
|
||||
|
||||
test('the Nutri-Score limit is persisted, and clearing it removes it', () async {
|
||||
final (container, notifier) = await loadedNotifier();
|
||||
|
||||
await notifier.chooseNutriscoreMax(NutriScore.b);
|
||||
expect(container.read(ingredientFiltersProvider).value!.nutriscoreMax, NutriScore.b);
|
||||
expect((await reloaded()).nutriscoreMax, NutriScore.b);
|
||||
|
||||
await notifier.chooseNutriscoreMax(null);
|
||||
expect(container.read(ingredientFiltersProvider).value!.nutriscoreMax, isNull);
|
||||
expect((await reloaded()).nutriscoreMax, isNull);
|
||||
});
|
||||
|
||||
group('resetFilters', () {
|
||||
Future<IngredientFilters> resetAfterChangingEverything(String localeCode) async {
|
||||
final (container, notifier) = await loadedNotifier();
|
||||
await notifier.toggleVegan(true);
|
||||
await notifier.toggleVegetarian(true);
|
||||
await notifier.chooseLanguage(SearchLanguage.all);
|
||||
await notifier.chooseNutriscoreMax(NutriScore.b);
|
||||
|
||||
await notifier.resetFilters(localeCode);
|
||||
|
||||
final state = container.read(ingredientFiltersProvider).value!;
|
||||
expect(state.isVegan, false);
|
||||
expect(state.isVegetarian, false);
|
||||
expect(state.nutriscoreMax, isNull);
|
||||
return state;
|
||||
}
|
||||
|
||||
test('restores the defaults of an English locale and persists them', () async {
|
||||
final state = await resetAfterChangingEverything('en');
|
||||
expect(state.searchLanguage, SearchLanguage.current);
|
||||
|
||||
final restored = await reloaded();
|
||||
expect(restored.isVegan, false);
|
||||
expect(restored.isVegetarian, false);
|
||||
expect(restored.searchLanguage, SearchLanguage.current);
|
||||
expect(restored.nutriscoreMax, isNull);
|
||||
});
|
||||
|
||||
test('keeps the English fallback for any other locale', () async {
|
||||
final state = await resetAfterChangingEverything('de');
|
||||
expect(state.searchLanguage, SearchLanguage.currentAndEnglish);
|
||||
expect((await reloaded()).searchLanguage, SearchLanguage.currentAndEnglish);
|
||||
});
|
||||
});
|
||||
|
||||
group('setSearchTerm', () {
|
||||
test('updates the state but is not persisted', () async {
|
||||
final (container, notifier) = await loadedNotifier();
|
||||
|
||||
notifier.setSearchTerm('oats');
|
||||
|
||||
expect(container.read(ingredientFiltersProvider).value!.searchTerm, 'oats');
|
||||
expect((await reloaded()).searchTerm, '');
|
||||
});
|
||||
|
||||
test('the same term again does not notify listeners', () async {
|
||||
final (container, notifier) = await loadedNotifier();
|
||||
notifier.setSearchTerm('oats');
|
||||
var notifications = 0;
|
||||
container.listen(ingredientFiltersProvider, (_, _) => notifications++);
|
||||
|
||||
notifier.setSearchTerm('oats');
|
||||
expect(notifications, 0);
|
||||
|
||||
notifier.setSearchTerm('rice');
|
||||
expect(notifications, 1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user