diff --git a/packages/frontend/core/src/modules/journal/__tests__/journal-store.spec.ts b/packages/frontend/core/src/modules/journal/__tests__/journal-store.spec.ts new file mode 100644 index 0000000000..98f4781610 --- /dev/null +++ b/packages/frontend/core/src/modules/journal/__tests__/journal-store.spec.ts @@ -0,0 +1,79 @@ +import { Framework, LiveData } from '@toeverything/infra'; +import { describe, expect, test } from 'vitest'; + +import type { DocsService } from '../../doc'; +import { JournalStore } from '../store/journal'; + +function createDoc(id: string, journal: string, trash = false) { + return { + id, + properties$: new LiveData({ journal }), + trash$: new LiveData(trash), + }; +} + +function createStore(docs: ReturnType[]) { + const docsService = { + list: { + docs$: new LiveData(docs), + }, + } as unknown as DocsService; + + const framework = new Framework(); + framework.store(JournalStore, () => new JournalStore(docsService)); + return framework.provider().get(JournalStore); +} + +describe('JournalStore', () => { + const date = '2026-08-10'; + + test('docsByJournalDate$ skips trashed journals', () => { + const store = createStore([ + createDoc('trashed', date, true), + createDoc('kept', date), + ]); + + expect(store.docsByJournalDate$(date).value.map(doc => doc.id)).toEqual([ + 'kept', + ]); + }); + + test('docsByJournalDate$ is empty when the only journal is trashed', () => { + const store = createStore([createDoc('trashed', date, true)]); + + expect(store.docsByJournalDate$(date).value).toEqual([]); + }); + + test('getDocsByJournalDate skips trashed journals', () => { + const store = createStore([ + createDoc('trashed', date, true), + createDoc('kept', date), + ]); + + expect(store.getDocsByJournalDate(date).map(doc => doc.id)).toEqual([ + 'kept', + ]); + }); + + test('allJournalDates$ skips trashed journals', () => { + const store = createStore([ + createDoc('trashed', date, true), + createDoc('kept', '2026-08-09'), + ]); + + expect(store.allJournalDates$.value).toEqual(new Set(['2026-08-09'])); + }); + + test('restoring a journal claims its date again', () => { + const doc = createDoc('journal', date, true); + const store = createStore([doc]); + + expect(store.docsByJournalDate$(date).value).toEqual([]); + + doc.trash$.next(false); + + expect(store.docsByJournalDate$(date).value.map(d => d.id)).toEqual([ + 'journal', + ]); + }); +}); diff --git a/packages/frontend/core/src/modules/journal/store/journal.ts b/packages/frontend/core/src/modules/journal/store/journal.ts index a44d9edfd1..2d5dd7a596 100644 --- a/packages/frontend/core/src/modules/journal/store/journal.ts +++ b/packages/frontend/core/src/modules/journal/store/journal.ts @@ -16,6 +16,9 @@ export class JournalStore extends Store { return new Set( get(this.docsService.list.docs$) .filter(doc => { + if (get(doc.trash$)) { + return false; + } const journal = get(doc.properties$.selector(p => p.journal)); return !!journal && isJournalString(journal); }) @@ -53,12 +56,15 @@ export class JournalStore extends Store { getDocsByJournalDate(date: string) { return this.docsService.list.docs$.value.filter( - doc => doc.properties$.value.journal === date + doc => !doc.trash$.value && doc.properties$.value.journal === date ); } docsByJournalDate$(date: string) { return LiveData.computed(get => { return get(this.docsService.list.docs$).filter(doc => { + if (get(doc.trash$)) { + return false; + } const journal = get(doc.properties$.selector(p => p.journal)); return journal === date; });