From 09db9f68b7c5136dd4220d5b9adda6da1f116f54 Mon Sep 17 00:00:00 2001 From: Louismaestre Date: Thu, 21 Nov 2024 18:52:18 +0100 Subject: [PATCH] [ADD] Creation of container profil test and increase coverage for company-profil folder --- .../company-profil/company-profil.dart | 1 + .../company-profil/container-profil.dart | 38 +++-- .../front/test/company_profil_test.dart | 80 ++++++++++ .../front/test/container_profil_test.dart | 147 ++++++++++++++++++ 4 files changed, 254 insertions(+), 12 deletions(-) create mode 100644 Configurateur/front/test/container_profil_test.dart diff --git a/Configurateur/front/lib/screens/company-profil/company-profil.dart b/Configurateur/front/lib/screens/company-profil/company-profil.dart index 05665c431..130ddb0e2 100644 --- a/Configurateur/front/lib/screens/company-profil/company-profil.dart +++ b/Configurateur/front/lib/screens/company-profil/company-profil.dart @@ -663,6 +663,7 @@ class CompanyProfilPageState extends State { ), isManager ? ElevatedButton( + key: const Key("isManager"), style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric( horizontal: 25, vertical: 15), diff --git a/Configurateur/front/lib/screens/company-profil/container-profil.dart b/Configurateur/front/lib/screens/company-profil/container-profil.dart index ecedb87c3..7a240ee38 100644 --- a/Configurateur/front/lib/screens/company-profil/container-profil.dart +++ b/Configurateur/front/lib/screens/company-profil/container-profil.dart @@ -29,22 +29,34 @@ class ContainerProfilPage extends StatefulWidget { const ContainerProfilPage({super.key}); @override - _ContainerProfilPageState createState() => _ContainerProfilPageState(); + ContainerProfilPageState createState() => ContainerProfilPageState(); } /// CompanyProfilPageState /// -class _ContainerProfilPageState extends State { - _ContainerProfilPageState(); - late List items; +class ContainerProfilPageState extends State { + ContainerProfilPageState(); + late List items = []; late String itemName = ''; late String itemDesc = ''; - late String city; - late String address; - late String saveName; - late String information; + late String city = ''; + late String address = ''; + late String saveName = ''; + late String information = ''; late int containerId; - late ContainerListData tmp; + late ContainerListData tmp = ContainerListData( + id: null, + createdAt: null, + organization: null, + organizationId: null, + containerMapping: null, + price: null, + address: null, + city: null, + design: null, + informations: null, + saveName: null, + ); String jwtToken = ''; /// [Function] : get information about the containers @@ -571,6 +583,7 @@ class _ContainerProfilPageState extends State { mainAxisSize: MainAxisSize.min, children: [ IconButton( + key: const Key("edit-item"), icon: Icon( Icons.mode_outlined, color: Provider.of(context).isDark @@ -661,8 +674,8 @@ class _ContainerProfilPageState extends State { child: Column( children: [ SizedBox( - width: 500, - height: 200, + // width: 500, + // height: 200, child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ @@ -775,7 +788,7 @@ class _ContainerProfilPageState extends State { ), const SizedBox(width: 5.0), InkWell( - key: const Key("edit-city"), + key: const Key("edit-address"), onTap: () async { await showEditPopupAddress(context, address, (String newAddress) { @@ -819,6 +832,7 @@ class _ContainerProfilPageState extends State { height: 30, ), ElevatedButton( + key: Key("objectCreation"), onPressed: () { storageService.writeStorage( 'containerId', diff --git a/Configurateur/front/test/company_profil_test.dart b/Configurateur/front/test/company_profil_test.dart index a7c4d4218..4a376f2b6 100644 --- a/Configurateur/front/test/company_profil_test.dart +++ b/Configurateur/front/test/company_profil_test.dart @@ -71,6 +71,11 @@ void main() { await tester.pump(const Duration(seconds: 3)); + // expect(find.text("L'entreprise ne possède pas de nom"), findsOneWidget); + // expect(find.text("Aucune information disponible"), findsOneWidget); + // expect(find.text("Pas de type disponible"), findsOneWidget); + // expect(find.text("Gérer les membres"), findsOneWidget); + // Edit information await tester.tap(find.byKey(const Key('edit-information'))); await tester.pump(const Duration(seconds: 3)); @@ -96,8 +101,83 @@ void main() { await tester.tap(find.byKey(const Key('button-information'))); await tester.pump(const Duration(seconds: 3)); + //EDIT information + await tester.tap(find.byKey(const Key('edit-type'))); + await tester.pump(const Duration(seconds: 3)); + + await tester.tap(find.byKey(const Key('cancel-edit-type'))); + await tester.pump(const Duration(seconds: 3)); + + await tester.tap(find.byKey(const Key('edit-type'))); + await tester.pump(const Duration(seconds: 3)); + + expect(find.byType(AlertDialog), findsOneWidget); + expect(find.text('Modifier'), findsNWidgets(2)); + expect(find.text('Annuler'), findsOneWidget); + + await tester.enterText(find.byKey(const Key('type')), 'info orga'); + await tester.tap(find.byKey(const Key('button-type'))); + await tester.pump(const Duration(seconds: 3)); + expect(find.byType(CustomFooter), findsOneWidget); }); + + testWidgets('Test empty company profil page', (WidgetTester tester) async { + when(sharedPreferences.getString('token')).thenReturn('test-token'); + when(sharedPreferences.getString('tokenExpiration')).thenReturn( + DateTime.now().add(const Duration(minutes: 30)).toIso8601String()); + + await tester.binding.setSurfaceSize(const Size(5000, 5000)); + + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider( + create: (_) => ThemeService(), + ), + ], + child: Sizer( + builder: (context, orientation, deviceType) { + return MaterialApp( + theme: ThemeData(fontFamily: 'Roboto'), + home: InheritedGoRouter( + goRouter: AppRouter.router, + child: const CompanyProfilPage(), + ), + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + locale: Locale('fr'), + ); + }, + ), + ), + ); + final state_assign = + tester.state(find.byType(CompanyProfilPage)) as CompanyProfilPageState; + + state_assign.setState(() { + state_assign.organization = OrganizationList( + id: 1, + name: null, + type: null, + affiliate: null, + containers: null, + contactInformation: null, + ); + }); + + state_assign.setState(() { + state_assign.isManager = true; + }); + + await tester.pump(const Duration(seconds: 3)); + + expect(find.text("L'entreprise ne possède pas de nom"), findsOneWidget); + expect(find.text("Aucune information disponible"), findsOneWidget); + expect(find.text("Pas de type disponible"), findsOneWidget); + // expect(find.text("Pas de type disponible"), findsOneWidget); + await tester.tap(find.byKey(const Key('isManager'))); + }); } class MockSharedPreferences extends Mock implements SharedPreferences {} diff --git a/Configurateur/front/test/container_profil_test.dart b/Configurateur/front/test/container_profil_test.dart new file mode 100644 index 000000000..ee2d26944 --- /dev/null +++ b/Configurateur/front/test/container_profil_test.dart @@ -0,0 +1,147 @@ +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:front/app_routes.dart'; +import 'package:front/components/container.dart'; +import 'package:front/components/items-information.dart'; +import 'package:front/screens/company-profil/container-profil.dart'; +import 'package:flutter/material.dart'; +import 'package:front/services/theme_service.dart'; +import 'package:go_router/go_router.dart'; +import 'package:mockito/mockito.dart'; +import 'package:provider/provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:sizer/sizer.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + late MockSharedPreferences sharedPreferences; + + setUp(() async { + sharedPreferences = MockSharedPreferences(); + final roboto = rootBundle.load('assets/roboto/Roboto-Medium.ttf'); + final fontLoader = FontLoader('Roboto')..addFont(roboto); + await fontLoader.load(); + }); + + testWidgets('Container profil screen', (WidgetTester tester) async { + tester.binding.window.physicalSizeTestValue = const Size(5000, 5000); + tester.binding.window.devicePixelRatioTestValue = 1.0; + + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider( + create: (_) => ThemeService(), + ), + ], + child: Sizer( + builder: (context, orientation, deviceType) { + return MaterialApp( + theme: ThemeData(fontFamily: 'Roboto'), + home: InheritedGoRouter( + goRouter: AppRouter.router, + child: const ContainerProfilPage(), + ), + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + locale: const Locale('fr'), + ); + }, + ), + ), + ); + + await tester.pumpAndSettle(const Duration(seconds: 2)); + + expect(find.byType(ContainerProfilPage), findsOneWidget); + expect(find.byType(AppBar), findsNothing); + + await tester.tap(find.byKey(const Key('edit-city'))); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const Key('cancel-edit-city'))); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const Key('edit-address'))); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const Key('cancel-edit-address'))); + await tester.pumpAndSettle(); + + + expect(find.text('Aucun objet trouvé.'), findsOneWidget); + expect(find.text('Ville : Pas de ville associée'), findsOneWidget); + expect(find.text('Adresse : Aucune adresse'), findsOneWidget); + }); + + testWidgets('Container profil screen with city and address', + (WidgetTester tester) async { + tester.binding.window.physicalSizeTestValue = const Size(5000, 5000); + tester.binding.window.devicePixelRatioTestValue = 1.0; + + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider( + create: (_) => ThemeService(), + ), + ], + child: Sizer( + builder: (context, orientation, deviceType) { + return MaterialApp( + theme: ThemeData(fontFamily: 'Roboto'), + home: InheritedGoRouter( + goRouter: AppRouter.router, + child: const ContainerProfilPage(), + ), + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + locale: const Locale('fr'), + ); + }, + ), + ), + ); + + final state_assign = tester.state(find.byType(ContainerProfilPage)) + as ContainerProfilPageState; + + state_assign.setState(() { + state_assign.tmp = ContainerListData( + id: 1, + createdAt: null, + organization: null, + organizationId: 1, + containerMapping: null, + price: 2, + address: 'rue george', + city: 'batz sur mer', + design: null, + informations: 'informations', + saveName: 'saveName'); + }); + + state_assign.setState(() { + state_assign.items = [ + ItemList(id: 1, name: "bonsoir", available: true, container: null, createdAt: null, containerId: 1, price: 2, image: null, description: "description", category: null, status: null) + ]; + }); + + await tester.pumpAndSettle(const Duration(seconds: 2)); + expect(find.text("Adresse : rue george"), findsOneWidget); + expect(find.text("Nom de la ville : batz sur mer"), findsOneWidget); + + expect(find.text("Nom : bonsoir"), findsOneWidget); + expect(find.text("Description : description"), findsOneWidget); + + await tester.tap(find.byKey(const Key('edit-item'))); + await tester.pump(const Duration(seconds: 3)); + await tester.tap(find.byKey(const Key('cancel-edit-item'))); + await tester.pump(const Duration(seconds: 3)); + + }); +} + +class MockSharedPreferences extends Mock implements SharedPreferences {}