forked from eatvio/eatvio_app
Use chat_id from the consultations payload for direct navigation and keep the chat lookup as a fallback for legacy rows without chat_id. - validate chat id in ChatRepository and make it nullable - guard repeated taps while opening chat - handle nullable chat id in ProfileCubit and EmptyChatScreen related to #1106
88 lines
3.0 KiB
Dart
88 lines
3.0 KiB
Dart
import 'package:eatvio_app/core/enums/base_state.dart';
|
|
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
|
|
import 'package:eatvio_app/features/consultations/models/consultation_model.dart';
|
|
import 'package:eatvio_app/features/consultations/presentation/cubit/consultations_cubit.dart';
|
|
import 'package:eatvio_app/features/consultations/presentation/cubit/consultations_state.dart';
|
|
import 'package:eatvio_app/features/consultations/repositories/consultations_repository.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:mocktail/mocktail.dart';
|
|
|
|
class MockConsultationsRepository extends Mock implements ConsultationsRepository {}
|
|
|
|
class MockChatRepository extends Mock implements ChatRepository {}
|
|
|
|
void main() {
|
|
group('ConsultationsCubit', () {
|
|
late ConsultationsCubit cubit;
|
|
late MockConsultationsRepository mockRepository;
|
|
late MockChatRepository mockChatRepository;
|
|
|
|
setUp(() {
|
|
mockRepository = MockConsultationsRepository();
|
|
mockChatRepository = MockChatRepository();
|
|
cubit = ConsultationsCubit(
|
|
consultationsRepository: mockRepository,
|
|
chatRepository: mockChatRepository,
|
|
);
|
|
});
|
|
|
|
tearDown(() {
|
|
cubit.close();
|
|
});
|
|
|
|
test('начальное состояние - ComplaintsState', () {
|
|
expect(cubit.state, isA<ConsultationsState>());
|
|
});
|
|
|
|
test('loadConsultations - успешная загрузка', () async {
|
|
final mockConsultations = <ConsultationModel>[];
|
|
|
|
when(() => mockRepository.getConsultations())
|
|
.thenAnswer((_) async => mockConsultations);
|
|
|
|
await cubit.loadConsultations();
|
|
|
|
expect(cubit.state.state, BaseState.success);
|
|
expect(cubit.state.consultations, mockConsultations);
|
|
verify(() => mockRepository.getConsultations()).called(1);
|
|
});
|
|
|
|
test('loadConsultations - обработка ошибки', () async {
|
|
when(() => mockRepository.getConsultations())
|
|
.thenThrow(Exception('Error'));
|
|
|
|
await cubit.loadConsultations();
|
|
|
|
expect(cubit.state.state, BaseState.error);
|
|
});
|
|
|
|
test('getChatIdByUserSlug - возвращает id чата', () async {
|
|
when(() => mockChatRepository.getChatByUserSlug('john-doe'))
|
|
.thenAnswer((_) async => 'chat-uuid');
|
|
|
|
final chatId = await cubit.getChatIdByUserSlug('john-doe');
|
|
|
|
expect(chatId, 'chat-uuid');
|
|
verify(() => mockChatRepository.getChatByUserSlug('john-doe')).called(1);
|
|
});
|
|
|
|
test('getChatIdByUserSlug - возвращает null при ошибке', () async {
|
|
when(() => mockChatRepository.getChatByUserSlug(any()))
|
|
.thenThrow(Exception('Error'));
|
|
|
|
final chatId = await cubit.getChatIdByUserSlug('john-doe');
|
|
|
|
expect(chatId, isNull);
|
|
});
|
|
|
|
test('getChatIdByUserSlug - возвращает null когда чата нет', () async {
|
|
when(() => mockChatRepository.getChatByUserSlug(any()))
|
|
.thenAnswer((_) async => null);
|
|
|
|
final chatId = await cubit.getChatIdByUserSlug('john-doe');
|
|
|
|
expect(chatId, isNull);
|
|
});
|
|
});
|
|
}
|