Files
F-r-ldraledigplannerare/frontend/src/utils/wishName.test.ts
T
jacobh2 dc4c0a5edd Show preset wish names in the current UI language
Preset wishes were created with `name: t(preset.labelKey)`, so the
translated string was persisted as the wish's name. A wish created while
the UI was Swedish stayed "Jul" in English forever -- the Swedish text
was the data, so no display-layer change could fix it.

Wish names are user-editable free text, so `name` cannot simply be
translated at render time. Migration 007 adds a nullable
`leave_wish.name_message_key` recording where a name came from: preset
wishes store the i18n key alongside the translated literal, and the UI
renders the key when present.

The key is cleared server-side the moment `name` actually changes,
compared against the stored value rather than trusting the caller, so no
client can forget to clear it and silently revert a user's own name to a
preset label.

An unknown key (a stale row, a renamed key) falls back to the stored
literal via an isTranslationKey guard, so a bad key can never crash the
page or leak a raw key to the user. Rows predating this column keep
displaying their stored name.

_FULL_SCHEMA in db/connection.py is updated in step with the migration.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
2026-09-14 22:32:54 +02:00

33 lines
1.4 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { resolveWishName } from './wishName'
import { translate } from '@/i18n'
const t = (key: Parameters<typeof translate>[1]) => translate('sv', key)
describe('resolveWishName', () => {
it('translates through nameMessageKey when present, ignoring the stored name', () => {
// The core bug fix: the persisted `name` can be stale (saved while the
// UI was a different language) — the key, when present, is the source
// of truth for display.
const wish = { name: 'STALE_ENGLISH_NAME', nameMessageKey: 'wishEditor.seasonJul' }
expect(resolveWishName(wish, t)).toBe('Jul')
})
it('returns the literal name verbatim when nameMessageKey is null', () => {
const wish = { name: 'Resa till mormor', nameMessageKey: null }
expect(resolveWishName(wish, t)).toBe('Resa till mormor')
})
it('falls back to the literal name for a key this build does not recognise', () => {
// A stale key from an older/newer server build must never crash or
// render a raw, untranslated key string to the user.
const wish = { name: 'Sommar', nameMessageKey: 'wishEditor.thisKeyDoesNotExist' }
expect(resolveWishName(wish, t)).toBe('Sommar')
})
it('falls back to the literal name for an empty-string key', () => {
const wish = { name: 'Custom trip', nameMessageKey: '' }
expect(resolveWishName(wish, t)).toBe('Custom trip')
})
})