import { describe, it, expect } from 'vitest' import { renderRejection } from './rejectionMessages' import { translate } from '@/i18n' import type { PersonalData, Rejection } from '@/types' const t = (key: Parameters[1], params?: Parameters[2]) => translate('sv', key, params) const member = (overrides: Partial = {}): PersonalData => ({ uuid: 'pd-a', workspaceId: 'ws-1', name: 'Alex', salaryType: 'monthly', employmentStatus: 'employed', createdAt: '2026-01-01', ...overrides, }) const rejection = (overrides: Partial = {}): Rejection => ({ messageKey: 'planApply.rejection.insufficientBalance', data: {}, ...overrides, }) describe('renderRejection', () => { it('renders every known message_key to a real (non-raw-key) string', () => { const knownKeys = [ 'planApply.rejection.invalidDayType', 'planApply.rejection.conflictingDuplicate', 'planApply.rejection.collisionsNeedReplace', 'planApply.rejection.dubbeldagWindowExceeded', 'planApply.rejection.dubbeldagQuotaReached', 'planApply.rejection.dubbeldagInvalid', 'planApply.rejection.dubbeldagBatchQuotaExceeded', 'planApply.rejection.outsideEligibleWindow', 'planApply.rejection.insufficientBalance', 'planApply.rejection.insufficientVacationBalance', 'planApply.rejection.deleteKeyConflict', 'planning.rejection.vacationOnFreeDay', 'planning.rejection.parentalLeaveOnFreeDayDisabled', ] for (const messageKey of knownKeys) { const rendered = renderRejection(rejection({ messageKey }), t, []) expect(rendered).not.toBe(messageKey) } }) it('falls back to the raw message_key when the backend sends one this build does not recognise', () => { expect(renderRejection(rejection({ messageKey: 'planApply.rejection.somethingNew' }), t, [])).toBe( 'planApply.rejection.somethingNew', ) }) it('resolves insufficientVacationBalance with a real parent name, never a raw uuid', () => { const rendered = renderRejection( rejection({ messageKey: 'planApply.rejection.insufficientVacationBalance', data: { parent_personal_data_uuid: 'pd-a', year: 2026, needed: 3, remaining: 1 }, }), t, [member({ uuid: 'pd-a', name: 'Alex' })], ) expect(rendered).toContain('Alex') expect(rendered).not.toContain('pd-a') expect(rendered).toContain('3') expect(rendered).toContain('2026') expect(rendered).toContain('1') }) it('falls back to a generic label, never the raw uuid, when no member matches', () => { const rendered = renderRejection( rejection({ messageKey: 'planApply.rejection.insufficientVacationBalance', data: { parent_personal_data_uuid: 'pd-unknown', year: 2026, needed: 3, remaining: 1 }, }), t, [member({ uuid: 'pd-a', name: 'Alex' })], ) expect(rendered).not.toContain('pd-unknown') expect(rendered).toContain('Okänd förälder') }) it('resolves the parent uuid in conflictingDuplicate to a name, not the raw uuid', () => { const rendered = renderRejection( rejection({ messageKey: 'planApply.rejection.conflictingDuplicate', data: { date: '2026-06-15', child_id: 'child-1', parent_personal_data_uuid: 'pd-a' }, }), t, [member({ uuid: 'pd-a', name: 'Alex' })], ) expect(rendered).toContain('Alex') expect(rendered).not.toContain('pd-a') }) it('resolves the parent uuid in deleteKeyConflict to a name, not the raw uuid', () => { const rendered = renderRejection( rejection({ messageKey: 'planApply.rejection.deleteKeyConflict', data: { date: '2026-06-15', child_id: 'child-1', parent_personal_data_uuid: 'pd-a' }, }), t, [member({ uuid: 'pd-a', name: 'Alex' })], ) expect(rendered).toContain('Alex') expect(rendered).not.toContain('pd-a') }) it('interpolates the date into the free-day rejections', () => { expect( renderRejection( rejection({ messageKey: 'planning.rejection.vacationOnFreeDay', data: { date: '2026-06-06' } }), t, [], ), ).toContain('2026-06-06') expect( renderRejection( rejection({ messageKey: 'planning.rejection.parentalLeaveOnFreeDayDisabled', data: { date: '2026-06-06' }, }), t, [], ), ).toContain('2026-06-06') }) it('leaves data with no parent uuid untouched', () => { const rendered = renderRejection( rejection({ messageKey: 'planApply.rejection.insufficientBalance', data: { needed: 2, tier: 'sjukpenning', remaining: 1 }, }), t, [], ) expect(rendered).toContain('2') expect(rendered).toContain('sjukpenning') }) })