From e1a90ec50c4f624525b8aa3d1089073c220ea6a2 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Tue, 27 Jun 2023 18:22:59 +0530 Subject: [PATCH] test(ui): add tests for message_input_attachment_list.dart Signed-off-by: xsahil03x --- .../ios/Runner.xcodeproj/project.pbxproj | 1 + .../stream_chat_flutter/example/lib/main.dart | 6 +- .../message_input_attachment_list_test.dart | 190 ++++++++++++++++++ 3 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 packages/stream_chat_flutter/test/src/message_input/message_input_attachment_list_test.dart diff --git a/packages/stream_chat_flutter/example/ios/Runner.xcodeproj/project.pbxproj b/packages/stream_chat_flutter/example/ios/Runner.xcodeproj/project.pbxproj index e5d8f743..4c7de605 100644 --- a/packages/stream_chat_flutter/example/ios/Runner.xcodeproj/project.pbxproj +++ b/packages/stream_chat_flutter/example/ios/Runner.xcodeproj/project.pbxproj @@ -204,6 +204,7 @@ files = ( ); inputPaths = ( + "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}", ); name = "Thin Binary"; outputPaths = ( diff --git a/packages/stream_chat_flutter/example/lib/main.dart b/packages/stream_chat_flutter/example/lib/main.dart index 332d813b..1db7f73e 100644 --- a/packages/stream_chat_flutter/example/lib/main.dart +++ b/packages/stream_chat_flutter/example/lib/main.dart @@ -356,7 +356,11 @@ class _ChannelPageState extends State { ), ); }, - child: defaultWidget.copyWith(onReplyTap: reply), + child: defaultWidget.copyWith( + onReplyTap: reply, + // showReactionPicker: true, + // showReactionPickerIndicator: false, + ), ); }, ), diff --git a/packages/stream_chat_flutter/test/src/message_input/message_input_attachment_list_test.dart b/packages/stream_chat_flutter/test/src/message_input/message_input_attachment_list_test.dart new file mode 100644 index 00000000..a7afeea8 --- /dev/null +++ b/packages/stream_chat_flutter/test/src/message_input/message_input_attachment_list_test.dart @@ -0,0 +1,190 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:stream_chat_flutter/stream_chat_flutter.dart'; + +import '../mocks.dart'; + +Widget wrapWithStreamChat( + Widget child, { + StreamChatClient? client, +}) { + return MaterialApp( + home: StreamChat( + client: client ?? MockClient(), + child: child, + ), + ); +} + +void main() { + group('StreamMessageInputAttachmentList tests', () { + testWidgets( + 'StreamMessageInputAttachmentList should render attachments', + (WidgetTester tester) async { + final attachments = [ + Attachment(type: 'file', id: 'file1'), + Attachment(type: 'file', id: 'file2'), + Attachment(type: 'media', id: 'media1'), + ]; + + await tester.pumpWidget( + wrapWithStreamChat( + StreamMessageInputAttachmentList( + attachments: attachments, + ), + ), + ); + + // Expect 2 file attachments and 1 media attachment + expect(find.byType(MessageInputFileAttachments), findsOneWidget); + expect(find.byType(MessageInputMediaAttachments), findsOneWidget); + }, + ); + + testWidgets( + 'StreamMessageInputAttachmentList should call onRemovePressed callback', + (WidgetTester tester) async { + Attachment? removedAttachment; + + final attachments = [ + Attachment(type: 'file', id: 'file1'), + Attachment(type: 'file', id: 'file2'), + ]; + + await tester.pumpWidget( + wrapWithStreamChat( + StreamMessageInputAttachmentList( + attachments: attachments, + onRemovePressed: (attachment) { + removedAttachment = attachment; + }, + ), + ), + ); + + final removeButtons = find.byType(RemoveAttachmentButton); + + // Tap the first remove button + await tester.tap(removeButtons.first); + await tester.pump(); + + // Expect the onRemovePressed callback to be called with the second + // attachment as they are reversed in the UI. + expect(removedAttachment, attachments[1]); + }, + ); + + testWidgets( + '''StreamMessageInputAttachmentList should display empty box if no attachments''', + (WidgetTester tester) async { + final attachments = []; + + await tester.pumpWidget( + wrapWithStreamChat( + StreamMessageInputAttachmentList( + attachments: attachments, + ), + ), + ); + + // Expect an empty box + expect(find.byType(SizedBox), findsOneWidget); + }, + ); + }); + + group('MessageInputFileAttachments tests', () { + testWidgets( + 'MessageInputFileAttachments should render file attachments', + (WidgetTester tester) async { + final attachments = [ + Attachment(type: 'file', id: 'file1'), + Attachment(type: 'file', id: 'file2'), + ]; + + await tester.pumpWidget( + wrapWithStreamChat( + MessageInputFileAttachments( + attachments: attachments, + ), + ), + ); + + // Expect 2 file attachments + expect(find.byType(ClipRRect), findsNWidgets(2)); + }, + ); + + testWidgets( + 'MessageInputFileAttachments should call onRemovePressed callback', + (WidgetTester tester) async { + Attachment? removedAttachment; + + final attachments = [ + Attachment(type: 'file', id: 'file1'), + ]; + + await tester.pumpWidget( + wrapWithStreamChat( + MessageInputFileAttachments( + attachments: attachments, + onRemovePressed: (attachment) { + removedAttachment = attachment; + }, + ), + ), + ); + + final removeButton = find.byType(RemoveAttachmentButton); + + // Tap the remove button + await tester.tap(removeButton); + await tester.pump(); + + // Expect the onRemovePressed callback to be called with the attachment + expect(removedAttachment, attachments.first); + }, + ); + }); + + group('MessageInputMediaAttachments tests', () { + testWidgets( + 'MessageInputMediaAttachments should render media attachments', + (WidgetTester tester) async { + final attachments = [ + Attachment(type: 'media', id: 'media1'), + Attachment(type: 'media', id: 'media2'), + ]; + + await tester.pumpWidget( + wrapWithStreamChat( + MessageInputMediaAttachments( + attachments: attachments, + ), + ), + ); + + // Expect 2 media attachments + expect(find.byType(Stack), findsNWidgets(2)); + }, + ); + + testWidgets( + 'MessageInputMediaAttachments should display empty box if no attachments', + (WidgetTester tester) async { + final attachments = []; + + await tester.pumpWidget( + wrapWithStreamChat( + MessageInputMediaAttachments( + attachments: attachments, + ), + ), + ); + + // Expect an empty box + expect(find.byType(SizedBox), findsOneWidget); + }, + ); + }); +}