refactor(tests): update password and sex widget tests to use const constructors for RegistrationStateV2

This commit is contained in:
mso7nueh
2025-12-26 13:54:10 +03:00
parent d70d172c69
commit 90746d0be7
4 changed files with 211 additions and 211 deletions

View File

@@ -1,83 +1,83 @@
import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/registration_v2/cubit/registration_cubit.dart';
import 'package:eatvio_app/features/registration_v2/cubit/registration_state.dart';
import 'package:eatvio_app/features/registration_v2/presentation/widgets/password_field.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'password_field_test.mocks.dart';
@GenerateNiceMocks([MockSpec<RegistrationCubitV2>()])
void main() {
group('PasswordField', () {
setUp(() async {
await sl.reset();
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
});
Widget createTestWidget(Widget child, RegistrationCubitV2 cubit) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: Portal(
child: BlocProvider<RegistrationCubitV2>(
create: (_) => cubit,
child: Scaffold(body: child),
),
),
);
}
testWidgets('should display password field with password from state', (WidgetTester tester) async {
// Arrange
final mockCubit = MockRegistrationCubitV2();
const testPassword = 'testPassword123';
when(mockCubit.state).thenReturn(
RegistrationStateV2(password: testPassword),
);
final widget = PasswordField(cubit: mockCubit);
// Act
await tester.pumpWidget(createTestWidget(widget, mockCubit));
await tester.pump();
// Assert
expect(find.byType(PasswordField), findsOneWidget);
expect(find.text(testPassword), findsOneWidget);
});
testWidgets('should show copy icon initially', (WidgetTester tester) async {
// Arrange
final mockCubit = MockRegistrationCubitV2();
when(mockCubit.state).thenReturn(
RegistrationStateV2(password: 'test'),
);
final widget = PasswordField(cubit: mockCubit);
// Act
await tester.pumpWidget(createTestWidget(widget, mockCubit));
await tester.pump();
// Assert
expect(find.byType(PasswordField), findsOneWidget);
});
});
}
import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/registration_v2/cubit/registration_cubit.dart';
import 'package:eatvio_app/features/registration_v2/cubit/registration_state.dart';
import 'package:eatvio_app/features/registration_v2/presentation/widgets/password_field.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'password_field_test.mocks.dart';
@GenerateNiceMocks([MockSpec<RegistrationCubitV2>()])
void main() {
group('PasswordField', () {
setUp(() async {
await sl.reset();
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
});
Widget createTestWidget(Widget child, RegistrationCubitV2 cubit) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: Portal(
child: BlocProvider<RegistrationCubitV2>(
create: (_) => cubit,
child: Scaffold(body: child),
),
),
);
}
testWidgets('should display password field with password from state', (WidgetTester tester) async {
// Arrange
final mockCubit = MockRegistrationCubitV2();
const testPassword = 'testPassword123';
when(mockCubit.state).thenReturn(
const RegistrationStateV2(password: testPassword),
);
final widget = PasswordField(cubit: mockCubit);
// Act
await tester.pumpWidget(createTestWidget(widget, mockCubit));
await tester.pump();
// Assert
expect(find.byType(PasswordField), findsOneWidget);
expect(find.text(testPassword), findsOneWidget);
});
testWidgets('should show copy icon initially', (WidgetTester tester) async {
// Arrange
final mockCubit = MockRegistrationCubitV2();
when(mockCubit.state).thenReturn(
const RegistrationStateV2(password: 'test'),
);
final widget = PasswordField(cubit: mockCubit);
// Act
await tester.pumpWidget(createTestWidget(widget, mockCubit));
await tester.pump();
// Assert
expect(find.byType(PasswordField), findsOneWidget);
});
});
}

View File

