From 77cd6620582b5c7ca87639d969fa127a7c1ad724 Mon Sep 17 00:00:00 2001 From: Parag Gupta <103507835+Dante291@users.noreply.github.com> Date: Wed, 20 Dec 2023 00:08:12 +0530 Subject: [PATCH] Test for join_organisation_after_auth.dart (#2260) * Test for join_organisation_after_auth.dart * modify * adding more tests for complete coverage --- .../join_organisation_after_auth_test.dart | 115 ++++++++++++++++-- 1 file changed, 103 insertions(+), 12 deletions(-) diff --git a/test/views/after_auth_screens/join_organisation_after_auth_test.dart b/test/views/after_auth_screens/join_organisation_after_auth_test.dart index 53d67e89d4..1fdcef83b8 100644 --- a/test/views/after_auth_screens/join_organisation_after_auth_test.dart +++ b/test/views/after_auth_screens/join_organisation_after_auth_test.dart @@ -4,26 +4,31 @@ 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:qr_code_scanner/qr_code_scanner.dart'; import 'package:talawa/enums/enums.dart'; import 'package:talawa/models/organization/org_info.dart'; import 'package:talawa/models/user/user_info.dart'; import 'package:talawa/services/graphql_config.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/pre_auth_view_models/select_organization_view_model.dart'; import 'package:talawa/views/after_auth_screens/join_org_after_auth/join_organisation_after_auth.dart'; import 'package:talawa/views/base_view.dart'; import 'package:talawa/widgets/organization_search_list.dart'; import '../../helpers/test_helpers.dart'; +import '../../helpers/test_helpers.mocks.dart'; import '../../helpers/test_locator.dart'; -Widget createJoinOrgAfterAuth({String orgId = "fake_id"}) { - return BaseView( - onModelReady: (model) => model.initialize(), - builder: (context, langModel, child) { +Widget createJoinOrgAfterAuth({ + String orgId = "fake_id", +}) { + return BaseView( + onModelReady: (model) => model.initialise(orgId), + builder: (context, model, child) { return MaterialApp( + navigatorKey: navigationService.navigatorKey, locale: const Locale('en'), localizationsDelegates: const [ AppLocalizationsDelegate(isTest: true), @@ -53,6 +58,99 @@ void main() { }); group("Tests for JoinOrganizationAfterAuth - widgets", () { + testWidgets('QR Scan Test', (WidgetTester tester) async { + final controller = MockQRViewController(); + when(controller.scannedDataStream).thenAnswer((_) async* { + yield Barcode( + ' ' + '?orgid=6737904485008f171cf29924', + BarcodeFormat.qrcode, + null, + ); + }); + when(controller.stopCamera()) + .thenAnswer((realInvocation) => Future.value()); + + await tester.pumpWidget( + createJoinOrgAfterAuth(), + ); + + await tester.pumpAndSettle(const Duration(seconds: 6)); + + await tester.tap(find.byIcon(Icons.qr_code_scanner)); + await tester.pumpAndSettle(); + + expect( + find.byWidgetPredicate( + (widget) => + widget is ClipRRect && + widget.child is Container && + (widget.child! as Container).child is Column, + ), + findsOneWidget, + ); + (tester.widget(find.byType(QRView)) as QRView) + .onQRViewCreated(controller); + }); + testWidgets('QR Scan Test when url != GraphqlConfig.orgURI', + (WidgetTester tester) async { + final controller = MockQRViewController(); + when(controller.scannedDataStream).thenAnswer((_) async* { + yield Barcode( + '1' + '?orgid=6737904485008f171cf29924', + BarcodeFormat.qrcode, + null, + ); + }); + when(controller.stopCamera()) + .thenAnswer((realInvocation) => Future.value()); + + await tester.pumpWidget( + createJoinOrgAfterAuth(), + ); + + await tester.pumpAndSettle(const Duration(seconds: 6)); + + await tester.tap(find.byIcon(Icons.qr_code_scanner)); + await tester.pumpAndSettle(); + + expect( + find.byWidgetPredicate( + (widget) => + widget is ClipRRect && + widget.child is Container && + (widget.child! as Container).child is Column, + ), + findsOneWidget, + ); + (tester.widget(find.byType(QRView)) as QRView) + .onQRViewCreated(controller); + }); + testWidgets('Test _onQRViewCreated when throwing exception', + (WidgetTester tester) async { + final controller = MockQRViewController(); + when(controller.scannedDataStream).thenAnswer((_) async* { + yield Barcode( + ' ' + '?orgid=6737904485008f171cf29924', + BarcodeFormat.qrcode, + null, + ); + }); + when(controller.stopCamera()) + .thenAnswer((realInvocation) => Future.value()); + + await tester.pumpWidget( + createJoinOrgAfterAuth(), + ); + when(controller.stopCamera()).thenThrow(Exception("exception")); + + await tester.pumpAndSettle(const Duration(seconds: 6)); + + await tester.tap(find.byIcon(Icons.qr_code_scanner)); + await tester.pumpAndSettle(); + + (tester.widget(find.byType(QRView)) as QRView) + .onQRViewCreated(controller); + }); testWidgets( "Check if JoinOrganizationsAfterAuth shows up", (tester) async { @@ -132,13 +230,6 @@ void main() { /// Search is No-Longer is a feature, if it gets implemented in future use this test /// Really good test to learn from so not deleting testWidgets("Check if model related functions work", (tester) async { - // Registers a singleton, which means that every instance of - // SelectOrganizationViewModel will be the same. - locator.unregister(); - locator.registerSingleton( - SelectOrganizationViewModel(), - ); - final orgOne = OrgInfo( name: "org_one", creatorInfo: User(