Files
accounted/lib/reference-data/__tests__/fiscal-scope.test.ts
T
Jakob Wennberg 9a56b7aff9 perf(bookkeeping): fiscal-year pickers and cash accounts read the session cache (#1934)
First consumer migration onto lib/reference-data. FyPicker and
FiscalYearSelector (14 consumer surfaces, 47 fiscal-period fetch sites
before this series) now read useFiscalPeriods(); with the layout seed the
restore of the persisted scope runs in the first effect tick and onReady
fires on mount instead of after a round trip. Their restore rules are
extracted into a pure resolveInitialFiscalScope() (lib/reference-data/
fiscal-scope.ts) so the two pickers cannot drift apart again, and the
restore runs once per company load, not on every background revalidation.

- /reports: the static catalog renders immediately; only the "no fiscal
  year" empty state waits for the picker (previously six skeleton bars
  until /api/bookkeeping/fiscal-periods resolved).
- JournalEntryList (/bookkeeping): resolves its initial scope from the
  cached list instead of its own fetch; the saved-scope shortcut still
  unblocks the entries fetch first when nothing is cached, and resolution
  is guarded to once per company so a revalidation can never snap a
  deep-link "all years" visit back to the stored year.
- /transactions: the account chooser reads useCashAccounts({ enabledOnly })
  (seeded) instead of fetching /api/cash-accounts on every visit; the bank
  sync button invalidates that entry after a sync.
- STORAGE_KEY_PREFIX / ALL_YEARS_VALUE move to a dependency-free
  fiscal-year-storage.ts (re-exported from FiscalYearSelector) so lib/ code
  can import them without a React component.

raw-reference-fetch ratchet: 55 -> 51 files.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 14:24:24 +02:00

90 lines
3.7 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { prepareFiscalPeriods, resolveInitialFiscalScope } from '../fiscal-scope'
import { ALL_YEARS_VALUE } from '@/components/common/fiscal-year-storage'
import type { FiscalPeriod } from '@/types'
const period = (id: string, start: string, end: string) =>
({ id, name: id, period_start: start, period_end: end }) as unknown as FiscalPeriod
const p2024 = period('p2024', '2024-01-01', '2024-12-31')
const p2025 = period('p2025', '2025-01-01', '2025-12-31')
const p2026 = period('p2026', '2026-01-01', '2026-12-31')
const p2027 = period('p2027', '2027-01-01', '2027-12-31')
const TODAY = '2026-08-26'
describe('prepareFiscalPeriods', () => {
it('sorts newest first and can hide periods that have not started', () => {
const all = prepareFiscalPeriods([p2024, p2027, p2026, p2025], false, TODAY)
expect(all.map((p) => p.id)).toEqual(['p2027', 'p2026', 'p2025', 'p2024'])
const started = prepareFiscalPeriods([p2024, p2027, p2026, p2025], true, TODAY)
expect(started.map((p) => p.id)).toEqual(['p2026', 'p2025', 'p2024'])
})
it('does not mutate the input', () => {
const input = [p2024, p2026]
prepareFiscalPeriods(input, false, TODAY)
expect(input.map((p) => p.id)).toEqual(['p2024', 'p2026'])
})
})
describe('resolveInitialFiscalScope', () => {
const periods = prepareFiscalPeriods([p2024, p2025, p2026], false, TODAY)
it('restores a persisted period that still exists', () => {
expect(resolveInitialFiscalScope(periods, 'p2025', { includeAllOption: true })).toEqual({
periodId: 'p2025',
period: p2025,
})
})
it('ignores a stale persisted id and falls back per the surface', () => {
expect(resolveInitialFiscalScope(periods, 'gone', { includeAllOption: true })).toBeNull()
expect(resolveInitialFiscalScope(periods, 'gone', { includeAllOption: false })).toEqual({
periodId: 'p2026',
period: p2026,
})
})
it('honours an explicit "all years" only where the surface allows it', () => {
expect(resolveInitialFiscalScope(periods, ALL_YEARS_VALUE, { includeAllOption: true })).toEqual({
periodId: null,
period: null,
})
expect(resolveInitialFiscalScope(periods, ALL_YEARS_VALUE, { includeAllOption: false })).toEqual({
periodId: 'p2026',
period: p2026,
})
})
it('with nothing stored: all-years surfaces stay unfiltered, others pick the newest', () => {
expect(resolveInitialFiscalScope(periods, null, { includeAllOption: true })).toBeNull()
expect(resolveInitialFiscalScope(periods, null, { includeAllOption: false })).toEqual({
periodId: 'p2026',
period: p2026,
})
})
it('preferLatestEnded opens on the most recently ended period and ignores the stored choice', () => {
expect(
resolveInitialFiscalScope(periods, 'p2024', { includeAllOption: false, preferLatestEnded: true, today: TODAY }),
).toEqual({ periodId: 'p2025', period: p2025 })
})
it('preferLatestEnded falls back to the newest period when none has ended', () => {
const onlyCurrent = prepareFiscalPeriods([p2026], false, TODAY)
expect(
resolveInitialFiscalScope(onlyCurrent, null, { includeAllOption: false, preferLatestEnded: true, today: TODAY }),
).toEqual({ periodId: 'p2026', period: p2026 })
})
it('returns null for an empty list on every path', () => {
expect(resolveInitialFiscalScope([], null, { includeAllOption: false })).toBeNull()
expect(resolveInitialFiscalScope([], ALL_YEARS_VALUE, { includeAllOption: false })).toBeNull()
expect(resolveInitialFiscalScope([], 'x', { includeAllOption: false, preferLatestEnded: true })).toBeNull()
expect(resolveInitialFiscalScope([], ALL_YEARS_VALUE, { includeAllOption: true })).toEqual({
periodId: null,
period: null,
})
})
})