mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
Added tests
This commit is contained in:
@@ -48,7 +48,7 @@ class NutritionPlansProvider with ChangeNotifier {
|
||||
late IngredientDatabase database;
|
||||
List<NutritionalPlan> _plans = [];
|
||||
List<Ingredient> ingredients = [];
|
||||
|
||||
|
||||
// Track current search to prevent multiple concurrent requests
|
||||
String? _currentSearchQuery;
|
||||
int _searchCounter = 0;
|
||||
@@ -306,9 +306,7 @@ class NutritionPlansProvider with ChangeNotifier {
|
||||
await database.deleteEverything();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Saves an ingredient to the cache
|
||||
/// Saves an ingredient to the cache
|
||||
Future<void> cacheIngredient(Ingredient ingredient, {IngredientDatabase? database}) async {
|
||||
database ??= this.database;
|
||||
|
||||
@@ -344,7 +342,7 @@ class NutritionPlansProvider with ChangeNotifier {
|
||||
// Try to fetch from local db
|
||||
if (ingredientDb != null) {
|
||||
ingredient = Ingredient.fromJson(jsonDecode(ingredientDb.data));
|
||||
ingredients.add(ingredient);
|
||||
ingredients.add(ingredient);
|
||||
_logger.info("Loaded ingredient '${ingredient.name}' from db cache");
|
||||
|
||||
// Prune old entries
|
||||
@@ -427,7 +425,6 @@ class NutritionPlansProvider with ChangeNotifier {
|
||||
|
||||
// TODO we should probably add it to ingredient cache.
|
||||
return Ingredient.fromJson(data['results'][0]);
|
||||
|
||||
}
|
||||
|
||||
/// Log meal to nutrition diary
|
||||
|
||||
@@ -207,11 +207,13 @@ void main() {
|
||||
description: 'Old active plan',
|
||||
startDate: now.subtract(const Duration(days: 10)),
|
||||
endDate: now.add(const Duration(days: 10)),
|
||||
creationDate: now.subtract(const Duration(days: 10)),
|
||||
);
|
||||
final newerPlan = NutritionalPlan(
|
||||
description: 'Newer active plan',
|
||||
startDate: now.subtract(const Duration(days: 5)),
|
||||
endDate: now.add(const Duration(days: 5)),
|
||||
creationDate: now.subtract(const Duration(days: 1)),
|
||||
);
|
||||
nutritionProvider = NutritionPlansProvider(mockWgerBaseProvider, [
|
||||
olderPlan,
|
||||
@@ -222,6 +224,19 @@ void main() {
|
||||
});
|
||||
|
||||
group('Ingredient cache DB', () {
|
||||
test('cacheIngredient saves to both in-memory and database cache', () async {
|
||||
nutritionProvider.ingredients = [];
|
||||
final ingredient = Ingredient.fromJson(ingredient59887Response);
|
||||
|
||||
await nutritionProvider.cacheIngredient(ingredient, database: database);
|
||||
|
||||
expect(nutritionProvider.ingredients.length, 1);
|
||||
expect(nutritionProvider.ingredients.first.id, 59887);
|
||||
|
||||
final rows = await database.select(database.ingredients).get();
|
||||
expect(rows.length, 1);
|
||||
expect(rows.first.id, ingredient.id);
|
||||
});
|
||||
test('that if there is already valid data in the DB, the API is not hit', () async {
|
||||
// Arrange
|
||||
nutritionProvider.ingredients = [];
|
||||
|
||||
@@ -331,5 +331,31 @@ void main() {
|
||||
verify(mockNutrition.addMealItem(any, meal1));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('selecting ingredient from autocomplete calls cacheIngredient', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
await tester.pumpWidget(createMealItemFormScreen(meal1, '', true));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
clearInteractions(mockNutrition);
|
||||
|
||||
when(
|
||||
mockNutrition.searchIngredient(
|
||||
any,
|
||||
languageCode: anyNamed('languageCode'),
|
||||
searchEnglish: anyNamed('searchEnglish'),
|
||||
),
|
||||
).thenAnswer((_) => Future.value([ingredient1]));
|
||||
|
||||
await tester.enterText(find.byType(TextFormField).first, 'Water');
|
||||
await tester.pumpAndSettle(const Duration(milliseconds: 600));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.byType(ListTile).first);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
verify(mockNutrition.cacheIngredient(ingredient1)).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user