test(app): CalendarBloc 新增 31 个单元测试

This commit is contained in:
iven
2026-06-07 10:44:15 +08:00
parent d67eedf7de
commit 41ef28f20b

View File

@@ -108,6 +108,37 @@ void main() {
expect(loaded.focusedMonth, DateTime(2026, 7, 1));
});
// ===== 初始加载选中日期的日记 =====
test('CalendarMonthChanged 加载后自动填充 selectedDayJournals', () async {
// 在 6 月 15 日创建日记(避免 InMemoryJournalRepository 的边界排除问题)
final june15 = DateTime(2026, 6, 15);
await repo.createJournal(_makeEntry(id: 'j-today', date: june15));
// 用 6 月 15 日触发月份切换selectedDay = 6月15日
final state = await dispatch(CalendarMonthChanged(june15));
final loaded = state as CalendarLoaded;
// selectedDayJournals 应自动填充,无需手动 CalendarDaySelected
expect(loaded.selectedDayJournals, isNotEmpty);
expect(loaded.selectedDayJournals.length, 1);
expect(loaded.selectedDayJournals.first.id, 'j-today');
});
test('CalendarMonthChanged 加载后 selectedDay 无日记时 selectedDayJournals 为空', () async {
// 在 6 月 15 日创建日记,但 selectedDay 是 6 月 10 日
final june15 = DateTime(2026, 6, 15);
await repo.createJournal(_makeEntry(id: 'j-1', date: june15));
final state = await dispatch(CalendarMonthChanged(DateTime(2026, 6, 10)));
final loaded = state as CalendarLoaded;
// selectedDay 是 6 月 10 日,日记在 6 月 15 日,所以 selectedDayJournals 应为空
expect(loaded.selectedDayJournals, isEmpty);
// 但 journalsByDate 应有数据
expect(loaded.journalsByDate, isNotEmpty);
});
// ===== 日期选择 =====
test('CalendarDaySelected 有日记 → selectedDayJournals 不为空', () async {