Skip to content

Commit

Permalink
Revisited pictogram_image_test.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
BicaniWolfie committed Nov 26, 2023
1 parent 1155184 commit df332fc
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions test/widgets/pictogram_image_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,34 @@ void main() {
onPressed: () {},
));

// Finder that gets all widgets that are PictogramImages.
final Finder f = find.byWidgetPredicate((Widget widget) {
return widget is PictogramImage;
});

// Verify that only one PictogramImage is found.
// The test fails if there there is not exactly one PictogramImage.
expect(f, findsOneWidget);

final Completer<bool> waiter = Completer<bool>();

// Listen for updates on the image stream.
// The function inside is called every time bloc.image is updated.
bloc.image.listen(expectAsync1((Image image) async {
// Update the frame.
await tester.pump();

// Expect that there is one and only one Image widget.
// The test fails if there is not exactly one Image widget.
expect(find.byType(Image), findsOneWidget);
waiter.complete();
}));

await waiter.future;
});

// TODO(Mathias): Fix or remove?
// Test doesn't evaluate anything and is reliant on time-out
testWidgets('triggers callback on tap', (WidgetTester tester) async {
final Completer<bool> done = Completer<bool>();

Expand All @@ -92,11 +105,15 @@ void main() {
},
));

// Finder that gets all widgets that are StreamBuilders.
final Finder f = find.byWidgetPredicate((Widget widget) {
return widget is StreamBuilder;
});

// Listen for updates on the image stream.
// The function inside is called every time bloc.image is updated.
bloc.image.listen(expectAsync1((Image image) async {
// Update the frame and tap the widget found by the finder 'f'.
await tester.pump();
await tester.tap(f);
}));
Expand All @@ -109,13 +126,20 @@ void main() {
onPressed: () {},
));

// Finder that gets all widgets that are CircularProgressIndicators.
final Finder f = find.byWidgetPredicate((Widget widget) {
return widget is CircularProgressIndicator;
});

// Verify that only one CircularProgressIndicator is found.
// The test fails if there there is not exactly one
// CircularProgressIndicator.
expect(f, findsOneWidget);

final Completer<bool> waiter = Completer<bool>();

// Listen for updates on the image stream.
// The function inside is called every time bloc.image is updated.
bloc.image.listen(expectAsync1((Image image) async {
await tester.pump();
expect(f, findsNothing);
Expand All @@ -131,24 +155,37 @@ void main() {
onPressed: () {},
haveRights: true,
));

expect(find.byType(GirafButton), findsOneWidget);
});

testWidgets('show delete button when comparing ids',
(WidgetTester tester) async {
String id;

final Completer<bool> done = Completer<bool>();

// Listen for updates on the currently authenticated user.
// The function inside is called every time the given authenticaded user's
// Information is changed.
api.user.me().listen((GirafUserModel model) {
id = model.id;
done.complete();
});

// Wait for the new user to be authenticated,
// which is done when the stream is updated.
await done.future;

// Create a new PictogramImage where the authenticated user's rights are
// determined by pictogramModel.userId.
await tester.pumpWidget(PictogramImage(
pictogram: pictogramModel,
onPressed: () {},
haveRights: pictogramModel.userId == id,
));

// Expect that there is one and only one GirafButton.
expect(find.byType(GirafButton), findsOneWidget);
});

Expand All @@ -159,6 +196,8 @@ void main() {
onPressed: () {},
haveRights: false,
));

// Expect that there is no GirafButton when the user does not have rights.
expect(find.byType(GirafButton), findsNothing);
});
}

0 comments on commit df332fc

Please sign in to comment.