refactor: decouple calendar screens from route-driven reload
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:social_app/core/cache/cache_entry.dart';
|
||||
import 'package:social_app/core/cache/cache_policy.dart';
|
||||
import 'package:social_app/core/cache/hybrid_cache_store.dart';
|
||||
import 'package:social_app/core/cache/memory_cache_store.dart';
|
||||
import 'package:social_app/core/cache/persistent_cache_store.dart';
|
||||
import 'package:social_app/features/calendar/data/models/schedule_item_model.dart';
|
||||
import 'package:social_app/features/calendar/data/services/calendar_repository.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'getDayEvents returns cache immediately and refreshes in background',
|
||||
() async {
|
||||
final store = HybridCacheStore(
|
||||
memory: MemoryCacheStore(),
|
||||
persistent: PersistentCacheStore(),
|
||||
);
|
||||
final date = DateTime(2026, 3, 20);
|
||||
final key = CalendarRepository.dayKey(date);
|
||||
await store.persistent.write<CacheEntry<List<ScheduleItemModel>>>(
|
||||
key,
|
||||
CacheEntry(
|
||||
value: [
|
||||
ScheduleItemModel(
|
||||
id: 'evt_cached',
|
||||
ownerId: 'owner_1',
|
||||
title: 'cached',
|
||||
startAt: DateTime(2026, 3, 20, 10),
|
||||
endAt: DateTime(2026, 3, 20, 11),
|
||||
status: ScheduleStatus.active,
|
||||
),
|
||||
],
|
||||
fetchedAt: DateTime(2026, 3, 20, 11, 0),
|
||||
),
|
||||
);
|
||||
|
||||
var remoteCalls = 0;
|
||||
final repository = CalendarRepository(
|
||||
store: store,
|
||||
now: () => DateTime(2026, 3, 20, 11, 5),
|
||||
policy: const CachePolicy(
|
||||
softTtl: Duration(minutes: 2),
|
||||
hardTtl: Duration(minutes: 30),
|
||||
minRefreshInterval: Duration(minutes: 1),
|
||||
),
|
||||
loadDayFromRemote: (_) async {
|
||||
remoteCalls += 1;
|
||||
return const <ScheduleItemModel>[];
|
||||
},
|
||||
loadMonthFromRemote: (_, __) async => const <ScheduleItemModel>[],
|
||||
);
|
||||
|
||||
final result = await repository.getDayEvents(date);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 10));
|
||||
|
||||
expect(result.first.id, 'evt_cached');
|
||||
expect(remoteCalls, 1);
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user