| 1 | import path from 'node:path'; |
| 2 | import type { ShortcodePreset, ShortcodesDataset } from 'emojibase'; |
| 3 | import SHORTCODE_PATTERN from 'emojibase-regex/shortcode'; |
| 4 | import SHORTCODE_NATIVE_PATTERN from 'emojibase-regex/shortcode-native'; |
| 5 | |
| 6 | const locale = process.env.TEST_LOCALE ?? 'en'; |
| 7 | const localeCache: Record<string, ShortcodesDataset> = {}; |
| 8 | |
| 9 | function loadShortcodesData(preset: ShortcodePreset): ShortcodesDataset { |
| 10 | const key = `${locale}:${preset}`; |
| 11 | |
| 12 | if (localeCache[key]) { |
| 13 | return localeCache[key]; |
| 14 | } |
| 15 | |
| 16 | try { |
| 17 | localeCache[key] = require(path.join(__dirname, '..', locale, `shortcodes/${preset}.raw.json`)); |
| 18 | } catch { |
| 19 | return {}; |
| 20 | } |
| 21 | |
| 22 | return localeCache[key]; |
| 23 | } |
| 24 | |
| 25 | describe('shortcodes', () => { |
| 26 | const presets: ShortcodePreset[] = [ |
| 27 | 'cldr', |
| 28 | 'cldr-native', |
| 29 | 'emojibase', |
| 30 | 'emojibase-native', |
| 31 | 'emojibase-legacy', |
| 32 | 'github', |
| 33 | 'iamcal', |
| 34 | 'joypixels', |
| 35 | ]; |
| 36 | |
| 37 | presets.forEach((preset) => { |
| 38 | const pattern = |
| 39 | preset === 'cldr-native' || preset === 'emojibase-native' |
| 40 | ? SHORTCODE_NATIVE_PATTERN |
| 41 | : SHORTCODE_PATTERN; |
| 42 | |
| 43 | describe(`${preset}`, () => { |
| 44 | Object.entries(loadShortcodesData(preset)).forEach(([, shortcodes]) => { |
| 45 | (Array.isArray(shortcodes) ? shortcodes : [shortcodes]).forEach((code) => { |
| 46 | const shortcode = `:${code}:`; // Does not include colons by default |
| 47 | |
| 48 | it(`matches shortcode by itself for ${shortcode}`, () => { |
| 49 | expect(shortcode).toMatch(pattern); |
| 50 | }); |
| 51 | |
| 52 | it(`matches shortcode in the middle of a string for ${shortcode}`, () => { |
| 53 | expect(`In the middle ${shortcode} of a string.`).toMatch(pattern); |
| 54 | }); |
| 55 | |
| 56 | it(`matches multiple shortcode for ${shortcode}`, () => { |
| 57 | const globalPattern = new RegExp(pattern.source, 'g'); |
| 58 | const matches = `One ${shortcode} Two ${shortcode} Three ${shortcode}.`.match( |
| 59 | globalPattern, |
| 60 | ); |
| 61 | expect(matches).toHaveLength(3); |
| 62 | }); |
| 63 | }); |
| 64 | }); |
| 65 | }); |
| 66 | }); |
| 67 | }); |