Weight entries - buggy test

This commit is contained in:
Miroslav Mazel
2023-11-25 00:57:27 +01:00
parent 87c8a599f0
commit 95ea711e97
3 changed files with 87 additions and 133 deletions

View File

@@ -52,72 +52,46 @@ class EntriesList extends StatelessWidget {
final currentEntry = _category.entries[index];
final provider = Provider.of<MeasurementProvider>(context, listen: false);
return Dismissible(
key: Key(currentEntry.id.toString()),
onDismissed: (direction) {
if (direction == DismissDirection.endToStart) {
// Delete entry from DB
provider.deleteEntry(currentEntry.id!, currentEntry.category);
return Card(
child: ListTile(
title: Text('${currentEntry.value} ${_category.unit}'),
subtitle: Text(
DateFormat.yMd(Localizations.localeOf(context).languageCode)
.format(currentEntry.date),
),
trailing: PopupMenuButton(
itemBuilder: (BuildContext context) {
return [
PopupMenuItem(
child: Text(AppLocalizations.of(context).edit),
onTap: () => Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
MeasurementEntryForm(currentEntry.category, currentEntry),
),
)),
PopupMenuItem(
child: Text(AppLocalizations.of(context).delete),
onTap: () async {
// Delete entry from DB
await provider.deleteEntry(currentEntry.id!, currentEntry.category);
// and inform the user
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).successfullyDeleted,
textAlign: TextAlign.center,
),
),
);
}
},
confirmDismiss: (direction) async {
// Edit entry
if (direction == DismissDirection.startToEnd) {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
MeasurementEntryForm(currentEntry.category, currentEntry),
),
);
return false;
}
return true;
},
secondaryBackground: Container(
color: Theme.of(context).colorScheme.error,
alignment: Alignment.centerRight,
padding: const EdgeInsets.only(right: 20),
margin: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 4,
),
child: const Icon(
Icons.delete,
color: Colors.white,
),
),
background: Container(
color: wgerPrimaryButtonColor,
alignment: Alignment.centerLeft,
padding: const EdgeInsets.only(left: 20),
margin: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 4,
),
child: const Icon(
Icons.edit,
color: Colors.white,
),
),
child: Card(
child: ListTile(
title: Text('${currentEntry.value} ${_category.unit}'),
subtitle: Text(
DateFormat.yMd(Localizations.localeOf(context).languageCode)
.format(currentEntry.date),
),
// and inform the user
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).successfullyDeleted,
textAlign: TextAlign.center,
),
),
);
}
})
];
},
),
),
);

View File

@@ -60,72 +60,46 @@ class WeightEntriesList extends StatelessWidget {
itemCount: weightProvider.items.length,
itemBuilder: (context, index) {
final currentEntry = weightProvider.items[index];
return Dismissible(
key: Key(currentEntry.id.toString()),
onDismissed: (direction) {
if (direction == DismissDirection.endToStart) {
// Delete entry from DB
weightProvider.deleteEntry(currentEntry.id!);
return Card(
child: ListTile(
title: Text('${currentEntry.weight} kg'),
subtitle: Text(
DateFormat.yMd(Localizations.localeOf(context).languageCode)
.format(currentEntry.date),
),
trailing: PopupMenuButton(
itemBuilder: (BuildContext context) {
return [
PopupMenuItem(
child: Text(AppLocalizations.of(context).edit),
onTap: () => Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
WeightForm(currentEntry),
),
)),
PopupMenuItem(
child: Text(AppLocalizations.of(context).delete),
onTap: () async {
// Delete entry from DB
await weightProvider.deleteEntry(currentEntry.id!);
// and inform the user
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).successfullyDeleted,
textAlign: TextAlign.center,
),
),
);
}
},
confirmDismiss: (direction) async {
// Edit entry
if (direction == DismissDirection.startToEnd) {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
WeightForm(currentEntry),
),
);
return false;
}
return true;
},
secondaryBackground: Container(
color: Theme.of(context).colorScheme.error,
alignment: Alignment.centerRight,
padding: const EdgeInsets.only(right: 20),
margin: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 4,
),
child: const Icon(
Icons.delete,
color: Colors.white,
),
),
background: Container(
// color: wgerPrimaryButtonColor,
alignment: Alignment.centerLeft,
padding: const EdgeInsets.only(left: 20),
margin: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 4,
),
child: const Icon(
Icons.edit,
color: Colors.white,
),
),
child: Card(
child: ListTile(
title: Text('${currentEntry.weight} kg'),
subtitle: Text(
DateFormat.yMd(Localizations.localeOf(context).languageCode)
.format(currentEntry.date),
),
// and inform the user
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).successfullyDeleted,
textAlign: TextAlign.center,
),
),
);
}
})
];
},
),
),
);

View File

@@ -58,15 +58,21 @@ void main() {
expect(find.text('Weight'), findsOneWidget);
expect(find.byType(MeasurementChartWidgetFl), findsOneWidget);
expect(find.byType(Dismissible), findsNWidgets(2));
expect(find.byType(Card), findsNWidgets(2));
expect(find.byType(ListTile), findsNWidgets(2));
});
testWidgets('Test deleting an item by dragging the dismissible', (WidgetTester tester) async {
testWidgets('Test deleting an item using the Delete button', (WidgetTester tester) async {
await tester.pumpWidget(createWeightScreen());
await tester.drag(find.byKey(const Key('1')), const Offset(-500.0, 0.0));
expect(find.byType(ListTile), findsNWidgets(2));
await tester.tap(find.byTooltip('Show menu').first);
await tester.pumpAndSettle();
await tester.tap(find.text('Delete'));
await tester.pumpAndSettle();
verify(mockWeightProvider.deleteEntry(1)).called(1);
expect(find.byType(ListTile), findsOneWidget);
});