Skip to content

Commit

Permalink
[ADD] Creation of container profil test and increase coverage for com…
Browse files Browse the repository at this point in the history
…pany-profil folder
  • Loading branch information
Louismaestre committed Nov 21, 2024
1 parent 9ca8c24 commit 09db9f6
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ class CompanyProfilPageState extends State<CompanyProfilPage> {
),
isManager
? ElevatedButton(
key: const Key("isManager"),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 25, vertical: 15),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,34 @@ class ContainerProfilPage extends StatefulWidget {
const ContainerProfilPage({super.key});

@override
_ContainerProfilPageState createState() => _ContainerProfilPageState();
ContainerProfilPageState createState() => ContainerProfilPageState();
}

/// CompanyProfilPageState
///
class _ContainerProfilPageState extends State<ContainerProfilPage> {
_ContainerProfilPageState();
late List<ItemList> items;
class ContainerProfilPageState extends State<ContainerProfilPage> {
ContainerProfilPageState();
late List<ItemList> 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
Expand Down Expand Up @@ -571,6 +583,7 @@ class _ContainerProfilPageState extends State<ContainerProfilPage> {
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
key: const Key("edit-item"),
icon: Icon(
Icons.mode_outlined,
color: Provider.of<ThemeService>(context).isDark
Expand Down Expand Up @@ -661,8 +674,8 @@ class _ContainerProfilPageState extends State<ContainerProfilPage> {
child: Column(
children: [
SizedBox(
width: 500,
height: 200,
// width: 500,
// height: 200,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expand Down Expand Up @@ -775,7 +788,7 @@ class _ContainerProfilPageState extends State<ContainerProfilPage> {
),
const SizedBox(width: 5.0),
InkWell(
key: const Key("edit-city"),
key: const Key("edit-address"),
onTap: () async {
await showEditPopupAddress(context, address,
(String newAddress) {
Expand Down Expand Up @@ -819,6 +832,7 @@ class _ContainerProfilPageState extends State<ContainerProfilPage> {
height: 30,
),
ElevatedButton(
key: Key("objectCreation"),
onPressed: () {
storageService.writeStorage(
'containerId',
Expand Down
80 changes: 80 additions & 0 deletions Configurateur/front/test/company_profil_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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<ThemeService>(
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 {}
147 changes: 147 additions & 0 deletions Configurateur/front/test/container_profil_test.dart
Original file line number Diff line number Diff line change
@@ -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<ThemeService>(
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<ThemeService>(
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 {}

0 comments on commit 09db9f6

Please sign in to comment.