Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

textDirection.test.ts

107 lines4.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { hasRightToLeftLetter, markdownProseDirection, textDirection } from "./textDirection";
4​
5describe("textDirection", () => {
6 it.each([
7 ["Arabic", "اكتب دالة تجمع الأرقام", "rtl"],
8 ["Hebrew", "תקן את הבאג", "rtl"],
9 ["English", "fix the failing test", "ltr"],
10 [
11 "Arabic that opens with an English term",
12 "StrictMode: يعيد الـ render مرتين في التطوير",
13 "rtl",
14 ],
15 ["English quoting one Arabic word", "translate the label مرحبا for the login screen", "ltr"],
16 [
17 "tied sentences use the whole-text word majority",
18 "Done. تم إصلاح المشكلة في الملف الرئيسي",
19 "rtl",
20 ],
21 ["a tie, by its first word", "React مكتبة", "ltr"],
22 ["a tie that opens in Arabic", "مكتبة React", "rtl"],
23 ["Arabic around numbers and punctuation", "عدّل السطر 42، ثم شغّل الاختبارات (3)", "rtl"],
24 [
25 "mostly Arabic sentences beside one long English one",
26 "هذا مثال على paragraph يحتوي على نص عربي و English words معًا. Arabic text should flow right-to-left while English words like JavaScript, React, and API should remain left-to-right within the same line. الأرقام 1234567890 ورموز مثل @username و #hashtag و 100$ تظهر في الاتجاه الصحيح. هذه جملة لاختبار RTL rendering في المتصفح أو المحرر.",
27 "rtl",
28 ],
29 [
30 "mostly English sentences beside one Arabic one",
31 "Run the migration first. ثم شغّل الاختبارات كلها. Then deploy to staging.",
32 "ltr",
33 ],
34 [
35 "Arabic with inline abbreviations",
36 "هذه جملة عربية تتحدث عن e.g. React i.e. TypeScript JavaScript",
37 "rtl",
38 ],
39 [
40 "an abbreviation at a sentence boundary",
41 "This long English sentence describes the technology sector in the U.S. هذه جملة عربية. وهذه جملة عربية أخرى.",
42 "rtl",
43 ],
44 ["Arabic after an unspaced em dash", "English—مرحبا بالعالم", "rtl"],
45 ["a sentence tie after an unspaced em dash", "Done. Hi—זה באמת עובד", "rtl"],
46 ["Hebrew words split by a zero-width space", "A שלום\u200Bעולם", "rtl"],
47 [
48 "sentences split by zero-width spaces",
49 "one\u200Btwo\u200Bthree\u200Bfour\u200Bfive.\u200Bשלום עולם.\u200Bשלום עולם",
50 "rtl",
51 ],
52 ["sentences split by fullwidth question marks", "שלום! This is English? مرحبا", "rtl"],
53 ["sentences split by fullwidth exclamation marks", "שלום! This is English! مرحبا", "rtl"],
54 ["sentences split by fullwidth full stops", "שלום! This is English. مرحبا", "rtl"],
55 ["an abbreviation with fullwidth dots", "שלום! Try an Arabic greeting, i.e. مرحبا", "ltr"],
56 ])("reads %s", (_, text, direction) => {
57 expect(textDirection(text)).toBe(direction);
58 });
59​
60 it("leaves text without letters to its surroundings", () => {
61 expect(textDirection("42 — 7 ✓")).toBeUndefined();
62 });
63});
64​
65describe("hasRightToLeftLetter", () => {
66 it("does not count a byte order mark as a right-to-left letter", () => {
67 expect(hasRightToLeftLetter("\uFEFFHello")).toBe(false);
68 });
69});
70​
71describe("markdownProseDirection", () => {
72 it.each([
73 [
74 "Hebrew around a code block",
75 "תתקן את זה:\n```ts\nexport function useCheckout(props: CheckoutProps) {\n return props.items.length;\n}\n```\nותריץ שוב",
76 "rtl",
77 ],
78 ["Hebrew around inline code", "לערוך את `apps/web/src/chat.tsx` או להשאיר `useMemo`?", "rtl"],
79 ["Hebrew around double-backtick code", "תבדוק ``const a = `b c d e` + f`` עכשיו", "rtl"],
80 [
81 "Hebrew after a four-backtick fence",
82 "````md\none two three four five six\n````\nשלום עולם, זה עובד",
83 "rtl",
84 ],
85 [
86 "Hebrew after a tilde fence",
87 "~~~\nnpm run build --filter web --force\n~~~\nזה נכשל אצלי",
88 "rtl",
89 ],
90 [
91 "Hebrew before an unclosed fence",
92 "שלום, הנה השגיאה:\n```\nError: cannot find module react dom client",
93 "rtl",
94 ],
95 ["Hebrew around a URL", "ראה https://github.com/pingdotgg/t3code/pull/24050 לפני", "rtl"],
96 ["Hebrew after an uppercase URL scheme", "HTTP://example.org שלום", "rtl"],
97 ["Hebrew immediately after an angle autolink", "<https://example.org>שלום", "rtl"],
98 ["English around inline code", "Should I edit `useMemo` or keep it?", "ltr"],
99 ["English quoting one Hebrew word", "Please check the שלום label before the release", "ltr"],
100 ])("reads %s", (_, markdown, direction) => {
101 expect(markdownProseDirection(markdown)).toBe(direction);
102 });
103​
104 it("leaves markdown without prose letters to its surroundings", () => {
105 expect(markdownProseDirection("```\nconst a = 1;\n```")).toBeUndefined();
106 });
107});