forked from eatvio/eatvio_app
feat(sbp_payments): added cubit layer for sbp payments with tests
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
import 'package:eatvio_app/features/chat/models/payment_model.dart';
|
||||
import 'package:eatvio_app/features/payment/presentation/cubit/consultation_payment_cubit/consultation_payment_cubit.dart';
|
||||
import 'package:eatvio_app/features/payment/presentation/cubit/consultation_payment_cubit/consultation_payment_state.dart';
|
||||
import 'package:eatvio_app/features/payment/repositories/payment_repository.dart';
|
||||
import 'package:eatvio_app/features/payment/repositories/sbp_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:sbp/models/c2bmembers_model.dart';
|
||||
|
||||
import '../../../../fixtures/fixture_reader.dart';
|
||||
import 'consultation_payment_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<PaymentRepository>(),
|
||||
MockSpec<SbpRepository>(),
|
||||
])
|
||||
void main() {
|
||||
group('ConsultationPaymentCubit', () {
|
||||
late ConsultationPaymentCubit cubit;
|
||||
late MockPaymentRepository mockPaymentRepository;
|
||||
late MockSbpRepository mockSbpRepository;
|
||||
|
||||
final installedBank = C2bmemberModel.fromJson(const {
|
||||
'bankName': 'Сбербанк',
|
||||
'logoURL': 'https://qr.nspk.ru/proxyapp/logo/bank100000000111.png',
|
||||
'schema': 'bank100000000111',
|
||||
'package_name': 'ru.sberbankmobile',
|
||||
'icon': 'packages/sbp/assets/icons/sber.png',
|
||||
});
|
||||
|
||||
late PaymentModel alfaPayment;
|
||||
|
||||
setUp(() {
|
||||
mockPaymentRepository = MockPaymentRepository();
|
||||
mockSbpRepository = MockSbpRepository();
|
||||
cubit = ConsultationPaymentCubit(
|
||||
paymentRepository: mockPaymentRepository,
|
||||
sbpRepository: mockSbpRepository,
|
||||
);
|
||||
alfaPayment = PaymentModel.fromJson(
|
||||
FixtureReader.readAsMap('chat/payments/payment_response_alfa_bank.json'),
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
cubit.close();
|
||||
});
|
||||
|
||||
test('начальный state - isLoading=false, banks=[], checkoutUrl=null', () {
|
||||
expect(cubit.state, isA<ConsultationPaymentState>());
|
||||
expect(cubit.state.isLoading, isFalse);
|
||||
expect(cubit.state.banks, isEmpty);
|
||||
expect(cubit.state.checkoutUrl, isNull);
|
||||
expect(cubit.state.orderId, isNull);
|
||||
expect(cubit.state.errorMessage, isNull);
|
||||
});
|
||||
|
||||
test(
|
||||
'startPayment - успешно получает checkout_url и загружает банки',
|
||||
() async {
|
||||
const planId = 123;
|
||||
|
||||
when(
|
||||
mockPaymentRepository.startPaymentProcess(planId: planId),
|
||||
).thenAnswer((_) async => alfaPayment);
|
||||
when(
|
||||
mockSbpRepository.getInstalledBanks(),
|
||||
).thenAnswer((_) async => [installedBank]);
|
||||
|
||||
await cubit.startPayment(planId: planId);
|
||||
|
||||
expect(cubit.state.isLoading, isFalse);
|
||||
expect(cubit.state.checkoutUrl, alfaPayment.paymentUrl);
|
||||
expect(cubit.state.orderId, alfaPayment.orderId);
|
||||
expect(cubit.state.banks, [installedBank]);
|
||||
expect(cubit.state.errorMessage, isNull);
|
||||
verify(
|
||||
mockPaymentRepository.startPaymentProcess(planId: planId),
|
||||
).called(1);
|
||||
verify(mockSbpRepository.getInstalledBanks()).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
test('startPayment - при ошибке API emit errorMessage', () async {
|
||||
const planId = 123;
|
||||
|
||||
when(
|
||||
mockPaymentRepository.startPaymentProcess(planId: planId),
|
||||
).thenThrow(Exception('Payment start failed'));
|
||||
|
||||
await cubit.startPayment(planId: planId);
|
||||
|
||||
expect(cubit.state.isLoading, isFalse);
|
||||
expect(cubit.state.errorMessage, isNotNull);
|
||||
expect(cubit.state.banks, isEmpty);
|
||||
expect(cubit.state.checkoutUrl, isNull);
|
||||
verifyNever(mockSbpRepository.getInstalledBanks());
|
||||
});
|
||||
|
||||
test(
|
||||
'startPayment - при ошибке SBP после успешного payment emit error',
|
||||
() async {
|
||||
const planId = 123;
|
||||
|
||||
when(
|
||||
mockPaymentRepository.startPaymentProcess(planId: planId),
|
||||
).thenAnswer((_) async => alfaPayment);
|
||||
when(
|
||||
mockSbpRepository.getInstalledBanks(),
|
||||
).thenThrow(Exception('SBP plugin error'));
|
||||
|
||||
await cubit.startPayment(planId: planId);
|
||||
|
||||
expect(cubit.state.isLoading, isFalse);
|
||||
expect(cubit.state.errorMessage, isNotNull);
|
||||
expect(cubit.state.checkoutUrl, alfaPayment.paymentUrl);
|
||||
expect(cubit.state.orderId, alfaPayment.orderId);
|
||||
expect(cubit.state.banks, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test('openBank - успешно открывает банк после startPayment', () async {
|
||||
const planId = 123;
|
||||
|
||||
when(
|
||||
mockPaymentRepository.startPaymentProcess(planId: planId),
|
||||
).thenAnswer((_) async => alfaPayment);
|
||||
when(
|
||||
mockSbpRepository.getInstalledBanks(),
|
||||
).thenAnswer((_) async => [installedBank]);
|
||||
when(
|
||||
mockSbpRepository.openBank(
|
||||
url: alfaPayment.paymentUrl,
|
||||
bank: installedBank,
|
||||
),
|
||||
).thenAnswer((_) async => true);
|
||||
|
||||
await cubit.startPayment(planId: planId);
|
||||
final result = await cubit.openBank(bank: installedBank);
|
||||
|
||||
expect(result, isTrue);
|
||||
verify(
|
||||
mockSbpRepository.openBank(
|
||||
url: alfaPayment.paymentUrl,
|
||||
bank: installedBank,
|
||||
),
|
||||
).called(1);
|
||||
});
|
||||
|
||||
test(
|
||||
'openBank - без checkoutUrl возвращает false и не вызывает SbpRepository',
|
||||
() async {
|
||||
final result = await cubit.openBank(bank: installedBank);
|
||||
|
||||
expect(result, isFalse);
|
||||
verifyNever(
|
||||
mockSbpRepository.openBank(
|
||||
url: anyNamed('url'),
|
||||
bank: anyNamed('bank'),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('openBank - возвращает false когда банк не открылся', () async {
|
||||
const planId = 123;
|
||||
|
||||
when(
|
||||
mockPaymentRepository.startPaymentProcess(planId: planId),
|
||||
).thenAnswer((_) async => alfaPayment);
|
||||
when(
|
||||
mockSbpRepository.getInstalledBanks(),
|
||||
).thenAnswer((_) async => [installedBank]);
|
||||
when(
|
||||
mockSbpRepository.openBank(
|
||||
url: alfaPayment.paymentUrl,
|
||||
bank: installedBank,
|
||||
),
|
||||
).thenAnswer((_) async => false);
|
||||
|
||||
await cubit.startPayment(planId: planId);
|
||||
final result = await cubit.openBank(bank: installedBank);
|
||||
|
||||
expect(result, isFalse);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user