|
| 1 | +/** |
| 2 | + * Test Suite: buildReferredVerseText — minimal docs |
| 3 | + * |
| 4 | + * Validates rendering of Quran references across LTR (en) and RTL (ar/ur/fa). |
| 5 | + * |
| 6 | + * Covers: |
| 7 | + * - Single ayah, multiple ayat, ranges, surah-only, and mixed (surah + ayat). |
| 8 | + * - Localized digits via `toLocalizedNumber`. |
| 9 | + * - Punctuation: LTR uses ", ", RTL uses "، ". |
| 10 | + * - Order: LTR <chapter>:<verse>, RTL <verse>:<chapter>. |
| 11 | + * - When both surah-only and ayat exist, inserts a single localized "and". |
| 12 | + * - Empty input returns "". |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect, vi, beforeAll } from 'vitest'; |
| 17 | + |
| 18 | +/* eslint-disable max-lines, react-func/max-lines-per-function */ |
| 19 | +import buildReferredVerseText from '../../components/QuranReader/ReflectionView/ReflectionItem/AuthorInfo/buildReferredVerseText'; |
| 20 | + |
| 21 | +import type Reference from '@/types/QuranReflect/Reference'; |
| 22 | +import { isRTLLocale, toLocalizedNumber } from '@/utils/locale'; |
| 23 | +import { makeVerseKey } from '@/utils/verse'; |
| 24 | + |
| 25 | +type CommonDict = Record<'ayah' | 'surah' | 'and', string>; |
| 26 | + |
| 27 | +const en: CommonDict = { ayah: 'Ayah', surah: 'Surah', and: 'and' }; |
| 28 | +const ar: CommonDict = { ayah: 'آية', surah: 'سورة', and: 'و' }; |
| 29 | +const ur: CommonDict = { ayah: 'آیت', surah: 'سورہ', and: 'اور' }; |
| 30 | +const fa: CommonDict = { ayah: 'آیه', surah: 'سوره', and: 'و' }; |
| 31 | + |
| 32 | +const maps: Record<string, CommonDict> = { en, ar, ur, fa }; |
| 33 | + |
| 34 | +vi.mock('next-translate/getT', () => ({ |
| 35 | + default: async (lang: string) => { |
| 36 | + const dict = maps[lang] ?? maps.en; |
| 37 | + return (key: string) => { |
| 38 | + const plainKey = key.includes(':') ? key.split(':')[1] : key; |
| 39 | + return dict[plainKey] ?? key; |
| 40 | + }; |
| 41 | + }, |
| 42 | +})); |
| 43 | + |
| 44 | +const makeRef = (chapterId: number, from: number, to?: number): Reference => ({ |
| 45 | + chapterId, |
| 46 | + from, |
| 47 | + to: typeof to === 'number' ? to : 0, |
| 48 | + id: |
| 49 | + to === undefined |
| 50 | + ? `surah-${chapterId}${from ? `-${from}` : ''}` |
| 51 | + : `surah-${chapterId}-${from}:${to}`, |
| 52 | +}); |
| 53 | + |
| 54 | +const COMMA = { ltr: ', ', rtl: '، ' } as const; |
| 55 | + |
| 56 | +/* eslint-disable max-lines, react-func/max-lines-per-function */ |
| 57 | +// ---- LTR (en) ---- |
| 58 | +describe('buildReferredVerseText — LTR (en)', () => { |
| 59 | + let t: (k: string) => string; |
| 60 | + beforeAll(async () => { |
| 61 | + const { default: getT } = await import('next-translate/getT'); |
| 62 | + t = await getT('en', 'common'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('single ayah', () => { |
| 66 | + const verses = [makeRef(2, 1, 1)]; |
| 67 | + const out = buildReferredVerseText(verses, verses, 'en', t); |
| 68 | + expect(out).toBe(`${t('common:ayah')} ${makeVerseKey('2', '1')}`); |
| 69 | + }); |
| 70 | + |
| 71 | + it('multiple ayat, stable order, LTR comma', () => { |
| 72 | + const all = [makeRef(2, 1, 1), makeRef(1, 1, 1)]; |
| 73 | + const out = buildReferredVerseText(all, all, 'en', t); |
| 74 | + const expected = `${t('common:ayah')} ${makeVerseKey('2', '1')}${COMMA.ltr}${makeVerseKey( |
| 75 | + '1', |
| 76 | + '1', |
| 77 | + )}`; |
| 78 | + expect(out).toBe(expected); |
| 79 | + }); |
| 80 | + |
| 81 | + it('mixed: surah-only + ayat (includes a range)', () => { |
| 82 | + const verseRefs = [makeRef(18, 4, 5), makeRef(2, 0, 0), makeRef(8, 12, 12)]; |
| 83 | + const nonChapters = [makeRef(18, 4, 5), makeRef(8, 12, 12)]; |
| 84 | + const out = buildReferredVerseText(verseRefs, nonChapters, 'en', t); |
| 85 | + const surahs = `${t('common:surah')} ${toLocalizedNumber(2, 'en')}`; |
| 86 | + const verses = `${makeVerseKey('18', '4', '5')}${COMMA.ltr}${makeVerseKey('8', '12')}`; |
| 87 | + expect(out).toBe(`${surahs} ${t('common:and')} ${t('common:ayah')} ${verses}`); |
| 88 | + }); |
| 89 | + |
| 90 | + it('surah-only', () => { |
| 91 | + const out = buildReferredVerseText([makeRef(36, 0, 0)], [], 'en', t); |
| 92 | + expect(out).toBe(`${t('common:surah')} ${toLocalizedNumber(36, 'en')}`); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +// ---- RTL (ar / ur / fa) ---- |
| 97 | +describe.each([ |
| 98 | + { lang: 'ar', comma: COMMA.rtl }, |
| 99 | + { lang: 'ur', comma: COMMA.rtl }, |
| 100 | + { lang: 'fa', comma: COMMA.rtl }, |
| 101 | +] as const)('buildReferredVerseText — RTL (%s)', ({ lang, comma }) => { |
| 102 | + let t: (k: string) => string; |
| 103 | + beforeAll(async () => { |
| 104 | + const { default: getT } = await import('next-translate/getT'); |
| 105 | + t = await getT(lang, 'common'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('flags RTL locale', () => { |
| 109 | + expect(isRTLLocale(lang)).toBe(true); |
| 110 | + }); |
| 111 | + |
| 112 | + it('single ayah uses localized numerals and ":" order (verse:chapter)', () => { |
| 113 | + const r = [makeRef(2, 1, 1)]; |
| 114 | + const out = buildReferredVerseText(r, r, lang, t); |
| 115 | + const expected = `${t('common:ayah')} ${toLocalizedNumber(1, lang)}:${toLocalizedNumber( |
| 116 | + 2, |
| 117 | + lang, |
| 118 | + )}`; |
| 119 | + expect(out).toBe(expected); |
| 120 | + }); |
| 121 | + |
| 122 | + it('multiple ayat join with Arabic comma and single space', () => { |
| 123 | + const all = [makeRef(2, 1, 1), makeRef(1, 1, 1)]; |
| 124 | + const out = buildReferredVerseText(all, all, lang, t); |
| 125 | + const first = `${toLocalizedNumber(1, lang)}:${toLocalizedNumber(2, lang)}`; |
| 126 | + const second = `${toLocalizedNumber(1, lang)}:${toLocalizedNumber(1, lang)}`; |
| 127 | + |
| 128 | + // enforce exact comma character and spacing |
| 129 | + expect(comma).toBe('، '); |
| 130 | + expect(out).toBe(`${t('common:ayah')} ${first}${comma}${second}`); |
| 131 | + }); |
| 132 | + |
| 133 | + it('mixed: surah-only + ayat (+range) with localized digits', () => { |
| 134 | + const verseRefs = [makeRef(18, 4, 5), makeRef(2, 0, 0), makeRef(8, 12, 12)]; |
| 135 | + const nonChapters = [makeRef(18, 4, 5), makeRef(8, 12, 12)]; |
| 136 | + const out = buildReferredVerseText(verseRefs, nonChapters, lang, t); |
| 137 | + |
| 138 | + const surahs = `${t('common:surah')} ${toLocalizedNumber(2, lang)}`; |
| 139 | + const v1 = `${toLocalizedNumber(4, lang)}:${toLocalizedNumber(18, lang)}-${toLocalizedNumber( |
| 140 | + 5, |
| 141 | + lang, |
| 142 | + )}`; |
| 143 | + const v2 = `${toLocalizedNumber(12, lang)}:${toLocalizedNumber(8, lang)}`; |
| 144 | + expect(out).toBe(`${surahs} ${t('common:and')} ${t('common:ayah')} ${v1}${comma}${v2}`); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +// ---- edge cases ---- |
| 149 | +describe('buildReferredVerseText — edge cases', () => { |
| 150 | + it('empty input → empty string (all locales)', async () => { |
| 151 | + const { default: getT } = await import('next-translate/getT'); |
| 152 | + await Promise.all( |
| 153 | + (['en', 'ar', 'ur', 'fa'] as const).map(async (lang) => { |
| 154 | + const t = await getT(lang, 'common'); |
| 155 | + expect(buildReferredVerseText([], [], lang, t)).toBe(''); |
| 156 | + }), |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + it('range where from === to is rendered as single ayah', async () => { |
| 161 | + const { default: getT } = await import('next-translate/getT'); |
| 162 | + const t = await getT('en', 'common'); |
| 163 | + const verses = [makeRef(2, 5, 5)]; |
| 164 | + const out = buildReferredVerseText(verses, verses, 'en', t); |
| 165 | + expect(out).toBe(`${t('common:ayah')} ${makeVerseKey('2', '5')}`); |
| 166 | + }); |
| 167 | + |
| 168 | + it('invalid range (from > to) — documents current behavior (no crash)', async () => { |
| 169 | + const { default: getT } = await import('next-translate/getT'); |
| 170 | + const t = await getT('en', 'common'); |
| 171 | + const verses = [makeRef(2, 10, 5)]; |
| 172 | + const out = buildReferredVerseText(verses, verses, 'en', t); |
| 173 | + expect(typeof out).toBe('string'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('no extra spaces or trailing commas (LTR)', async () => { |
| 177 | + const { default: getT } = await import('next-translate/getT'); |
| 178 | + const t = await getT('en', 'common'); |
| 179 | + const all = [makeRef(2, 1, 1), makeRef(1, 1, 1)]; |
| 180 | + const out = buildReferredVerseText(all, all, 'en', t); |
| 181 | + expect(out).not.toMatch(/\s{2,}/); |
| 182 | + expect(out.endsWith(',')).toBe(false); |
| 183 | + expect(out.endsWith(' ')).toBe(false); |
| 184 | + }); |
| 185 | + |
| 186 | + it('no extra spaces or trailing commas (RTL)', async () => { |
| 187 | + const { default: getT } = await import('next-translate/getT'); |
| 188 | + const t = await getT('ar', 'common'); |
| 189 | + const all = [makeRef(2, 1, 1), makeRef(1, 1, 1)]; |
| 190 | + const out = buildReferredVerseText(all, all, 'ar', t); |
| 191 | + expect(out).not.toMatch(/\s{2,}/); |
| 192 | + expect(out.endsWith('،')).toBe(false); |
| 193 | + expect(out.endsWith(' ')).toBe(false); |
| 194 | + }); |
| 195 | +}); |
0 commit comments