Files
doormile_milderapp/test/collect_payment_test.dart
2026-08-11 13:16:33 +05:30

211 lines
7.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:miler/views/Dashboard/pickups/collect_payment.dart';
/// The payment screen is the only place in the app where money changes hands,
/// and it is read by two people at once — the rider holding the phone and the
/// customer being asked to pay. So the things worth testing are the things
/// either of them would dispute afterwards:
///
/// • the amount comes from the booking and nowhere else;
/// • the page says which consignment is being paid for;
/// • the QR is on the page, carrying that amount, not in a dialog;
/// • a short payment cannot be recorded without a figure and a reason.
void main() {
Future<void> pump(
WidgetTester tester,
Map<String, dynamic> stop, {
Map<String, dynamic> verification = const {},
String? initialMethod,
double textScale = 1.0,
}) async {
tester.view.physicalSize = const Size(390 * 3, 844 * 3);
tester.view.devicePixelRatio = 3.0;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
ScreenUtilInit(
designSize: const Size(390, 844),
builder: (_, _) => MaterialApp(
builder: (context, child) => MediaQuery(
data: MediaQuery.of(context)
.copyWith(textScaler: TextScaler.linear(textScale)),
child: child!,
),
home: CollectPaymentPage(
pickup: stop,
verificationData: verification,
initialMethod: initialMethod,
),
),
),
);
// Not pumpAndSettle: the slide-to-confirm control animates its hint
// forever, so a settle never comes. Two frames is enough for this page,
// which has no entrance animation of its own.
await tester.pump();
await tester.pump(const Duration(milliseconds: 400));
}
Map<String, dynamic> stopOf({double due = 450, double pickupAmt = 0}) =>
<String, dynamic>{
'orderid': 'DM-99120',
'type': 'pickup',
'pickupqty': '3',
'Quantity': '3',
'pickupcustomer': 'Priya Sharma',
'tenantname': 'Chettinad Textiles',
'pickupaddress': '12/3B, Second Cross Street, Ram Nagar, Coimbatore',
'collectionamt': '$due',
if (pickupAmt > 0) 'pickupamt': '$pickupAmt',
};
testWidgets('the amount is the booking\'s, and it names who owes it',
(tester) async {
await pump(tester, stopOf(due: 450));
expect(find.text('₹450'), findsOneWidget);
expect(find.text('from Priya Sharma'), findsOneWidget);
});
testWidgets('the page says what the money is for', (tester) async {
await pump(
tester,
stopOf(),
verification: {
'weight': '4.5',
'pickup': {'collected': 3},
},
);
// The consignment, the shipper it belongs to, and what was actually taken —
// this is the half of the screen a customer paying at his own door needs
// before he will trust the number above it.
expect(find.text('What this pays for'), findsOneWidget);
expect(find.text('DM-99120'), findsOneWidget);
expect(find.text('Chettinad Textiles'), findsOneWidget);
expect(find.text('3 collected'), findsOneWidget);
expect(find.text('4.5 kg'), findsOneWidget);
});
testWidgets('cash is the default, and it shows no QR', (tester) async {
await pump(tester, stopOf());
expect(find.byType(QrImageView), findsNothing);
expect(
find.textContaining('Slide — took ₹450 by Cash'),
findsOneWidget,
);
});
testWidgets('choosing UPI puts the QR on the page, not behind a dialog',
(tester) async {
await pump(tester, stopOf(due: 450));
expect(find.byType(QrImageView), findsNothing);
await tester.tap(find.text('UPI / QR'));
await tester.pump(const Duration(milliseconds: 300));
// On the page, next to the consignment it is settling — no dialog, and
// nothing hidden behind a further confirm.
expect(find.byType(QrImageView), findsOneWidget);
expect(find.text('₹450 to miler@upi'), findsOneWidget);
});
group('the UPI intent', () {
test('carries the amount, the payee and the consignment', () {
final uri = buildUpiUri(
payeeVpa: 'shop@okhdfcbank',
payeeName: 'Chettinad Textiles',
orderId: 'DM-99120',
amount: 450,
);
// The amount is baked in, so the customer cannot pay the wrong figure by
// mistyping it; the note makes the payment identifiable in his own
// statement weeks later.
expect(uri, contains('pa=shop%40okhdfcbank'));
expect(uri, contains('am=450.00'));
expect(uri, contains('cu=INR'));
expect(uri, contains(Uri.encodeComponent('Miler pickup DM-99120')));
});
test('escapes a payee name that would otherwise break the intent', () {
final uri = buildUpiUri(
payeeVpa: 'shop@upi',
payeeName: 'Ram & Co · Traders',
orderId: '',
amount: 99.5,
);
expect(uri, isNot(contains('&pn=Ram & Co')));
expect(uri, contains('am=99.50'));
expect(uri, contains(Uri.encodeComponent('Miler pickup')));
});
});
testWidgets('the caller can open straight onto UPI', (tester) async {
// The Bookings sheet already asked which method; asking again on arrival
// would be the extra tap this page exists to remove.
await pump(tester, stopOf(), initialMethod: 'upi (₹450)');
expect(find.byType(QrImageView), findsOneWidget);
});
testWidgets('a short payment needs a figure and a reason', (tester) async {
await pump(tester, stopOf(due: 450));
await tester.ensureVisible(find.text('Paid less'));
await tester.pump();
await tester.tap(find.text('Paid less'));
await tester.pump(const Duration(milliseconds: 300));
expect(find.text('Enter how much you actually took'), findsOneWidget);
await tester.enterText(find.byType(TextField).first, '400');
await tester.pump(const Duration(milliseconds: 300));
expect(find.text('Say why the customer paid less'), findsOneWidget);
await tester.enterText(find.byType(TextField).last, 'No change');
await tester.pump(const Duration(milliseconds: 300));
expect(find.text('₹50 still owed on this consignment'), findsOneWidget);
});
testWidgets('a separate pickup charge is listed, never added up',
(tester) async {
// The office sets what is due. The page explains it; it does not do
// arithmetic the office has not sanctioned.
await pump(tester, stopOf(due: 450, pickupAmt: 60));
expect(find.text('Pickup charge'), findsOneWidget);
expect(find.text('₹60'), findsOneWidget);
expect(find.text('Due now'), findsOneWidget);
expect(find.text('₹510'), findsNothing);
});
testWidgets('a prepaid stop is not a payment screen at all', (tester) async {
await pump(tester, <String, dynamic>{
'orderid': 'DM-1',
'pickupcustomer': 'Priya Sharma',
});
expect(find.text('Nothing to collect here'), findsOneWidget);
expect(find.byType(QrImageView), findsNothing);
});
for (final scale in const [1.0, 1.5, 2.0]) {
testWidgets('lays out at ${scale}x text scale', (tester) async {
await pump(tester, stopOf(due: 1250, pickupAmt: 60), textScale: scale);
expect(tester.takeException(), isNull);
await tester.ensureVisible(find.text('UPI / QR'));
await tester.pump();
await tester.tap(find.text('UPI / QR'));
await tester.pump(const Duration(milliseconds: 300));
expect(tester.takeException(), isNull);
});
}
}