games_app/prediqo_mobile/test/transaction_history_section_test.dart
2026-10-25 20:49:23 +02:00

239 lines
7.5 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:prediqo_mobile/core/config/app_config.dart';
import 'package:prediqo_mobile/core/di/injection.dart';
import 'package:prediqo_mobile/core/network/api_client.dart';
import 'package:prediqo_mobile/core/network/connectivity_controller.dart';
import 'package:prediqo_mobile/core/theme/app_theme.dart';
import 'package:prediqo_mobile/data/models/balance_model.dart';
import 'package:prediqo_mobile/data/models/test_wallet_models.dart';
import 'package:prediqo_mobile/data/models/transaction_model.dart';
import 'package:prediqo_mobile/data/models/wallet_type_model.dart';
import 'package:prediqo_mobile/data/repositories/share_card_repository.dart';
import 'package:prediqo_mobile/data/repositories/wallet_repository_impl.dart';
import 'package:prediqo_mobile/domain/repositories/health_repository.dart';
import 'package:prediqo_mobile/domain/repositories/referral_repository.dart';
import 'package:prediqo_mobile/features/wallet/bloc/wallet_bloc.dart';
import 'package:prediqo_mobile/features/wallet/presentation/transaction_history_screen.dart';
import 'package:prediqo_mobile/l10n/locale_controller.dart';
import 'package:prediqo_mobile/shared/widgets/pred_skeleton.dart';
import 'package:prediqo_mobile/shared/widgets/section_error.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'support/e2e_fixtures.dart';
const _usdt = WalletTypeModel(
id: 1,
code: 'USDT_TRC20',
name: 'USDT',
network: 'TRON',
decimals: 6,
);
TransactionModel _tx(String id, {String type = 'deposit'}) => TransactionModel(
id: id,
walletTypeId: 1,
type: type,
amount: '1000000',
fee: '0',
status: 'completed',
createdAt: '2026-09-24T10:00:00.000Z',
);
class _ControllableWallet implements WalletRepository {
Completer<List<TransactionModel>>? hold;
Object? failWith;
var txCalls = 0;
List<TransactionModel> Function(int limit, int offset)? pages;
@override
Future<List<WalletTypeModel>> getWalletTypes() async => const [_usdt];
@override
Future<List<BalanceModel>> getBalances() async => const [];
@override
Future<List<TransactionModel>> getTransactions({
int limit = 50,
int offset = 0,
String? category,
}) async {
txCalls++;
if (failWith != null) throw failWith!;
if (hold != null) return hold!.future;
if (pages != null) return pages!(limit, offset);
return [_tx('t1')];
}
@override
Future<TestWalletStatusModel> getTestWalletStatus() async =>
const TestWalletStatusModel(
enabled: false,
balanceRaw: '0',
walletTypeId: 1,
decimals: 6,
creditAmountRaw: '0',
maxRequests: 0,
requestsUsed: 0,
canRequest: false,
);
@override
Future<TestWalletCreditResult> requestTestCredits() async {
throw UnimplementedError();
}
@override
Future<DepositAddressResult> getDepositAddress(String walletType) async {
throw UnimplementedError();
}
@override
Future<WithdrawResult> withdraw(
String walletType,
String toAddress,
String amount, {
String? totpCode,
}) async {
throw UnimplementedError();
}
@override
Future<TransferResult> transfer(
String toEmail,
String walletType,
String amount, {
String? totpCode,
}) async {
throw UnimplementedError();
}
}
Future<void> _boot(_ControllableWallet wallet) async {
configureE2eTestEnvironment();
if (sl.isRegistered<AppConfig>()) await sl.reset();
sl.registerSingleton<AppConfig>(
const AppConfig(apiBaseUrl: 'http://localhost', isProduction: false, wsEnabled: false),
);
sl.registerSingleton<ConnectivityController>(ConnectivityController());
final locale = LocaleController();
SharedPreferences.setMockInitialValues({'app_locale': 'en'});
await locale.load();
sl.registerSingleton<LocaleController>(locale);
sl.registerLazySingleton<ApiClient>(() => ApiClient(config: sl()));
sl.registerLazySingleton<ShareCardRepository>(() => ShareCardRepository(sl()));
sl.registerLazySingleton<HealthRepository>(() => FakeHealthRepository());
sl.registerLazySingleton<ReferralRepository>(() => FakeReferralRepository());
sl.registerLazySingleton<WalletRepository>(() => wallet);
sl.registerFactory<WalletBloc>(
() => WalletBloc(wallet, sl(), sl(), shareCards: sl()),
);
}
Widget _app() => BlocProvider(
create: (_) => sl<WalletBloc>(),
child: MaterialApp(
theme: AppTheme.dark,
home: const TransactionHistoryScreen(),
),
);
void main() {
tearDown(() async {
if (sl.isRegistered<AppConfig>()) await sl.reset();
});
testWidgets('shows Activity chrome + skeletons while history loads', (tester) async {
final hold = Completer<List<TransactionModel>>();
final wallet = _ControllableWallet()..hold = hold;
await _boot(wallet);
await tester.pumpWidget(_app());
await tester.pump();
expect(find.text('Activity'), findsOneWidget);
expect(find.byType(PredSkeletonCard), findsWidgets);
expect(
find.descendant(
of: find.byType(RefreshIndicator),
matching: find.byType(CircularProgressIndicator),
),
findsNothing,
);
hold.complete([_tx('t1')]);
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.textContaining('Deposit'), findsOneWidget);
expect(find.byType(PredSkeletonCard), findsNothing);
});
testWidgets('SectionError keeps AppBar and retries once', (tester) async {
final wallet = _ControllableWallet()..failWith = Exception('offline');
await _boot(wallet);
await tester.pumpWidget(_app());
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.byType(SectionError), findsOneWidget);
expect(find.text('Activity'), findsOneWidget);
wallet.failWith = null;
wallet.hold = Completer<List<TransactionModel>>();
final before = wallet.txCalls;
final retry = find.descendant(
of: find.byType(SectionError),
matching: find.byType(IconButton),
);
await tester.tap(retry);
await tester.pump();
expect(wallet.txCalls, before + 1);
expect(find.byType(SectionError), findsOneWidget);
wallet.hold!.complete([_tx('ok')]);
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.textContaining('Deposit'), findsOneWidget);
expect(find.byType(SectionError), findsNothing);
});
testWidgets('Load more appends next page without blanking', (tester) async {
final wallet = _ControllableWallet()
..pages = (limit, offset) {
if (offset == 0) {
return List.generate(20, (i) => _tx('p0-$i'));
}
return [_tx('p1-0')];
};
await _boot(wallet);
await tester.pumpWidget(_app());
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(wallet.txCalls, greaterThanOrEqualTo(1));
expect(find.textContaining('Deposit'), findsWidgets);
await tester.scrollUntilVisible(
find.byKey(const Key('tx_history_load_more')),
400,
scrollable: find.byType(Scrollable).first,
);
await tester.pump();
expect(find.byKey(const Key('tx_history_load_more')), findsOneWidget);
await tester.tap(find.byKey(const Key('tx_history_load_more')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.textContaining('Deposit'), findsWidgets);
expect(wallet.txCalls, greaterThanOrEqualTo(2));
});
}