From fecaf28a974ccda192f96351c0aed32fe0d4e008 Mon Sep 17 00:00:00 2001 From: Ayush0Chaudhary Date: Wed, 8 Feb 2023 20:22:51 +0530 Subject: [PATCH] Fixes #1242 Create tests for create_event_page.dart --- .../create_event_view_model.dart | 3 - .../events/create_event_page.dart | 4 + lib/widgets/add_members_bottom_sheet.dart | 1 + test/helpers/test_helpers.dart | 17 + test/helpers/test_helpers.mocks.dart | 11 + .../events/create_event_page_test.dart | 480 ++++++++++++++++++ 6 files changed, 513 insertions(+), 3 deletions(-) create mode 100644 test/views/after_auth_screens/events/create_event_page_test.dart diff --git a/lib/view_model/after_auth_view_models/event_view_models/create_event_view_model.dart b/lib/view_model/after_auth_view_models/event_view_models/create_event_view_model.dart index ff345c8bc..6208b8103 100644 --- a/lib/view_model/after_auth_view_models/event_view_models/create_event_view_model.dart +++ b/lib/view_model/after_auth_view_models/event_view_models/create_event_view_model.dart @@ -105,13 +105,11 @@ class CreateEventViewModel extends BaseModel { ); final tokenResult = await databaseFunctions .refreshAccessToken(userConfig.currentUser.refreshToken!); - print(tokenResult); final result = await databaseFunctions.gqlAuthMutation( EventQueries().addEvent(), variables: variables, ); navigationService.pop(); - print('Result is : $result'); if (result != null) { navigationService.pop(); @@ -153,7 +151,6 @@ class CreateEventViewModel extends BaseModel { void buildUserList({required bool isAdmin}) { isAdmin ? _selectedAdmins.clear() : _selectedMembers.clear(); - orgMembersList.forEach((orgMember) { if (_adminCheckedMap[orgMember.id] == true && isAdmin) { _selectedAdmins.add(orgMember); diff --git a/lib/views/after_auth_screens/events/create_event_page.dart b/lib/views/after_auth_screens/events/create_event_page.dart index d9b4e1e27..5422fa19b 100644 --- a/lib/views/after_auth_screens/events/create_event_page.dart +++ b/lib/views/after_auth_screens/events/create_event_page.dart @@ -75,6 +75,7 @@ class _CreateEventPageState extends State { width: SizeConfig.screenWidth! * 0.036, ), TextButton( + key: const Key('txt_btn_cep'), onPressed: () => model.getImageFromGallery(), child: Text( AppLocalizations.of(context)! @@ -162,6 +163,7 @@ class _CreateEventPageState extends State { height: SizeConfig.screenHeight! * 0.013, ), DateTimeTile( + key: const Key('key for test cep'), date: "${model.eventEndDate.toLocal()}".split(' ')[0], time: model.eventEndTime.format(context), setDate: () async { @@ -259,6 +261,7 @@ class _CreateEventPageState extends State { SizedBox(height: SizeConfig.screenHeight! * 0.026), const Divider(), InkWell( + key: const Key('inwell_cep1'), onTap: () { EventBottomSheet().addUserBottomSheet( context: context, @@ -297,6 +300,7 @@ class _CreateEventPageState extends State { SizedBox(height: SizeConfig.screenHeight! * 0.026), const Divider(), InkWell( + key: const Key('inwell_cep2'), onTap: () { EventBottomSheet().addUserBottomSheet( context: context, diff --git a/lib/widgets/add_members_bottom_sheet.dart b/lib/widgets/add_members_bottom_sheet.dart index 8f57d43a1..e623d57c3 100644 --- a/lib/widgets/add_members_bottom_sheet.dart +++ b/lib/widgets/add_members_bottom_sheet.dart @@ -39,6 +39,7 @@ class EventBottomSheet { style: const TextStyle(fontSize: 16), ), TextButton( + key: const Key('text_btn_ambs1'), onPressed: () { model.buildUserList(isAdmin: isAdmin); Navigator.pop(context); diff --git a/test/helpers/test_helpers.dart b/test/helpers/test_helpers.dart index 99c2a4652..2b730c7ff 100644 --- a/test/helpers/test_helpers.dart +++ b/test/helpers/test_helpers.dart @@ -95,6 +95,23 @@ OrganizationService getAndRegisterOrganizationService() { _removeRegistrationIfExists(); final service = MockOrganizationService(); locator.registerSingleton(service); + + final User user1 = User( + id: "fakeUser1", + firstName: 'ayush', + lastName: 'chaudhary', + image: 'www.image.com', + ); + final User user2 = User( + id: "fakeUser2", + firstName: 'ayush', + lastName: 'chaudhary', + image: 'www.image.com', + ); + final List users = [user1, user2]; + when(service.getOrgMembersList('XYZ')).thenAnswer((realInvocation) async { + return users; + }); return service; } diff --git a/test/helpers/test_helpers.mocks.dart b/test/helpers/test_helpers.mocks.dart index cff124de5..29ecfe610 100644 --- a/test/helpers/test_helpers.mocks.dart +++ b/test/helpers/test_helpers.mocks.dart @@ -148,6 +148,17 @@ class MockNavigationService extends _i2.Mock implements _i7.NavigationService { Invocation.method(#showSnackBar, [message], {#duration: duration}), returnValueForMissingStub: null); @override + void showTalawaErrorWidget(String? errorMessage, + {Duration? duration = const Duration(seconds: 2)}) => + super.noSuchMethod( + Invocation.method( + #showTalawaErrorWidget, [errorMessage], {#duration: duration}), + returnValueForMissingStub: null); + @override + void showTalawaErrorDialog(String? errorMessage) => super.noSuchMethod( + Invocation.method(#showTalawaErrorDialog, [errorMessage]), + returnValueForMissingStub: null); + @override void pop() => super.noSuchMethod(Invocation.method(#pop, []), returnValueForMissingStub: null); } diff --git a/test/views/after_auth_screens/events/create_event_page_test.dart b/test/views/after_auth_screens/events/create_event_page_test.dart new file mode 100644 index 000000000..5e0df90ca --- /dev/null +++ b/test/views/after_auth_screens/events/create_event_page_test.dart @@ -0,0 +1,480 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:intl/intl.dart'; +import 'package:mockito/mockito.dart'; +import 'package:talawa/constants/custom_theme.dart'; +import 'package:talawa/services/org_service.dart'; +import 'package:talawa/services/size_config.dart'; +import 'package:talawa/services/third_party_service/multi_media_pick_service.dart'; +import 'package:talawa/utils/app_localization.dart'; +import 'package:talawa/view_model/lang_view_model.dart'; +import 'package:talawa/views/after_auth_screens/events/create_event_page.dart'; +import 'package:talawa/views/base_view.dart'; + +import '../../../helpers/test_helpers.dart'; +import '../../../helpers/test_locator.dart'; + +class MockCallbackFunction extends Mock { + call(); +} + +final setDateCallback = MockCallbackFunction(); +final setTimeCallback = MockCallbackFunction(); + +Widget createEventScreen( + {ThemeMode themeMode = ThemeMode.light, required ThemeData theme}) => + BaseView( + onModelReady: (model) => model.initialize(), + builder: (context, langModel, child) { + return MaterialApp( + locale: const Locale('en'), + localizationsDelegates: [ + const AppLocalizationsDelegate(isTest: true), + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + ], + key: const Key('Root'), + themeMode: themeMode, + theme: theme, + home: Scaffold( + key: navigationService.navigatorKey, + body: const CreateEventPage( + key: Key('CreateEventScreen'), + ), + ), + // navigatorKey: navigationService.navigatorKey, + // onGenerateRoute: router.generateRoute, + ); + }); + +void main() { + SizeConfig().test(); + testSetupLocator(); + locator.unregister(); + locator.unregister(); + locator.registerSingleton(OrganizationService()); + // locator.registerSingleton(LikeButtonViewModel()); + + setUp(() { + registerServices(); + }); + + testWidgets("Checking tap Inkwell for setDate 1 datetime", (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + final inkwellFinder = find.byType(InkWell); + expect(inkwellFinder, findsNWidgets(8)); + // tester.allElements.forEach((element) { + // print(element); + // }); + + ///returning the file variable to the + ///result of function multimediaPickerService.getPhotoFromGallery + ///when this function is called in the + ///view model of add_post_page. + final file = File('fakePath'); + + /// using the new instance of multimediaPickerService + /// so that when statement can be used again, + /// else it gives null point exception + final multimediaPickerService = locator(); + + /// when is function provided by mockito lib + when(multimediaPickerService.getPhotoFromGallery(camera: false)) + .thenAnswer((_) async { + return file; + }); + + await tester.tap(inkwellFinder.at(1)); + await tester.pump(); + }); + + testWidgets("Checking tap Inkwell for setTime 1 datetime", (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + final inkwellFinder = find.byType(InkWell); + expect(inkwellFinder, findsNWidgets(8)); + + ///returning the file variable to the + ///result of function multimediaPickerService.getPhotoFromGallery + ///when this function is called in the + ///view model of add_post_page. + final file = File('fakePath'); + + /// using the new instance of multimediaPickerService + /// so that when statement can be used again, + /// else it gives null point exception + final multimediaPickerService = locator(); + + /// when is function provided by mockito lib + when(multimediaPickerService.getPhotoFromGallery(camera: false)) + .thenAnswer((_) async { + return file; + }); + + await tester.tap(inkwellFinder.at(2)); + await tester.pump(); + }); + testWidgets("Checking tap Inkwell for setDate 2 datetime", (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + final inkwellFinder = find.byType(InkWell); + expect(inkwellFinder, findsNWidgets(8)); + + ///returning the file variable to the + ///result of function multimediaPickerService.getPhotoFromGallery + ///when this function is called in the + ///view model of add_post_page. + final file = File('fakePath'); + + /// using the new instance of multimediaPickerService + /// so that when statement can be used again, + /// else it gives null point exception + final multimediaPickerService = locator(); + + /// when is function provided by mockito lib + when(multimediaPickerService.getPhotoFromGallery(camera: false)) + .thenAnswer((_) async { + return file; + }); + + await tester.tap(inkwellFinder.at(3)); + await tester.pump(); + }); + + testWidgets("Checking tap Inkwell for set time 2 datetime", (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + final inkwellFinder = find.byType(InkWell); + expect(inkwellFinder, findsNWidgets(8)); + // tester.allElements.forEach((element) { + // print(element); + // }); + + ///returning the file variable to the + ///result of function multimediaPickerService.getPhotoFromGallery + ///when this function is called in the + ///view model of add_post_page. + final file = File('fakePath'); + + /// using the new instance of multimediaPickerService + /// so that when statement can be used again, + /// else it gives null point exception + final multimediaPickerService = locator(); + + /// when is function provided by mockito lib + when(multimediaPickerService.getPhotoFromGallery(camera: false)) + .thenAnswer((_) async { + return file; + }); + + await tester.tap(inkwellFinder.at(4)); + await tester.pump(); + }); + + testWidgets('checks if the upload photo from button shows correct icon', + (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + + /// using the key of icon button + /// because their are many icon button + + final finder = find.byKey(const Key('txt_btn_cep')); + + expect(finder, findsOneWidget); + + ///returning the file variable to the + ///result of function multimediaPickerService.getPhotoFromGallery + ///when this function is called in the + ///view model of add_post_page. + final file = File('fakePath'); + + /// using the new instance of multimediaPickerService + /// so that when statement can be used again, + /// else it gives null point exception + final multimediaPickerServices = locator(); + + /// when is function provided by mockito lib + when(multimediaPickerServices.getPhotoFromGallery()).thenAnswer((_) async { + return file; + }); + + await tester.tap(finder); + await tester.pump(); + + expect(finder, findsOneWidget); + }); + + testWidgets( + 'checks if the uploaded photo from gallery button is removed after pressing remove button', + (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + + /// using the key of icon button + /// because their are many icon button + + final finder = find.byKey(const Key('txt_btn_cep')); + + final cancelBtn = find.byIcon(Icons.cancel); + + expect(finder, findsOneWidget); + + ///returning the file variable to the + ///result of function multimediaPickerService.getPhotoFromGallery + ///when this function is called in the + ///view model of add_post_page. + final file = File('fakePath'); + + /// using the new instance of multimediaPickerService + /// so that when statement can be used again, + /// else it gives null point exception + final multimediaPickerService = locator(); + + /// when is function provided by mockito lib + when(multimediaPickerService.getPhotoFromGallery(camera: false)) + .thenAnswer((_) async { + return file; + }); + + await tester.tap(finder); + await tester.pump(); + + await tester.tap(cancelBtn); + await tester.pump(); + }); + + testWidgets("Testing Keep public section", (tester) async { + await tester.pumpWidget(createEventScreen( + theme: TalawaTheme.lightTheme, + )); + await tester.pumpAndSettle(); + final appLocalization = AppLocalizations.of( + navigationService.navigatorKey.currentContext!, + ); + final keepPublicText = find.descendant( + of: find.byType(Row), + matching: find.text( + appLocalization!.strictTranslate('Keep Public'), + ), + ); + final switches = find.descendant( + of: find.byType(Row), + matching: find.byType(Switch), + ); + expect(keepPublicText, findsOneWidget); + expect(switches, findsNWidgets(2)); + expect( + (tester.widget(keepPublicText) as Text?)?.style!.fontSize, + 16, + ); + expect((tester.firstWidget(switches) as Switch).value, true); + await tester.tap(switches.first); + await tester.pumpAndSettle(); + expect((tester.firstWidget(switches) as Switch).value, false); + }); + + testWidgets("Testing Keep Registerable section", (tester) async { + await tester.pumpWidget(createEventScreen( + theme: TalawaTheme.lightTheme, + )); + await tester.pumpAndSettle(); + final appLocalization = AppLocalizations.of( + navigationService.navigatorKey.currentContext!, + ); + final keepRegisterableText = find.descendant( + of: find.byType(Row), + matching: find.text( + appLocalization!.strictTranslate('Keep Registerable'), + ), + ); + final switches = find.descendant( + of: find.byType(Row), + matching: find.byType(Switch), + ); + expect(keepRegisterableText, findsOneWidget); + expect(switches, findsNWidgets(2)); + expect( + (tester.widget(keepRegisterableText) as Text?)?.style!.fontSize, + 16, + ); + expect((tester.widgetList(switches).toList()[1] as Switch).value, true); + await tester.ensureVisible(switches.at(1)); + await tester.tap(switches.at(1)); + await tester.pumpAndSettle(); + expect((tester.widgetList(switches).toList()[1] as Switch).value, false); + }); + + testWidgets("Checking tap Inkwell work for admin list", (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + final inkwellFinder = find.byType(InkWell); + expect(inkwellFinder, findsNWidgets(8)); + + ///returning the file variable to the + ///result of function multimediaPickerService.getPhotoFromGallery + ///when this function is called in the + ///view model of add_post_page. + final file = File('fakePath'); + + /// using the new instance of multimediaPickerService + /// so that when statement can be used again, + /// else it gives null point exception + final multimediaPickerService = locator(); + + /// when is function provided by mockito lib + when(multimediaPickerService.getPhotoFromGallery(camera: false)) + .thenAnswer((_) async { + return file; + }); + + await tester.ensureVisible(find.byKey(const Key('inwell_cep1'))); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const Key('inwell_cep1'))); + await tester.pump(); + + //TODO: implement Rest of the test when the _adminCheckedMap or _memberCheckedMap return true for some id + //TODO: for not it return false for all the ordMember.id + // + // orgMembersList.forEach((orgMember) { + // if (isAdmin) { + // _adminCheckedMap.putIfAbsent(orgMember.id!, () => false); + // } else { + // _memberCheckedMap.putIfAbsent(orgMember.id!, () => false); + // } + // _memberCheckedMap.putIfAbsent(orgMember.id!, () => false); + // }); + }); + + testWidgets("Checking tap Inkwell for second add to bottom sheet", + (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + final inkwellFinder = find.byType(InkWell); + expect(inkwellFinder, findsNWidgets(8)); + + await tester.ensureVisible(find.byKey(const Key('inwell_cep2'))); + await tester.pump(); + + // await tester.tap(find.byKey(const Key('text_btn_ambs'))); + await tester.tap(find.byKey( + const Key('inwell_cep2'), + )); + await tester.pump(); + + await tester.pump(); + await tester.ensureVisible(find.byKey(const Key('text_btn_ambs1'))); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const Key('text_btn_ambs1'))); + await tester.pump(); + }); + group('setState Coverage completion', () { + testWidgets('Tap on DataTimeTile date', (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + + await tester.tap(find.byKey(const Key('EventDateTimeTileDate')).first); + await tester.pump(); + + await tester.ensureVisible(find.byKey(const Key('key for test cep'))); + await tester.pump(); + + expect(find.byType(DatePickerDialog), findsOneWidget); + expect(find.byType(CalendarDatePicker), findsOneWidget); + + await tester.tap(find.text('OK')); + await tester.pumpAndSettle(); + + expect(find.text(DateTime.now().toString().split(' ').first), + findsNWidgets(2)); + }); + testWidgets('Tap on DataTimeTile time', (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const Key('EventDateTimeTileTime')).first); + await tester.pump(); + + expect(find.byType(TimePickerDialog), findsOneWidget); + + await tester.tap(find.text('OK')); + await tester.pump(); + expect(find.text(DateFormat.jm().format(DateTime.now()).toString()), + findsNWidgets(2)); + }); + testWidgets('Tap on DataTimeTile date', (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pump(); + + await tester.tap(find.byKey(const Key('EventDateTimeTileDate')).last); + await tester.pump(); + + await tester.ensureVisible(find.byKey(const Key('key for test cep'))); + await tester.pump(); + + expect(find.byType(DatePickerDialog), findsOneWidget); + expect(find.byType(CalendarDatePicker), findsOneWidget); + + await tester.tap(find.text('OK')); + await tester.pumpAndSettle(); + expect(find.text(DateTime.now().toString().split(' ').first), + findsNWidgets(2)); + }); + testWidgets('Tap on DataTimeTile time', (tester) async { + await tester.pumpWidget(createEventScreen( + themeMode: ThemeMode.dark, + theme: TalawaTheme.darkTheme, + )); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const Key('EventDateTimeTileTime')).last); + await tester.pump(); + + expect(find.byType(TimePickerDialog), findsOneWidget); + + await tester.tap(find.text('OK')); + await tester.pump(); + expect(find.text(DateFormat.jm().format(DateTime.now()).toString()), + findsNWidgets(2)); + }); + }); +}