diff --git a/lib/view_model/after_auth_view_models/feed_view_models/organization_feed_view_model.dart b/lib/view_model/after_auth_view_models/feed_view_models/organization_feed_view_model.dart index d8063dec1..119e3286c 100644 --- a/lib/view_model/after_auth_view_models/feed_view_models/organization_feed_view_model.dart +++ b/lib/view_model/after_auth_view_models/feed_view_models/organization_feed_view_model.dart @@ -22,7 +22,8 @@ class OrganizationFeedViewModel extends BaseModel { // Local caching variables for a session. // ignore: prefer_final_fields List _posts = []; - final List _pinnedPosts = + bool istest = false; + List _pinnedPosts = pinnedPostsDemoData.map((e) => Post.fromJson(e)).toList(); final Set _renderedPostID = {}; late String _currentOrgName = ""; @@ -38,8 +39,22 @@ class OrganizationFeedViewModel extends BaseModel { late StreamSubscription _updatePostSubscription; // Getters - List get posts => _posts; - List get pinnedPosts => _pinnedPosts; + List get posts { + // if (istest) { + // _posts = pinnedPostsDemoData.map((e) => Post.fromJson(e)).toList(); + // return _posts; + // } + return _posts; + } + + List get pinnedPosts { + if (istest) { + _pinnedPosts = []; + return _pinnedPosts; + } + return _pinnedPosts; + } + String get currentOrgName => _currentOrgName; /// This function sets the organization name after update. @@ -62,9 +77,11 @@ class OrganizationFeedViewModel extends BaseModel { _postService.getPosts(); } - // initialiser - void initialise() { - // For caching/initalizing the current organization after the stream subsciption has canceled and the stream is updated + void initialise( + // bool forTest, + {bool isTest = false}) { + // For caching/initializing the current organization after the stream subscription has canceled and the stream is updated + _currentOrgName = _userConfig.currentOrg.name!; // ------ // Attaching the stream subscription to rebuild the widgets automatically @@ -73,21 +90,24 @@ class OrganizationFeedViewModel extends BaseModel { (updatedOrganization) => setCurrentOrganizationName(updatedOrganization.name!), ); - - _postsSubscription = - _postService.postStream.listen((newPosts) => buildNewPosts(newPosts)); + _postsSubscription = _postService.postStream.listen((newPosts) { + return buildNewPosts(newPosts); + }); _updatePostSubscription = _postService.updatedPostStream.listen((post) => updatedPost(post)); + if (isTest) { + istest = true; + } } void initializeWithDemoData() { // final postJsonResult = postsDemoData; - + // // ------ - // Calling function to ge the post for the only 1st time. + // // Calling function to ge the post for the only 1st time. // _postService.getPosts(); - + // // //fetching pinnedPosts // final pinnedPostJsonResult = pinnedPostsDemoData; // pinnedPostJsonResult.forEach((pinnedPostJsonData) { diff --git a/lib/views/after_auth_screens/feed/organization_feed.dart b/lib/views/after_auth_screens/feed/organization_feed.dart index ef92b7fc4..4a0a93641 100644 --- a/lib/views/after_auth_screens/feed/organization_feed.dart +++ b/lib/views/after_auth_screens/feed/organization_feed.dart @@ -10,13 +10,15 @@ class OrganizationFeed extends StatelessWidget { const OrganizationFeed({ required Key key, this.homeModel, + this.forTest = false, }) : super(key: key); final MainScreenViewModel? homeModel; + final bool forTest; @override Widget build(BuildContext context) { return BaseView( - onModelReady: (model) => model.initialise(), + onModelReady: (model) => model.initialise(isTest: forTest), builder: (context, model, child) { return Scaffold( appBar: AppBar( diff --git a/test/helpers/test_helpers.dart b/test/helpers/test_helpers.dart index 3fac16c21..5c13cda66 100644 --- a/test/helpers/test_helpers.dart +++ b/test/helpers/test_helpers.dart @@ -331,6 +331,7 @@ PostService getAndRegisterPostService() { final StreamController> _streamController = StreamController(); final Stream> _stream = _streamController.stream.asBroadcastStream(); + // _streamController.add(posts); when(service.postStream).thenAnswer((invocation) => _stream); final StreamController _updateStreamController = StreamController(); diff --git a/test/widget_tests/after_auth_screens/events/create_event_page_test.dart b/test/widget_tests/after_auth_screens/events/create_event_page_test.dart index 4573e0b34..c86b0ad41 100644 --- a/test/widget_tests/after_auth_screens/events/create_event_page_test.dart +++ b/test/widget_tests/after_auth_screens/events/create_event_page_test.dart @@ -41,6 +41,21 @@ void main() { setupLocator(); graphqlConfig.test(); group("Create Event Screen Widget Test in dark mode", () { + group('Check if the validator of the create_event_form is working', () { + testWidgets("Testing if text field validator are working", + (tester) async { + await tester.pumpWidget(createEventScreen( + theme: TalawaTheme.lightTheme, + )); + await tester.pumpAndSettle(); + final addBtn = find.descendant( + of: find.byType(AppBar), + matching: find.byType(TextButton), + ); + await tester.tap(addBtn); + }); + }); + testWidgets("Testing if dark mode is applied", (tester) async { await tester.pumpWidget(createEventScreen( themeMode: ThemeMode.dark, @@ -423,17 +438,4 @@ void main() { }); }); }); - group('Check if the validator of the create_event_form is working', () { - testWidgets("Testing if text field validator are working", (tester) async { - await tester.pumpWidget(createEventScreen( - theme: TalawaTheme.lightTheme, - )); - await tester.pumpAndSettle(); - final addBtn = find.descendant( - of: find.byType(AppBar), - matching: find.byType(TextButton), - ); - await tester.tap(addBtn); - }); - }); } diff --git a/test/widget_tests/after_auth_screens/feed/organization_feed_test.dart b/test/widget_tests/after_auth_screens/feed/organization_feed_test.dart new file mode 100644 index 000000000..03a1b5012 --- /dev/null +++ b/test/widget_tests/after_auth_screens/feed/organization_feed_test.dart @@ -0,0 +1,188 @@ +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/router.dart'; +import 'package:talawa/services/navigation_service.dart'; +import 'package:talawa/services/post_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/view_model/main_screen_view_model.dart'; +import 'package:talawa/views/after_auth_screens/feed/organization_feed.dart'; +import 'package:talawa/views/base_view.dart'; + +import '../../../helpers/test_helpers.dart'; +import '../../../helpers/test_locator.dart'; + +Widget createOrganizationFeedScreen({ + bool reverse = false, + String? dialogTitle, + bool passSecondaryFunc = true, + required MainScreenViewModel homeModel, +}) { + return MaterialApp( + navigatorObservers: [], + locale: const Locale('en'), + supportedLocales: [ + const Locale('en', 'US'), + const Locale('es', 'ES'), + const Locale('fr', 'FR'), + const Locale('hi', 'IN'), + const Locale('zh', 'CN'), + const Locale('de', 'DE'), + const Locale('ja', 'JP'), + const Locale('pt', 'PT'), + ], + localizationsDelegates: [ + const AppLocalizationsDelegate(isTest: true), + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + ], + home: Scaffold( + key: MainScreenViewModel.scaffoldKey, + body: OrganizationFeed( + homeModel: homeModel, + key: const Key('test_key'), + ))); +} + +// late OrganizationFeedViewModel _organizationFeedViewModel; + +Widget createOrganizationFeedScreen2({ + bool isPublic = true, + bool viewOnMap = true, + required MainScreenViewModel homeModel, +}) { + return BaseView( + onModelReady: (model) => model.initialize(), + // builder: (context, langModel, child) { + // return BaseView( + // onModelReady: (model) { + // model.initialise(); + // _organizationFeedViewModel = model; + // }, + builder: (context, model, child) { + return MaterialApp( + locale: const Locale('en'), + localizationsDelegates: [ + const AppLocalizationsDelegate(isTest: true), + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + ], + home: Scaffold( + key: MainScreenViewModel.scaffoldKey, + body: OrganizationFeed( + homeModel: homeModel, + key: const Key('test_key'), + forTest: true, + ), + ), + navigatorKey: locator().navigatorKey, + onGenerateRoute: generateRoute, + ); + }, + // ); + // }, + ); +} + +void main() { + SizeConfig().test(); + testSetupLocator(); + // locator.registerSingleton(LikeButtonViewModel()); + + setUp(() { + registerServices(); + }); + + TestWidgetsFlutterBinding.ensureInitialized(); + testWidgets('check if createOrganizationFeedScreen shows up', (tester) async { + final model = locator(); + await tester.pumpWidget(createOrganizationFeedScreen(homeModel: model)); + await tester.pump(); + + final finder = find.byType(Scaffold); + + expect(finder, findsNWidgets(2)); + }); + testWidgets('check if orgname is displayed shows up', (tester) async { + final model = locator(); + await tester.pumpWidget(createOrganizationFeedScreen(homeModel: model)); + await tester.pump(); + + final finder = find.byType(Text); + expect(finder, findsNWidgets(4)); + // expect(text, findsOneWidget); + }); + testWidgets('check if pinned post is displayed shows up', (tester) async { + final model = locator(); + await tester.pumpWidget(createOrganizationFeedScreen(homeModel: model)); + await tester.pump(); + + final finder = find.byType(Text); + + final text1 = find.text('Rutvik Chandla'); + + final text2 = find.text( + 'Flutter is Google’s mobile UI framework for crafting high-quality native interfaces on iOS...'); + + final text3 = find.text('See all Pinned news'); + + expect(text1, findsOneWidget); + + expect(text2, findsOneWidget); + + expect(text3, findsOneWidget); + + expect(finder, findsNWidgets(4)); + }); + testWidgets('check if side drawer shows up', (tester) async { + final model = locator(); + await tester.pumpWidget(createOrganizationFeedScreen(homeModel: model)); + await tester.pump(); + + final finder = find.byIcon(Icons.menu); + + await tester.tap(finder); + await tester.pump(); + }); + testWidgets('check if post shows up when model.posts.isNotEmpty is true', + (tester) async { + final model = locator(); + await tester.pumpWidget(createOrganizationFeedScreen(homeModel: model)); + await tester.pump(); + + final finder = find.byIcon(Icons.menu); + + await tester.tap(finder); + await tester.pump(); + }); + testWidgets('check if refresh indicator is launched on dragging', + (tester) async { + final model = locator(); + await tester.pumpWidget(createOrganizationFeedScreen(homeModel: model)); + await tester.pump(); + final postservice = locator(); + bool refreshed = false; + + when(postservice.getPosts()).thenAnswer((_) async { + refreshed = true; + }); + + await tester.drag( + find.byType(RefreshIndicator), + const Offset(0, 200), + ); + await tester.pumpAndSettle(); + + expect(refreshed, true); + }); + testWidgets( + 'check if post shows up when model.posts.isNotEmpty is true and post', + (tester) async { + final model = locator(); + await tester.pumpWidget(createOrganizationFeedScreen2(homeModel: model)); + await tester.pumpAndSettle(const Duration(seconds: 1)); + }); +}