66 lines
1.7 KiB
Dart
66 lines
1.7 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:prediqo_mobile/data/models/advertisement_models.dart';
|
|
import 'package:prediqo_mobile/data/repositories/advertisement_repository_impl.dart';
|
|
|
|
AdvertisementModel _ad(String id) => AdvertisementModel(
|
|
id: id,
|
|
placement: 'home_hero',
|
|
title: 'Welcome bonus',
|
|
accentColor: '#10B981',
|
|
sortOrder: 0,
|
|
);
|
|
|
|
void main() {
|
|
test('coalesces parallel list() into a single fetch', () async {
|
|
final store = AdvertisementStore();
|
|
var fetches = 0;
|
|
Future<List<AdvertisementModel>> fetch() async {
|
|
fetches++;
|
|
await Future<void>.delayed(const Duration(milliseconds: 20));
|
|
return [_ad('a1')];
|
|
}
|
|
|
|
final results = await Future.wait([
|
|
store.get(fetch),
|
|
store.get(fetch),
|
|
store.get(fetch),
|
|
]);
|
|
|
|
expect(fetches, 1);
|
|
expect(store.fetchStarts, 1);
|
|
expect(results, everyElement(hasLength(1)));
|
|
});
|
|
|
|
test('returns the fresh snapshot without refetching', () async {
|
|
final store = AdvertisementStore();
|
|
var fetches = 0;
|
|
Future<List<AdvertisementModel>> fetch() async {
|
|
fetches++;
|
|
return [_ad('a1')];
|
|
}
|
|
|
|
await store.get(fetch);
|
|
final again = await store.get(fetch);
|
|
|
|
expect(fetches, 1);
|
|
expect(again.single.id, 'a1');
|
|
expect(store.peekFresh(), isNotNull);
|
|
expect(store.peekLast(), isNotNull);
|
|
});
|
|
|
|
test('force bypasses the fresh cache', () async {
|
|
final store = AdvertisementStore();
|
|
var fetches = 0;
|
|
Future<List<AdvertisementModel>> fetch() async {
|
|
fetches++;
|
|
return [_ad('a$fetches')];
|
|
}
|
|
|
|
await store.get(fetch);
|
|
final forced = await store.get(fetch, force: true);
|
|
|
|
expect(fetches, 2);
|
|
expect(forced.single.id, 'a2');
|
|
});
|
|
}
|