Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
50 lines
2.7 KiB
TypeScript
50 lines
2.7 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import { containsPersianText, ensurePersianApiErrorMessage, localizeApiErrorPayload } from '@/services/apiErrorLocalization'
|
|
|
|
describe('API error localization', () => {
|
|
it('keeps an existing Persian API message', () => {
|
|
expect(ensurePersianApiErrorMessage('شماره موبایل معتبر نیست', 400)).toBe('شماره موبایل معتبر نیست')
|
|
})
|
|
|
|
it('maps known API codes before using the status fallback', () => {
|
|
expect(ensurePersianApiErrorMessage('event is full', 409, 'EVENT_FULL')).toBe('ظرفیت این رویداد تکمیل شده است.')
|
|
expect(ensurePersianApiErrorMessage('This account has been suspended', 403, 'ACCOUNT_SUSPENDED')).toBe(
|
|
'حساب کاربری شما معلق شده است و امکان انجام این عملیات را ندارید. برای پیگیری با پشتیبانی تماس بگیرید.'
|
|
)
|
|
expect(ensurePersianApiErrorMessage('The review window for this event has expired', 403, 'REVIEW_WINDOW_EXPIRED')).toBe(
|
|
'مهلت ثبت نظر تمام شده'
|
|
)
|
|
expect(ensurePersianApiErrorMessage('The host reply window for this event has expired', 403, 'HOST_REPLY_WINDOW_EXPIRED')).toBe(
|
|
'مهلت پاسخ میزبان به نظر تمام شده است.'
|
|
)
|
|
expect(ensurePersianApiErrorMessage('Identity verification is required to host events', 403, 'IDENTITY_REQUIRED')).toBe(
|
|
'برای میزبانی رویداد باید هویت شما تأیید شده باشد.'
|
|
)
|
|
expect(ensurePersianApiErrorMessage('Chat is closed', 409, 'CHAT_CLOSED')).toBe('این چت بسته شده و امکان ارسال پیام جدید وجود ندارد.')
|
|
})
|
|
|
|
it('localizes top-level and nested validation messages', () => {
|
|
const payload = localizeApiErrorPayload(
|
|
{
|
|
success: false,
|
|
code: 'VALIDATION_ERROR',
|
|
message: ['mobile must be a string', 'نام خانوادگی الزامی است'],
|
|
body: { message: 'validation failed' },
|
|
},
|
|
422
|
|
)
|
|
|
|
expect(payload.message).toEqual(['اطلاعات واردشده معتبر نیست.', 'نام خانوادگی الزامی است'])
|
|
expect((payload.body as { message: string }).message).toBe('اطلاعات واردشده معتبر نیست.')
|
|
expect((payload.message as string[]).every(containsPersianText)).toBe(true)
|
|
})
|
|
|
|
it('creates a Persian envelope for plain-text error responses', () => {
|
|
expect(localizeApiErrorPayload('Bad Gateway', 502)).toMatchObject({
|
|
success: false,
|
|
message: 'سرور موقتاً در دسترس نیست. لطفاً دوباره تلاش کنید.',
|
|
})
|
|
})
|
|
})
|