85 lines
2.4 KiB
Dart
85 lines
2.4 KiB
Dart
// Author: fruit.com.cn
|
|
// 验证登出、切换请求及销毁后不会回写旧钱包。
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:fruitshorts/core/data/models/wallet.dart';
|
|
import 'package:fruitshorts/core/data/repositories/commerce_repository.dart';
|
|
import 'package:fruitshorts/core/data/services/wallet_controller.dart';
|
|
|
|
const _oldBalance = 320;
|
|
const _newBalance = 50;
|
|
|
|
void main() {
|
|
test('should_discard_inflight_balance_when_identity_is_reset', () async {
|
|
final repository = _PendingCommerce();
|
|
final wallet = WalletController(repository);
|
|
final pending = wallet.load();
|
|
|
|
wallet.reset();
|
|
repository.requests.single.complete(_snapshot(_oldBalance));
|
|
await pending;
|
|
|
|
expect(wallet.snapshot, isNull);
|
|
expect(wallet.loading, isFalse);
|
|
wallet.dispose();
|
|
});
|
|
|
|
test('should_keep_new_balance_when_an_older_request_finishes_last', () async {
|
|
final repository = _PendingCommerce();
|
|
final wallet = WalletController(repository);
|
|
final oldRequest = wallet.load();
|
|
wallet.reset();
|
|
final newRequest = wallet.load();
|
|
|
|
repository.requests.last.complete(_snapshot(_newBalance));
|
|
await newRequest;
|
|
repository.requests.first.complete(_snapshot(_oldBalance));
|
|
await oldRequest;
|
|
|
|
expect(wallet.balance, _newBalance);
|
|
expect(wallet.loading, isFalse);
|
|
wallet.dispose();
|
|
});
|
|
|
|
test('should_discard_inflight_error_when_wallet_is_reset', () async {
|
|
final repository = _PendingCommerce();
|
|
final wallet = WalletController(repository);
|
|
final pending = wallet.load();
|
|
|
|
wallet.reset();
|
|
repository.requests.single.completeError(StateError('old request'));
|
|
await pending;
|
|
|
|
expect(wallet.error, isNull);
|
|
expect(wallet.snapshot, isNull);
|
|
wallet.dispose();
|
|
});
|
|
}
|
|
|
|
WalletSnapshot _snapshot(int balance) => WalletSnapshot(
|
|
wallet: CoinWallet(
|
|
coinBalance: balance,
|
|
totalRechargedCoin: balance,
|
|
totalConsumedCoin: 0,
|
|
totalRefundedCoin: 0,
|
|
),
|
|
);
|
|
|
|
class _PendingCommerce implements CommerceRepository {
|
|
final requests = <Completer<WalletSnapshot>>[];
|
|
|
|
@override
|
|
Future<WalletSnapshot> fetchWallet() {
|
|
final request = Completer<WalletSnapshot>();
|
|
requests.add(request);
|
|
return request.future;
|
|
}
|
|
|
|
@override
|
|
Future<UnlockResult> unlock(String episodeId) async {
|
|
throw UnimplementedError('not needed by wallet identity tests');
|
|
}
|
|
}
|