From f2d69d86d1b5f95e8c946cb7a54013b0dc708caf Mon Sep 17 00:00:00 2001 From: ANKIT VARSHNEY <132201033+AVtheking@users.noreply.github.com> Date: Wed, 10 Jan 2024 20:52:26 +0530 Subject: [PATCH] written test for post_modal.dart (#2304) * written test for post_modal.dart * written the missing test * descriptive name to the key * fix failing test * created settings page (#2299) * created settings page * formatted the code * fixed failing tests * resolved requested changes * fixed failing tests * rebased * descriptive name to the key * Select contact test (#2301) * Created Test * Updated Formatting * fixed fetch events bug (#2308) * fixed events fetching bug * fixed failing tests * added comments to event_queries * fixed failing test * Refactor: Decouple ViewModel by Moving UI Logic to View (#2306) * Refactor: Decouple ViewModel by Moving UI Logic to View * refactoring app_settings_view_model and app_seetings_page * writing test for missing lines * fix: Removed all references to Google firebase (#2257) * feature: Removed all references to Google firebase * formatted firebase_mocks.dart * removed ignore directive for custom lint rule * formatted login_view_model file * removal of commented code * removed fcmToken test * avoid dynamic calls check passed * format check * fixed linting issue * added documentation for event_queries file * format check * Bump syncfusion_flutter_datepicker from 24.1.43 to 24.1.44 (#2315) Bumps [syncfusion_flutter_datepicker](https://github.com/syncfusion/flutter-widgets/tree/master/packages) from 24.1.43 to 24.1.44. - [Release notes](https://github.com/syncfusion/flutter-widgets/releases) - [Commits](https://github.com/syncfusion/flutter-widgets/commits/HEAD/packages) --- updated-dependencies: - dependency-name: syncfusion_flutter_datepicker dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump url_launcher_platform_interface from 2.2.0 to 2.3.0 (#2314) Bumps [url_launcher_platform_interface](https://github.com/flutter/packages/tree/main/packages/url_launcher) from 2.2.0 to 2.3.0. - [Release notes](https://github.com/flutter/packages/releases) - [Commits](https://github.com/flutter/packages/commits/url_launcher_platform_interface-v2.3.0/packages/url_launcher) --- updated-dependencies: - dependency-name: url_launcher_platform_interface dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump image_picker from 1.0.5 to 1.0.6 (#2313) Bumps [image_picker](https://github.com/flutter/packages/tree/main/packages/image_picker) from 1.0.5 to 1.0.6. - [Release notes](https://github.com/flutter/packages/releases) - [Commits](https://github.com/flutter/packages/commits/image_picker-v1.0.6/packages/image_picker) --- updated-dependencies: - dependency-name: image_picker dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump video_player from 2.8.1 to 2.8.2 (#2317) Bumps [video_player](https://github.com/flutter/packages/tree/main/packages/video_player) from 2.8.1 to 2.8.2. - [Release notes](https://github.com/flutter/packages/releases) - [Commits](https://github.com/flutter/packages/commits/video_player-v2.8.2/packages/video_player) --- updated-dependencies: - dependency-name: video_player dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump google_maps_flutter from 2.5.0 to 2.5.1 (#2316) Bumps [google_maps_flutter](https://github.com/flutter/packages/tree/main/packages/google_maps_flutter) from 2.5.0 to 2.5.1. - [Release notes](https://github.com/flutter/packages/releases) - [Commits](https://github.com/flutter/packages/commits/google_maps_flutter-v2.5.1/packages/google_maps_flutter) --- updated-dependencies: - dependency-name: google_maps_flutter dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * merged develop branch * fix * fix failing test --------- Signed-off-by: dependabot[bot] Co-authored-by: Shaik Azad <120930148+Azad99-9@users.noreply.github.com> Co-authored-by: Shivam Gupta Co-authored-by: Parag Gupta <103507835+Dante291@users.noreply.github.com> Co-authored-by: Abhishek Saini <78199221+Abhisheksainii@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- lib/widgets/post_modal.dart | 4 + pubspec.lock | 2 +- .../widget_tests/widgets/post_modal_test.dart | 202 ++++++++++++++++++ 3 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 test/widget_tests/widgets/post_modal_test.dart diff --git a/lib/widgets/post_modal.dart b/lib/widgets/post_modal.dart index 327934b01d..6a47c9e005 100644 --- a/lib/widgets/post_modal.dart +++ b/lib/widgets/post_modal.dart @@ -44,6 +44,7 @@ class PostBottomModal extends StatelessWidget { ), ), TextButton( + key: const Key('reportPost'), onPressed: () { navigationService.showTalawaErrorSnackBar( 'Your Report has been sent to the Admin', @@ -74,6 +75,7 @@ class PostBottomModal extends StatelessWidget { ), ), TextButton( + key: const Key('deletePost'), onPressed: () { deletePost != null ? deletePost!(post) @@ -89,6 +91,7 @@ class PostBottomModal extends StatelessWidget { ), actions: [ TextButton( + key: const Key('alert_dialog_yes_btn'), onPressed: () { navigationService.showTalawaErrorSnackBar( 'Post was deleted if you had the rights!', @@ -99,6 +102,7 @@ class PostBottomModal extends StatelessWidget { child: const Text("Yes"), ), TextButton( + key: const Key('alert_dialog_no_btn'), onPressed: () { Navigator.pop(context); }, diff --git a/pubspec.lock b/pubspec.lock index 26e172a960..77e2873cb3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1843,4 +1843,4 @@ packages: version: "3.1.2" sdks: dart: ">=3.2.0 <3.13.0" - flutter: ">=3.16.0" + flutter: ">=3.16.0" \ No newline at end of file diff --git a/test/widget_tests/widgets/post_modal_test.dart b/test/widget_tests/widgets/post_modal_test.dart new file mode 100644 index 0000000000..0ee90eb12c --- /dev/null +++ b/test/widget_tests/widgets/post_modal_test.dart @@ -0,0 +1,202 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:talawa/enums/enums.dart'; +import 'package:talawa/models/comment/comment_model.dart'; +import 'package:talawa/models/organization/org_info.dart'; +import 'package:talawa/models/post/post_model.dart'; +import 'package:talawa/models/user/user_info.dart'; +import 'package:talawa/services/navigation_service.dart'; +import 'package:talawa/services/size_config.dart'; +import 'package:talawa/utils/app_localization.dart'; +import 'package:talawa/view_model/lang_view_model.dart'; +import 'package:talawa/views/base_view.dart'; +import 'package:talawa/widgets/post_modal.dart'; + +import '../../helpers/test_helpers.dart'; +import '../../helpers/test_locator.dart'; + +//Mock classes +class MockFunction extends Mock { + void call(Post post); +} + +class MockNavigationService extends Mock implements NavigationService {} + +//test data +final MockFunction mockDeletePost = MockFunction(); +final LikedBy user = LikedBy(sId: "test_id"); + +//fake user data +final u1 = User( + id: '123', + firstName: 'Lakshay', + lastName: 'Gupta', + email: 'test@test.com', +); +final u2 = User( + id: '123', + firstName: 'Ankit', + lastName: 'Varshney', + email: 'test@test.com', +); +final List users = [u1, u2]; + +List comments = [ + Comments(sId: 'comment1'), + Comments(sId: 'comment2'), + Comments(sId: 'comment3'), + Comments(sId: 'comment4'), + Comments(sId: 'comment5'), + Comments(sId: 'comment6'), +]; + +//fake comment data +final comment = Comment( + creator: User( + id: '123', + firstName: 'Ankit', + lastName: 'Varshney', + email: 'test@test.com', + ), + createdAt: '123456', + text: 'test text', + post: 'test post', + likeCount: 'test count', +); + +final LikedBy l1 = LikedBy(sId: 'test1'); +final LikedBy l2 = LikedBy(sId: 'test2'); +final List likeby = [l1, l2]; + +final comment1 = Comments(sId: 'comment1'); +final comment2 = Comments(sId: 'comment2'); +final comment3 = Comments(sId: 'comment3'); +final List comments1 = [comment1, comment2, comment3]; + +final myBirthday = DateTime.utc(2004, DateTime.june, 16, 5, 30, 0, 0, 0); + +//fake post data +final post = Post( + creator: User( + id: '123', + firstName: 'John', + lastName: 'Doe', + email: 'test@test.com', + ), + sId: "sid", + createdAt: myBirthday, + description: 'test description', + organization: OrgInfo(admins: users), + likedBy: likeby, + comments: comments1, +); + +Widget createPostBottomModal() { + return BaseView( + onModelReady: (model) => model.initialize(), + builder: (context, model, child) { + return MaterialApp( + locale: const Locale('en'), + localizationsDelegates: const [ + AppLocalizationsDelegate(isTest: true), + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + ], + home: PostBottomModal( + post: post, + deletePost: mockDeletePost, + ), + navigatorKey: navigationService.navigatorKey, + ); + }, + ); +} + +void main() { + SizeConfig().test(); + testSetupLocator(); + + setUp(() { + registerServices(); + }); + + tearDown(() => unregisterServices()); + + group('PostBottomModalTest -', () { + testWidgets('has a post widget', (tester) async { + await tester.pumpWidget(createPostBottomModal()); + await tester.pumpAndSettle(); + + // Verify the existence of PostBottomModal widget and reportPost button + expect(find.byType(PostBottomModal), findsOneWidget); + expect(find.byKey(const Key('reportPost')), findsOneWidget); + + // Tap the reportPost button and verify the behavior + await tester.tap(find.byKey(const Key('reportPost'))); + await tester.pumpAndSettle(); + + verify( + navigationService.showTalawaErrorSnackBar( + 'Your Report has been sent to the Admin', + MessageType.info, + ), + ).called(1); + }); + + testWidgets('Testing the delete Post button', (tester) async { + await tester.pumpWidget(createPostBottomModal()); + await tester.pumpAndSettle(); + + // Verify the existence of delete Post button + expect(find.byIcon(Icons.delete), findsOneWidget); + expect(find.byKey(const Key('deletePost')), findsOneWidget); + + // Tap the delete Post button and verify the behavior + await tester.tap(find.byKey(const Key('deletePost'))); + await tester.pumpAndSettle(); + + verify(mockDeletePost.call(post)).called(1); + + // Verify the presence of AlertDialog and its elements + expect(find.byType(AlertDialog), findsOneWidget); + expect(find.byKey(const Key('alert_dialog_yes_btn')), findsOneWidget); + expect(find.text('The post was deleted'), findsOneWidget); + + // Tap the yes button in AlertDialog and verify the behavior + await tester.tap(find.byKey(const Key('alert_dialog_yes_btn'))); + await tester.pumpAndSettle(); + + verify( + navigationService.showTalawaErrorSnackBar( + 'Post was deleted if you had the rights!', + MessageType.info, + ), + ).called(1); + }); + + testWidgets("Testing no button of alertDialogBox", (tester) async { + await tester.pumpWidget(createPostBottomModal()); + await tester.pumpAndSettle(); + + // Tap the delete Post button and verify the behavior + expect(find.byKey(const Key('deletePost')), findsOneWidget); + await tester.tap(find.byKey(const Key('deletePost'))); + await tester.pumpAndSettle(); + + verify(mockDeletePost.call(post)).called(1); + + // Verify the presence of AlertDialog and its no button + expect(find.byType(AlertDialog), findsOneWidget); + expect(find.byKey(const Key('alert_dialog_no_btn')), findsOneWidget); + + // Tap the no button in AlertDialog and verify the behavior + await tester.tap(find.byKey(const Key('alert_dialog_no_btn'))); + await tester.pumpAndSettle(); + + // Verify that AlertDialog is dismissed + expect(find.byType(AlertDialog), findsNothing); + }); + }); +}