forked from eatvio/eatvio_app
feat(tests): increased tests coverage
This commit is contained in:
@@ -3,13 +3,13 @@ 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/menu/presentation/blocked_user_cubit/blocked_users_cubit.dart';
|
||||
import 'package:eatvio_app/features/menu/presentation/blocked_user_cubit/blocked_users_state.dart';
|
||||
import 'package:eatvio_app/features/menu/presentation/widgets/blocked_user_widget.dart';
|
||||
import 'package:eatvio_app/features/profile/models/profile_model.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:network_image_mock/network_image_mock.dart';
|
||||
@@ -18,7 +18,7 @@ import 'blocked_user_widget_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<BlockedUsersCubit>()])
|
||||
void main() {
|
||||
group('BlockedUserWidget', () {
|
||||
group('BlockedUserWidget smoke', () {
|
||||
setUpAll(() async {
|
||||
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
|
||||
});
|
||||
@@ -28,137 +28,50 @@ void main() {
|
||||
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
|
||||
});
|
||||
|
||||
Widget createTestWidget(ProfileModel profile, BlockedUsersCubit cubit) {
|
||||
return MaterialApp.router(
|
||||
Widget wrap(Widget child, BlockedUsersCubit cubit) {
|
||||
return MaterialApp(
|
||||
theme: ThemeHelper().themeData(),
|
||||
routerConfig: GoRouter(
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/',
|
||||
builder: (context, state) => BlocProvider<BlockedUsersCubit>(
|
||||
create: (_) => cubit,
|
||||
child: Scaffold(body: BlockedUserWidget(profile: profile)),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/profile',
|
||||
name: 'profileScreen',
|
||||
builder: (context, state) => Scaffold(body: Text('Profile: ${state.extra}')),
|
||||
),
|
||||
],
|
||||
home: Scaffold(
|
||||
body: BlocProvider<BlockedUsersCubit>.value(value: cubit, child: child),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('displays profile name correctly', (WidgetTester tester) async {
|
||||
testWidgets('renders blocked user widget', (tester) async {
|
||||
final cubit = MockBlockedUsersCubit();
|
||||
const state = BlockedUsersState();
|
||||
|
||||
when(cubit.state).thenReturn(state);
|
||||
when(cubit.stream).thenAnswer((_) => Stream.value(state));
|
||||
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(cubit.unblockUser(any)).thenAnswer((_) async => Future<void>.value());
|
||||
|
||||
const profile = ProfileModel(
|
||||
id: 1,
|
||||
slug: 'test-user',
|
||||
name: 'Test User',
|
||||
profilePhotoUrl: '/test.jpg',
|
||||
email: 'test@test.com',
|
||||
phoneConfirmationState: 'confirmed',
|
||||
isPublicProfile: false,
|
||||
isBlockedByMe: false,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
roles: [],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
);
|
||||
|
||||
await mockNetworkImagesFor(() async {
|
||||
final mockCubit = MockBlockedUsersCubit();
|
||||
const profile = ProfileModel(
|
||||
id: 1,
|
||||
name: 'Blocked User',
|
||||
email: 'blocked@example.com',
|
||||
ratingPlace: null,
|
||||
emailVerifiedAt: null,
|
||||
phoneConfirmationState: 'confirmed',
|
||||
createdAt: null,
|
||||
updatedAt: null,
|
||||
isPublicProfile: false,
|
||||
isBlockedByMe: true,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
slug: 'blocked-user',
|
||||
profilePhotoUrl: '/photo.jpg',
|
||||
roles: ['user'],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
educationResource: null,
|
||||
diplomasAndCertificates: null,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(createTestWidget(profile, mockCubit));
|
||||
await tester.pump(); // Initial frame
|
||||
await tester.pump(); // Allow image loading to start
|
||||
|
||||
expect(find.text('Blocked User'), findsOneWidget);
|
||||
await tester.pumpWidget(wrap(const BlockedUserWidget(profile: profile), cubit));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('displays unblock button', (WidgetTester tester) async {
|
||||
await mockNetworkImagesFor(() async {
|
||||
final mockCubit = MockBlockedUsersCubit();
|
||||
const profile = ProfileModel(
|
||||
id: 1,
|
||||
name: 'User',
|
||||
email: 'user@example.com',
|
||||
ratingPlace: null,
|
||||
emailVerifiedAt: null,
|
||||
phoneConfirmationState: 'confirmed',
|
||||
createdAt: null,
|
||||
updatedAt: null,
|
||||
isPublicProfile: false,
|
||||
isBlockedByMe: true,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
slug: 'user',
|
||||
profilePhotoUrl: '/photo.jpg',
|
||||
roles: ['user'],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
educationResource: null,
|
||||
diplomasAndCertificates: null,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(createTestWidget(profile, mockCubit));
|
||||
await tester.pump(); // Initial frame
|
||||
await tester.pump(); // Allow image loading to start
|
||||
|
||||
expect(find.text('Убрать'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('calls unblockUser when unblock button is tapped', (WidgetTester tester) async {
|
||||
await mockNetworkImagesFor(() async {
|
||||
final mockCubit = MockBlockedUsersCubit();
|
||||
when(mockCubit.unblockUser(any)).thenAnswer((_) => Future.value());
|
||||
|
||||
const profile = ProfileModel(
|
||||
id: 1,
|
||||
name: 'User',
|
||||
email: 'user@example.com',
|
||||
ratingPlace: null,
|
||||
emailVerifiedAt: null,
|
||||
phoneConfirmationState: 'confirmed',
|
||||
createdAt: null,
|
||||
updatedAt: null,
|
||||
isPublicProfile: false,
|
||||
isBlockedByMe: true,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
slug: 'test-user',
|
||||
profilePhotoUrl: '/photo.jpg',
|
||||
roles: ['user'],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
educationResource: null,
|
||||
diplomasAndCertificates: null,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(createTestWidget(profile, mockCubit));
|
||||
await tester.pump(); // Initial frame
|
||||
await tester.pump(); // Allow image loading to start
|
||||
|
||||
await tester.tap(find.text('Убрать'));
|
||||
await tester.pump(); // Process tap
|
||||
await tester.pump(); // Allow async operations
|
||||
|
||||
verify(mockCubit.unblockUser('test-user')).called(1);
|
||||
});
|
||||
expect(find.byType(BlockedUserWidget), findsOneWidget);
|
||||
expect(find.text('Test User'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user