38 lines
1.5 KiB
TypeScript
38 lines
1.5 KiB
TypeScript
|
|
import { describe, it, expect } from 'vitest';
|
||
|
|
import i18n from '../src/i18n';
|
||
|
|
import { ApiError } from '../src/api/client';
|
||
|
|
import { apiErrorMessage } from '../src/lib/errors';
|
||
|
|
|
||
|
|
const t = (key: string, options?: Record<string, unknown>) => i18n.t(key, options);
|
||
|
|
|
||
|
|
describe('apiErrorMessage (panel admina)', () => {
|
||
|
|
it('maps an error code to the localized label instead of the server message', () => {
|
||
|
|
const err = new ApiError(403, { error: { code: 'FORBIDDEN', message: 'Forbidden' } });
|
||
|
|
expect(apiErrorMessage(err, t, 'awaryjny')).toBe('Nie masz uprawnień do tej operacji.');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('renders the suspension date and reason from details', () => {
|
||
|
|
const err = new ApiError(403, {
|
||
|
|
error: {
|
||
|
|
code: 'ACCOUNT_SUSPENDED',
|
||
|
|
message: 'Account suspended',
|
||
|
|
details: { until: '2026-12-01T10:00:00.000Z', reason: 'Udostepnianie konta' },
|
||
|
|
},
|
||
|
|
});
|
||
|
|
const msg = apiErrorMessage(err, t);
|
||
|
|
expect(msg).toContain('Konto zawieszone do');
|
||
|
|
expect(msg).toContain('Powód: Udostepnianie konta');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('falls back to the server message for an unmapped code, then to the HTTP status', () => {
|
||
|
|
const unmapped = new ApiError(418, { error: { code: 'TEAPOT', message: 'Teapot' } });
|
||
|
|
expect(apiErrorMessage(unmapped, t, 'awaryjny')).toBe('Teapot');
|
||
|
|
const noBody = new ApiError(500, null);
|
||
|
|
expect(apiErrorMessage(noBody, t, 'Cos poszlo nie tak')).toBe('HTTP 500');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('uses the message of a plain Error', () => {
|
||
|
|
expect(apiErrorMessage(new Error('boom'), t, 'awaryjny')).toBe('boom');
|
||
|
|
});
|
||
|
|
});
|