Files
accounted/lib/articles/__tests__/currency-scope.test.ts
T
Jakob Wennberg f5697cfc2f feat(articles): filter the article register by currency (#1229)
Non-SEK article prices became first-class in #1166, so a mixed register needs a
way to look at one currency at a time (user request, christian@odinaero.se
2026-07-25).

- ContextPicker chip far right in the toolbar (convention 8), options = the
  currencies actually present in the register, default "Alla valutor". A
  single-currency register does not render it: a control with one meaningful
  position is noise.
- The scope lives in the URL alongside sort/dir, so a filtered register survives
  opening an article and coming back, and can be linked to. A code that is not
  in the register (last EUR article deleted, hand-edited link) falls back to
  "Alla" instead of hiding every row.
- Search and scope combine, and the no-matches copy names the scope when one is
  active: otherwise "no articles match X" reads as a claim about the whole
  register when it is only true inside the current currency.
- Predicates live in lib/articles/currency-scope.ts with unit tests; blank and
  legacy-null currencies fold into SEK the way the price column already
  displays them.

New strings in both messages/sv.json and messages/en.json. npm test 11369
passed, lint 0 errors, no new tsc errors. Chip screenshotted against the design
system via a temporary sandbox route.

Closes #1189

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-27 14:39:13 +02:00

79 lines
2.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
ALL_CURRENCIES,
articleCurrency,
listArticleCurrencies,
matchesCurrencyScope,
resolveCurrencyScope,
} from '@/lib/articles/currency-scope'
describe('articleCurrency', () => {
it('normalizes the code', () => {
expect(articleCurrency({ currency: 'eur' })).toBe('EUR')
})
it('treats a missing or blank currency as SEK', () => {
expect(articleCurrency({})).toBe('SEK')
expect(articleCurrency({ currency: null })).toBe('SEK')
expect(articleCurrency({ currency: '' })).toBe('SEK')
})
})
describe('listArticleCurrencies', () => {
it('lists each present currency once, SEK first then alphabetically', () => {
expect(
listArticleCurrencies([
{ currency: 'USD' },
{ currency: 'EUR' },
{ currency: 'SEK' },
{ currency: 'EUR' },
{ currency: 'NOK' },
]),
).toEqual(['SEK', 'EUR', 'NOK', 'USD'])
})
it('folds legacy blank rows into SEK', () => {
expect(listArticleCurrencies([{ currency: null }, { currency: 'sek' }])).toEqual(['SEK'])
})
it('is empty for an empty register, so the picker can stay hidden', () => {
expect(listArticleCurrencies([])).toEqual([])
})
})
describe('resolveCurrencyScope', () => {
const available = ['SEK', 'EUR']
it('defaults to no scope', () => {
expect(resolveCurrencyScope(null, available)).toBe(ALL_CURRENCIES)
expect(resolveCurrencyScope(undefined, available)).toBe(ALL_CURRENCIES)
})
it('accepts a present currency, case-insensitively', () => {
expect(resolveCurrencyScope('eur', available)).toBe('EUR')
})
// A stale or hand-edited link must not empty the register: the fallback is
// "show everything", which the user can see and recover from.
it('falls back to no scope for a currency that is not in the register', () => {
expect(resolveCurrencyScope('USD', available)).toBe(ALL_CURRENCIES)
expect(resolveCurrencyScope('nonsense', available)).toBe(ALL_CURRENCIES)
expect(resolveCurrencyScope('EUR', [])).toBe(ALL_CURRENCIES)
})
})
describe('matchesCurrencyScope', () => {
it('keeps every article when unscoped', () => {
expect(matchesCurrencyScope({ currency: 'USD' }, ALL_CURRENCIES)).toBe(true)
})
it('keeps only the scoped currency', () => {
expect(matchesCurrencyScope({ currency: 'EUR' }, 'EUR')).toBe(true)
expect(matchesCurrencyScope({ currency: 'USD' }, 'EUR')).toBe(false)
})
it('matches legacy blank rows under SEK', () => {
expect(matchesCurrencyScope({ currency: null }, 'SEK')).toBe(true)
})
})