forked from eatvio/eatvio_app
feat(promo_codes): added promo codes handling to app
This commit is contained in:
108
test/features/payment/models/promo_response_models_test.dart
Normal file
108
test/features/payment/models/promo_response_models_test.dart
Normal file
@@ -0,0 +1,108 @@
|
||||
import 'package:eatvio_app/features/payment/models/promo_applied_list_response.dart';
|
||||
import 'package:eatvio_app/features/payment/models/promo_apply_response.dart';
|
||||
import 'package:eatvio_app/features/payment/models/promo_code_model.dart';
|
||||
import 'package:eatvio_app/features/payment/models/promo_validate_response.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('PromoValidateResponse', () {
|
||||
test('parses valid preview', () {
|
||||
final response = PromoValidateResponse.fromJson({
|
||||
'valid': true,
|
||||
'promo_code': 'PROMO_100117_A',
|
||||
'discount_percent': 5,
|
||||
'discount_amount_rub': 60.0,
|
||||
'original_amount_rub': 1200.0,
|
||||
'final_amount_rub': 1140.0,
|
||||
});
|
||||
|
||||
expect(response.valid, isTrue);
|
||||
expect(response.promoCode, 'PROMO_100117_A');
|
||||
expect(response.discountPercent, 5);
|
||||
expect(response.finalAmountRub, 1140.0);
|
||||
expect(response.error, isNull);
|
||||
});
|
||||
|
||||
test('parses invalid preview with HTTP 200 body', () {
|
||||
final response = PromoValidateResponse.fromJson({
|
||||
'valid': false,
|
||||
'error': 'Промокод «UNKNOWN» не найден в системе.',
|
||||
});
|
||||
|
||||
expect(response.valid, isFalse);
|
||||
expect(response.error, contains('не найден'));
|
||||
});
|
||||
});
|
||||
|
||||
group('PromoApplyResponse', () {
|
||||
test('parses success with nested promo_code', () {
|
||||
final response = PromoApplyResponse.fromJson({
|
||||
'success': true,
|
||||
'promo_code': {
|
||||
'id': 1,
|
||||
'chosen_plan_id': 42,
|
||||
'promo_code': 'PROMO_100117_A',
|
||||
'discount_percent': 5,
|
||||
'discount_amount_rub': 60.0,
|
||||
'state': 'applied',
|
||||
'applied_at': '2024-01-15T10:30:00+00:00',
|
||||
'confirmed_at': null,
|
||||
'cancelled_at': null,
|
||||
},
|
||||
'discount_percent': 5,
|
||||
'discount_amount_rub': 60.0,
|
||||
'original_amount_rub': 1200.0,
|
||||
'final_amount_rub': 1140.0,
|
||||
});
|
||||
|
||||
expect(response.success, isTrue);
|
||||
expect(response.promoCode, isA<PromoCodeModel>());
|
||||
expect(response.promoCode!.promoCode, 'PROMO_100117_A');
|
||||
expect(response.promoCode!.state, 'applied');
|
||||
expect(response.finalAmountRub, 1140.0);
|
||||
});
|
||||
|
||||
test('parses failure body', () {
|
||||
final response = PromoApplyResponse.fromJson({
|
||||
'success': false,
|
||||
'error': 'Промокод «X» уже был использован вами ранее.',
|
||||
});
|
||||
|
||||
expect(response.success, isFalse);
|
||||
expect(response.error, contains('уже был использован'));
|
||||
expect(response.promoCode, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('PromoAppliedListResponse', () {
|
||||
test('parses promo_codes list', () {
|
||||
final response = PromoAppliedListResponse.fromJson({
|
||||
'promo_codes': [
|
||||
{
|
||||
'id': 2,
|
||||
'chosen_plan_id': 42,
|
||||
'promo_code': 'PROMO_LIST_B',
|
||||
'discount_percent': 3,
|
||||
'discount_amount_rub': 36.0,
|
||||
'state': 'applied',
|
||||
'applied_at': '2024-01-15T10:30:00+00:00',
|
||||
'confirmed_at': null,
|
||||
'cancelled_at': null,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(response.promoCodes, hasLength(1));
|
||||
expect(response.promoCodes.first.promoCode, 'PROMO_LIST_B');
|
||||
expect(response.promoCodes.first.chosenPlanId, 42);
|
||||
});
|
||||
|
||||
test('parses empty list', () {
|
||||
final response = PromoAppliedListResponse.fromJson({
|
||||
'promo_codes': <dynamic>[],
|
||||
});
|
||||
|
||||
expect(response.promoCodes, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user