Skip to content

Commit

Permalink
Weight entries - buggy test
Browse files Browse the repository at this point in the history
  • Loading branch information
Miroslav Mazel committed Nov 24, 2023
1 parent 87c8a59 commit 95ea711
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 133 deletions.
104 changes: 39 additions & 65 deletions lib/widgets/measurements/entries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

// 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,
return Card(
child: ListTile(
title: Text('${currentEntry.value} ${_category.unit}'),
subtitle: Text(
DateFormat.yMd(Localizations.localeOf(context).languageCode)
.format(currentEntry.date),
),
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),
),
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
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).successfullyDeleted,
textAlign: TextAlign.center,
),
),
);
}
})
];
},
),
),
);
Expand Down
104 changes: 39 additions & 65 deletions lib/widgets/weight/entries_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!);

// 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,
return Card(
child: ListTile(
title: Text('${currentEntry.weight} kg'),
subtitle: Text(
DateFormat.yMd(Localizations.localeOf(context).languageCode)
.format(currentEntry.date),
),
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),
),
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
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).successfullyDeleted,
textAlign: TextAlign.center,
),
),
);
}
})
];
},
),
),
);
Expand Down
12 changes: 9 additions & 3 deletions test/weight/weight_screen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 95ea711

Please sign in to comment.