test(tests): add comprehensive unit tests for various models and repositories, enhancing test coverage and ensuring correct functionality

This commit is contained in:
mso7nueh
2025-12-09 16:19:48 +03:00
parent edd064d20d
commit 70e4a42e98
25 changed files with 2565 additions and 58 deletions

View File

@@ -1,14 +1,25 @@
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/comments/presentation/comments_actions_bottom_sheet.dart';
import 'package:eatvio_app/core/widgets/bottom_sheet_action_widget.dart';
import 'package:eatvio_app/core/widgets/custom_text_form_field.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
void main() {
group('CommentsActionsBottomSheet', () {
Widget createTestWidget(Widget child) {
return MaterialApp(
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(body: child),
),
],
);
return MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
@@ -17,9 +28,7 @@ void main() {
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
home: Scaffold(
body: child,
),
routerConfig: router,
);
}
@@ -44,15 +53,7 @@ void main() {
testWidgets('displays edit option for my comment', (tester) async {
await tester.pumpWidget(
createTestWidget(
CommentsActionsBottomSheet(
isMyComment: true,
reply: null,
copy: () {},
edit: (text) {},
delete: () {},
complain: () {},
commentText: 'Test comment',
),
CommentsActionsBottomSheet(isMyComment: true, reply: null, copy: () {}, edit: (text) {}, delete: () {}, complain: () {}, commentText: 'Test comment'),
),
);
@@ -80,19 +81,523 @@ void main() {
testWidgets('shows edit mode when edit is tapped', (tester) async {
await tester.pumpWidget(
createTestWidget(
CommentsActionsBottomSheet(
isMyComment: true,
reply: null,
copy: () {},
edit: (text) {},
delete: () {},
complain: () {},
commentText: 'Test comment',
),
CommentsActionsBottomSheet(isMyComment: true, reply: null, copy: () {}, edit: (text) {}, delete: () {}, complain: () {}, commentText: 'Test comment'),
),
);
expect(find.byType(CommentsActionsBottomSheet), findsOneWidget);
});
testWidgets('initializes with comment text in controller', (tester) async {
const commentText = 'Initial comment text';
await tester.pumpWidget(
createTestWidget(
CommentsActionsBottomSheet(isMyComment: true, reply: null, copy: () {}, edit: (text) {}, delete: () {}, complain: () {}, commentText: commentText),
),
);
expect(find.byType(CommentsActionsBottomSheet), findsOneWidget);
});
testWidgets('calls copy callback when copy is tapped', (tester) async {
bool copyCalled = false;
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showCommentsActionsBottomSheet(
context,
reply: () {},
copy: () {
copyCalled = true;
},
edit: (text) {},
delete: () {},
complain: () {},
commentText: 'Test comment',
isMyComment: false,
);
},
child: const Text('Show'),
),
),
),
),
],
);
await tester.pumpWidget(
MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
routerConfig: router,
),
);
await tester.tap(find.text('Show'));
await tester.pumpAndSettle();
// Находим кнопку копирования (вторая кнопка для not my comment)
final actionWidgets = find.byType(BottomSheetActionWidget);
expect(actionWidgets, findsWidgets);
await tester.tap(actionWidgets.at(1)); // copy button
await tester.pump();
expect(copyCalled, isTrue);
});
testWidgets('calls delete callback when delete is tapped for my comment', (tester) async {
bool deleteCalled = false;
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showCommentsActionsBottomSheet(
context,
reply: null,
copy: () {},
edit: (text) {},
delete: () {
deleteCalled = true;
},
complain: () {},
commentText: 'Test comment',
isMyComment: true,
);
},
child: const Text('Show'),
),
),
),
),
],
);
await tester.pumpWidget(
MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
routerConfig: router,
),
);
await tester.tap(find.text('Show'));
await tester.pumpAndSettle();
// Находим кнопку удаления (последняя кнопка для my comment)
final actionWidgets = find.byType(BottomSheetActionWidget);
expect(actionWidgets, findsWidgets);
await tester.tap(actionWidgets.last); // delete button
await tester.pump();
expect(deleteCalled, isTrue);
});
testWidgets('calls complain callback when complain is tapped for not my comment', (tester) async {
bool complainCalled = false;
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showCommentsActionsBottomSheet(
context,
reply: () {},
copy: () {},
edit: (text) {},
delete: () {},
complain: () {
complainCalled = true;
},
commentText: 'Test comment',
isMyComment: false,
);
},
child: const Text('Show'),
),
),
),
),
],
);
await tester.pumpWidget(
MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
routerConfig: router,
),
);
await tester.tap(find.text('Show'));
await tester.pumpAndSettle();
// Находим кнопку жалобы (последняя кнопка для not my comment)
final actionWidgets = find.byType(BottomSheetActionWidget);
expect(actionWidgets, findsWidgets);
await tester.tap(actionWidgets.last); // complain button
await tester.pump();
expect(complainCalled, isTrue);
});
testWidgets('shows edit text field when edit button is tapped', (tester) async {
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showCommentsActionsBottomSheet(
context,
reply: null,
copy: () {},
edit: (text) {},
delete: () {},
complain: () {},
commentText: 'Test comment',
isMyComment: true,
);
},
child: const Text('Show'),
),
),
),
),
],
);
await tester.pumpWidget(
MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
routerConfig: router,
),
);
await tester.tap(find.text('Show'));
await tester.pumpAndSettle();
expect(find.byType(CommentsActionsBottomSheet), findsOneWidget);
// Нажимаем на кнопку редактирования
final editButtons = find.byType(BottomSheetActionWidget);
expect(editButtons, findsWidgets);
await tester.tap(editButtons.first);
await tester.pumpAndSettle();
// После нажатия на кнопку редактирования должен появиться текстовое поле
expect(find.byType(CustomTextFormField), findsOneWidget);
});
testWidgets('showCommentsActionsBottomSheet - отображает bottom sheet', (tester) async {
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
showCommentsActionsBottomSheet(
context,
reply: () {},
copy: () {},
edit: (text) {},
delete: () {},
complain: () {},
commentText: 'Test comment',
isMyComment: false,
);
},
child: const Text('Show Bottom Sheet'),
),
),
),
),
],
);
await tester.pumpWidget(
MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
routerConfig: router,
),
);
await tester.tap(find.text('Show Bottom Sheet'));
await tester.pumpAndSettle();
expect(find.byType(CommentsActionsBottomSheet), findsOneWidget);
});
testWidgets('edit mode - показывает текстовое поле и кнопку отправки', (tester) async {
String? editedText;
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showCommentsActionsBottomSheet(
context,
reply: null,
copy: () {},
edit: (text) {
editedText = text;
},
delete: () {},
complain: () {},
commentText: 'Original comment',
isMyComment: true,
);
},
child: const Text('Show'),
),
),
),
),
],
);
await tester.pumpWidget(
MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
routerConfig: router,
),
);
await tester.tap(find.text('Show'));
await tester.pumpAndSettle();
// Находим и нажимаем на кнопку редактирования (первая кнопка для my comment)
final editButtons = find.byType(BottomSheetActionWidget);
expect(editButtons, findsWidgets);
await tester.tap(editButtons.first);
await tester.pumpAndSettle();
// Проверяем, что появилось текстовое поле
expect(find.byType(CustomTextFormField), findsOneWidget);
// Вводим новый текст
await tester.enterText(find.byType(CustomTextFormField), 'Edited comment');
await tester.pump();
// Нажимаем на кнопку отправки (текст достаточно длинный > 2 символов)
final sendButton = find.byIcon(Icons.send_rounded);
expect(sendButton, findsOneWidget);
// Используем pump() вместо pumpAndSettle() чтобы избежать ошибок с GoRouter.pop()
await tester.tap(sendButton);
await tester.pump();
expect(editedText, 'Edited comment');
});
testWidgets('edit mode - кнопка отправки неактивна при коротком тексте', (tester) async {
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showCommentsActionsBottomSheet(
context,
reply: null,
copy: () {},
edit: (text) {},
delete: () {},
complain: () {},
commentText: 'Original',
isMyComment: true,
);
},
child: const Text('Show'),
),
),
),
),
],
);
await tester.pumpWidget(
MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
routerConfig: router,
),
);
await tester.tap(find.text('Show'));
await tester.pumpAndSettle();
// Находим и нажимаем на кнопку редактирования
final editButtons = find.byType(BottomSheetActionWidget);
expect(editButtons, findsWidgets);
await tester.tap(editButtons.first);
await tester.pumpAndSettle();
// Вводим короткий текст (меньше 3 символов)
await tester.enterText(find.byType(CustomTextFormField), 'Hi');
await tester.pump();
// Кнопка отправки должна быть неактивной (серая)
expect(find.byType(CustomTextFormField), findsOneWidget);
expect(find.byIcon(Icons.send_rounded), findsOneWidget);
});
testWidgets('displays delete option for my comment', (tester) async {
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showCommentsActionsBottomSheet(
context,
reply: () {},
copy: () {},
edit: (text) {},
delete: () {},
complain: () {},
commentText: 'Test comment',
isMyComment: true,
);
},
child: const Text('Show'),
),
),
),
),
],
);
await tester.pumpWidget(
MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
routerConfig: router,
),
);
await tester.tap(find.text('Show'));
await tester.pumpAndSettle();
expect(find.byType(CommentsActionsBottomSheet), findsOneWidget);
});
testWidgets('displays complain option for not my comment', (tester) async {
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showCommentsActionsBottomSheet(
context,
reply: null,
copy: () {},
edit: (text) {},
delete: () {},
complain: () {},
commentText: 'Test comment',
isMyComment: false,
);
},
child: const Text('Show'),
),
),
),
),
],
);
await tester.pumpWidget(
MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
routerConfig: router,
),
);
await tester.tap(find.text('Show'));
await tester.pumpAndSettle();
expect(find.byType(CommentsActionsBottomSheet), findsOneWidget);
});
});
}