feat: add todo cache repository and precise invalidation
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:social_app/core/cache/cache_entry.dart';
|
||||
import 'package:social_app/core/cache/cache_invalidator.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/todo/data/todo_api.dart';
|
||||
import 'package:social_app/features/todo/data/todo_repository.dart';
|
||||
|
||||
class _MockTodoApi extends Mock implements TodoApi {}
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'complete todo should optimistically update and invalidate pending list key',
|
||||
() async {
|
||||
final api = _MockTodoApi();
|
||||
final store = HybridCacheStore(
|
||||
memory: MemoryCacheStore(),
|
||||
persistent: PersistentCacheStore(),
|
||||
);
|
||||
final invalidator = CacheInvalidator(store: store);
|
||||
final repository = TodoRepository(
|
||||
api: api,
|
||||
store: store,
|
||||
invalidator: invalidator,
|
||||
);
|
||||
|
||||
final cached = TodoResponse(
|
||||
id: 'todo_1',
|
||||
ownerId: 'u1',
|
||||
title: 't1',
|
||||
priority: 1,
|
||||
order: 0,
|
||||
status: 'pending',
|
||||
createdAt: DateTime(2026, 3, 20, 10),
|
||||
updatedAt: DateTime(2026, 3, 20, 10),
|
||||
);
|
||||
await store.write<CacheEntry<List<TodoResponse>>>(
|
||||
TodoRepository.pendingListKey,
|
||||
CacheEntry(value: [cached], fetchedAt: DateTime(2026, 3, 20, 10, 0)),
|
||||
);
|
||||
|
||||
when(
|
||||
() => api.completeTodo('todo_1'),
|
||||
).thenAnswer((_) async => cached.copyWith(status: 'completed'));
|
||||
|
||||
await repository.completeTodo('todo_1');
|
||||
|
||||
final updated = await store.read<CacheEntry<List<TodoResponse>>>(
|
||||
TodoRepository.pendingListKey,
|
||||
);
|
||||
expect(updated?.value.first.status, 'completed');
|
||||
expect(invalidator.wasInvalidated(TodoRepository.pendingListKey), true);
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user