refactor(tests): update model instantiation to use const for improved performance

This commit is contained in:
mso7nueh
2025-12-03 06:16:34 +03:00
parent d0becb9e11
commit f3d4478c65
15 changed files with 1840 additions and 27 deletions

View File

@@ -0,0 +1,266 @@
import 'package:eatvio_app/features/chat/models/payment_model.dart';
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
import 'package:eatvio_app/features/order_info/cubit/order_info_cubit.dart';
import 'package:eatvio_app/features/plans/models/chosen_plan_model.dart';
import 'package:eatvio_app/features/plans/enums/chosen_plan_state_enum.dart';
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:eatvio_app/features/profile/models/profile_model.dart';
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'order_info_cubit_test.mocks.dart';
@GenerateMocks([ChatRepository, ProfileRepository])
void main() {
group('OrderInfoCubit', () {
late OrderInfoCubit cubit;
late MockChatRepository mockChatRepository;
late MockProfileRepository mockProfileRepository;
setUp(() {
mockChatRepository = MockChatRepository();
mockProfileRepository = MockProfileRepository();
cubit = OrderInfoCubit(
chatRepository: mockChatRepository,
profileRepository: mockProfileRepository,
);
});
tearDown(() {
cubit.close();
});
test('начальное состояние', () {
expect(cubit.state.isLoading, isTrue);
expect(cubit.state.chosenPlan, isNull);
expect(cubit.state.customerProfile, isNull);
});
test('getChosenPlan - успешная загрузка плана', () async {
final chosenPlan = ChosenPlanModel(
id: 1,
optionType: 'options1',
startDate: DateTime.now(),
chosenPlanState: ChosenPlanStateEnum.stateStarted,
chatId: 'chat-1',
consultantId: 1,
plan: const PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: '',
duration: 30,
state: 'active',
type: PlanType(fit: false, slim: false, behavior: false, wellness: false),
details: '',
packetsCount: 1,
author: Author(
authorSlug: 'author-slug',
authorName: 'Author Name',
authorProfileIcon: '',
authorIsMe: false,
),
totalLikes: 0,
totalViews: 0,
totalComments: 0,
isFavorite: false,
minPriceOfOptions: 0,
maxPriceOfOptions: 0,
wasLikedByMe: false,
),
);
when(mockChatRepository.getChosenPlan(id: 1))
.thenAnswer((_) async => chosenPlan);
await cubit.getChosenPlan(id: 1);
expect(cubit.state.isLoading, isFalse);
expect(cubit.state.chosenPlan, equals(chosenPlan));
verify(mockChatRepository.getChosenPlan(id: 1)).called(1);
});
test('getChosenPlan - загрузка плана с профилем клиента', () async {
final chosenPlan = ChosenPlanModel(
id: 1,
optionType: 'options1',
startDate: DateTime.now(),
chosenPlanState: ChosenPlanStateEnum.stateStarted,
chatId: 'chat-1',
consultantId: 1,
plan: const PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: '',
duration: 30,
state: 'active',
type: PlanType(fit: false, slim: false, behavior: false, wellness: false),
details: '',
packetsCount: 1,
author: Author(
authorSlug: 'author-slug',
authorName: 'Author Name',
authorProfileIcon: '',
authorIsMe: false,
),
totalLikes: 0,
totalViews: 0,
totalComments: 0,
isFavorite: false,
minPriceOfOptions: 0,
maxPriceOfOptions: 0,
wasLikedByMe: false,
),
);
const customerProfile = ProfileModel(
id: 1,
name: 'Test User',
email: 'test@example.com',
phoneConfirmationState: 'confirmed',
isPublicProfile: true,
isBlockedByMe: false,
isBlockedByAuthor: false,
isBlockedByUser: false,
isEntitySubscribedByMe: false,
slug: 'test-user',
profilePhotoUrl: '',
roles: [],
badges: [],
subscriptions: [],
subscribers: [],
);
when(mockChatRepository.getChosenPlan(id: 1))
.thenAnswer((_) async => chosenPlan);
when(mockProfileRepository.getProfileBySlug('test-user'))
.thenAnswer((_) async => customerProfile);
await cubit.getChosenPlan(id: 1, customerSlug: 'test-user');
expect(cubit.state.isLoading, isFalse);
expect(cubit.state.chosenPlan, equals(chosenPlan));
expect(cubit.state.customerProfile, equals(customerProfile));
verify(mockChatRepository.getChosenPlan(id: 1)).called(1);
verify(mockProfileRepository.getProfileBySlug('test-user')).called(1);
});
test('getChosenPlan - обработка ошибки', () async {
when(mockChatRepository.getChosenPlan(id: 1))
.thenThrow(Exception('Ошибка загрузки'));
await cubit.getChosenPlan(id: 1);
expect(cubit.state.isLoading, isFalse);
expect(cubit.state.chosenPlan, isNull);
});
test('getPaymentLink - успешное получение ссылки на оплату', () async {
final chosenPlan = ChosenPlanModel(
id: 1,
optionType: 'options1',
startDate: DateTime.now(),
chosenPlanState: ChosenPlanStateEnum.stateStarted,
chatId: 'chat-1',
consultantId: 1,
plan: const PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: '',
duration: 30,
state: 'active',
type: PlanType(fit: false, slim: false, behavior: false, wellness: false),
details: '',
packetsCount: 1,
author: Author(
authorSlug: 'author-slug',
authorName: 'Author Name',
authorProfileIcon: '',
authorIsMe: false,
),
totalLikes: 0,
totalViews: 0,
totalComments: 0,
isFavorite: false,
minPriceOfOptions: 0,
maxPriceOfOptions: 0,
wasLikedByMe: false,
),
);
final paymentModel = PaymentModel(paymentUrl: 'https://payment.url');
// Сначала устанавливаем план в состояние
when(mockChatRepository.getChosenPlan(id: 1))
.thenAnswer((_) async => chosenPlan);
await cubit.getChosenPlan(id: 1);
when(mockChatRepository.getPaymentLink(id: 1))
.thenAnswer((_) async => paymentModel);
final result = await cubit.getPaymentLink();
expect(result, equals('https://payment.url'));
verify(mockChatRepository.getPaymentLink(id: 1)).called(1);
});
test('getPaymentLink - возвращает null, если план не выбран', () async {
final result = await cubit.getPaymentLink();
expect(result, isNull);
verifyNever(mockChatRepository.getPaymentLink(id: anyNamed('id')));
});
test('getPaymentLink - обработка ошибки', () async {
final chosenPlan = ChosenPlanModel(
id: 1,
optionType: 'options1',
startDate: DateTime.now(),
chosenPlanState: ChosenPlanStateEnum.stateStarted,
chatId: 'chat-1',
consultantId: 1,
plan: const PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: '',
duration: 30,
state: 'active',
type: PlanType(fit: false, slim: false, behavior: false, wellness: false),
details: '',
packetsCount: 1,
author: Author(
authorSlug: 'author-slug',
authorName: 'Author Name',
authorProfileIcon: '',
authorIsMe: false,
),
totalLikes: 0,
totalViews: 0,
totalComments: 0,
isFavorite: false,
minPriceOfOptions: 0,
maxPriceOfOptions: 0,
wasLikedByMe: false,
),
);
when(mockChatRepository.getChosenPlan(id: 1))
.thenAnswer((_) async => chosenPlan);
await cubit.getChosenPlan(id: 1);
when(mockChatRepository.getPaymentLink(id: 1))
.thenThrow(Exception('Ошибка'));
final result = await cubit.getPaymentLink();
expect(result, isNull);
});
});
}