72 lines
2.0 KiB
Dart
72 lines
2.0 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:prediqo_mobile/data/models/share_card_models.dart';
|
|
import 'package:prediqo_mobile/data/repositories/share_card_repository.dart';
|
|
|
|
ShareCardConfig _config({bool hasDeposited = false}) => ShareCardConfig(
|
|
enabled: true,
|
|
allowMainWallet: true,
|
|
allowBonusWallet: true,
|
|
allowTestWallet: true,
|
|
allowWon: true,
|
|
allowLost: true,
|
|
allowRefunded: true,
|
|
walletOpsEnabled: true,
|
|
allowDeposit: true,
|
|
allowWithdrawal: true,
|
|
allowTransfer: true,
|
|
testWalletShareRequiresRealDeposit: true,
|
|
hasDeposited: hasDeposited,
|
|
);
|
|
|
|
void main() {
|
|
test('coalesces parallel getConfig into a single fetch', () async {
|
|
final store = ShareConfigStore();
|
|
var fetches = 0;
|
|
Future<ShareCardConfig> fetch() async {
|
|
fetches++;
|
|
await Future<void>.delayed(const Duration(milliseconds: 20));
|
|
return _config();
|
|
}
|
|
|
|
final results = await Future.wait([
|
|
store.get(fetch),
|
|
store.get(fetch),
|
|
store.get(fetch),
|
|
]);
|
|
|
|
expect(fetches, 1);
|
|
expect(store.fetchStarts, 1);
|
|
expect(results, everyElement(isA<ShareCardConfig>()));
|
|
});
|
|
|
|
test('TTL cache returns the same snapshot without a second fetch', () async {
|
|
final store = ShareConfigStore();
|
|
var fetches = 0;
|
|
Future<ShareCardConfig> fetch() async {
|
|
fetches++;
|
|
return _config();
|
|
}
|
|
|
|
await store.get(fetch);
|
|
await store.get(fetch);
|
|
expect(fetches, 1);
|
|
});
|
|
|
|
test('invalidate drops cache so a mutation can see hasDeposited', () async {
|
|
final store = ShareConfigStore();
|
|
var fetches = 0;
|
|
Future<ShareCardConfig> fetch() async {
|
|
fetches++;
|
|
return _config(hasDeposited: fetches > 1);
|
|
}
|
|
|
|
final first = await store.get(fetch);
|
|
expect(first.hasDeposited, isFalse);
|
|
|
|
store.invalidate();
|
|
final second = await store.get(fetch);
|
|
expect(second.hasDeposited, isTrue);
|
|
expect(fetches, 2);
|
|
});
|
|
}
|