@@ -42,14 +42,14 @@ void main() {
testWidgets('displays sex widget', (tester) async {
when(mockCubit.state).thenReturn(
RegistrationStateV2(
const RegistrationStateV2(
step: 0,
gender: '',
isGenderValid: true,
),
);
when(mockCubit.stream).thenAnswer((_) => Stream.value(
RegistrationStateV2(
const RegistrationStateV2(
step: 0,
gender: '',
isGenderValid: true,
@@ -64,14 +64,14 @@ void main() {
testWidgets('calls setGender when male is tapped', (tester) async {
when(mockCubit.state).thenReturn(
RegistrationStateV2(
const RegistrationStateV2(
step: 0,
gender: '',
isGenderValid: true,
),
);
when(mockCubit.stream).thenAnswer((_) => Stream.value(
RegistrationStateV2(
const RegistrationStateV2(
step: 0,
gender: '',
isGenderValid: true,
@@ -94,14 +94,14 @@ void main() {
testWidgets('shows error when gender is invalid', (tester) async {
when(mockCubit.state).thenReturn(
RegistrationStateV2(
const RegistrationStateV2(
step: 0,
gender: '',
isGenderValid: false,
),
);
when(mockCubit.stream).thenAnswer((_) => Stream.value(
RegistrationStateV2(
const RegistrationStateV2(
step: 0,
gender: '',
isGenderValid: false,

View File

@@ -1,118 +1,118 @@
import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/registration_v2/cubit/registration_cubit.dart';
import 'package:eatvio_app/features/registration_v2/cubit/registration_state.dart';
import 'package:eatvio_app/features/registration_v2/presentation/widgets/show_successful_registration_bottom_sheet.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'show_successful_registration_bottom_sheet_test.mocks.dart';
@GenerateNiceMocks([MockSpec<RegistrationCubitV2>()])
void main() {
group('ShowSuccessfulRegistrationBottomSheet', () {
setUp(() async {
await sl.reset();
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
});
Widget createTestWidget(Widget child, RegistrationCubitV2 cubit) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: Portal(
child: BlocProvider<RegistrationCubitV2>(
create: (_) => cubit,
child: Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showRegistrationBottomSheet(context, cubit, true);
},
child: const Text('Show Bottom Sheet'),
),
),
),
),
),
);
}
testWidgets('should display success bottom sheet when isSuccess is true', (WidgetTester tester) async {
// Arrange
final mockCubit = MockRegistrationCubitV2();
when(mockCubit.state).thenReturn(
RegistrationStateV2(password: 'testPassword'),
);
// Act
await tester.pumpWidget(createTestWidget(const SizedBox(), mockCubit));
await tester.tap(find.text('Show Bottom Sheet'));
await tester.pumpAndSettle();
// Assert
expect(find.byType(RegistrationBottomSheet), findsOneWidget);
});
testWidgets('should display error bottom sheet when isSuccess is false', (WidgetTester tester) async {
// Arrange
final mockCubit = MockRegistrationCubitV2();
when(mockCubit.state).thenReturn(
RegistrationStateV2(),
);
Widget createTestWidgetError(Widget child, RegistrationCubitV2 cubit) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: Portal(
child: BlocProvider<RegistrationCubitV2>(
create: (_) => cubit,
child: Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showRegistrationBottomSheet(context, cubit, false);
},
child: const Text('Show Bottom Sheet'),
),
),
),
),
),
);
}
// Act
await tester.pumpWidget(createTestWidgetError(const SizedBox(), mockCubit));
await tester.tap(find.text('Show Bottom Sheet'));
await tester.pumpAndSettle();
// Assert
expect(find.byType(RegistrationBottomSheet), findsOneWidget);
});
});
}
import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/registration_v2/cubit/registration_cubit.dart';
import 'package:eatvio_app/features/registration_v2/cubit/registration_state.dart';
import 'package:eatvio_app/features/registration_v2/presentation/widgets/show_successful_registration_bottom_sheet.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'show_successful_registration_bottom_sheet_test.mocks.dart';
@GenerateNiceMocks([MockSpec<RegistrationCubitV2>()])
void main() {
group('ShowSuccessfulRegistrationBottomSheet', () {
setUp(() async {
await sl.reset();
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
});
Widget createTestWidget(Widget child, RegistrationCubitV2 cubit) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: Portal(
child: BlocProvider<RegistrationCubitV2>(
create: (_) => cubit,
child: Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showRegistrationBottomSheet(context, cubit, true);
},
child: const Text('Show Bottom Sheet'),
),
),
),
),
),
);
}
testWidgets('should display success bottom sheet when isSuccess is true', (WidgetTester tester) async {
// Arrange
final mockCubit = MockRegistrationCubitV2();
when(mockCubit.state).thenReturn(
const RegistrationStateV2(password: 'testPassword'),
);
// Act
await tester.pumpWidget(createTestWidget(const SizedBox(), mockCubit));
await tester.tap(find.text('Show Bottom Sheet'));
await tester.pumpAndSettle();
// Assert
expect(find.byType(RegistrationBottomSheet), findsOneWidget);
});
testWidgets('should display error bottom sheet when isSuccess is false', (WidgetTester tester) async {
// Arrange
final mockCubit = MockRegistrationCubitV2();
when(mockCubit.state).thenReturn(
const RegistrationStateV2(),
);
Widget createTestWidgetError(Widget child, RegistrationCubitV2 cubit) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: Portal(
child: BlocProvider<RegistrationCubitV2>(
create: (_) => cubit,
child: Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showRegistrationBottomSheet(context, cubit, false);
},
child: const Text('Show Bottom Sheet'),
),
),
),
),
),
);
}
// Act
await tester.pumpWidget(createTestWidgetError(const SizedBox(), mockCubit));
await tester.tap(find.text('Show Bottom Sheet'));
await tester.pumpAndSettle();
// Assert
expect(find.byType(RegistrationBottomSheet), findsOneWidget);
});
});
}

View File

@@ -43,9 +43,9 @@ void main() {
testWidgets('displays step widget', (tester) async {
when(mockCubit.state).thenReturn(
RegistrationStateV2(step: 0),
const RegistrationStateV2(step: 0),
);
when(mockCubit.stream).thenAnswer((_) => Stream.value(RegistrationStateV2(step: 0)));
when(mockCubit.stream).thenAnswer((_) => Stream.value(const RegistrationStateV2(step: 0)));
await tester.pumpWidget(createTestWidget());
await tester.pumpAndSettle();
@@ -55,9 +55,9 @@ void main() {
testWidgets('displays correct step number', (tester) async {
when(mockCubit.state).thenReturn(
RegistrationStateV2(step: 2),
const RegistrationStateV2(step: 2),
);
when(mockCubit.stream).thenAnswer((_) => Stream.value(RegistrationStateV2(step: 2)));
when(mockCubit.stream).thenAnswer((_) => Stream.value(const RegistrationStateV2(step: 2)));
await tester.pumpWidget(createTestWidget());
await tester.pumpAndSettle();