|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import { Streamdown } from '../index'; |
| 3 | + |
| 4 | +describe('RTL (Right-to-Left) Support', () => { |
| 5 | + it('renders basic RTL text correctly', () => { |
| 6 | + const rtlContent = 'مرحبا بك في Streamdown'; |
| 7 | + const { container } = render(<Streamdown>{rtlContent}</Streamdown>); |
| 8 | + |
| 9 | + expect(container.textContent).toContain('مرحبا بك في Streamdown'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('renders mixed RTL/LTR content in paragraphs', () => { |
| 13 | + const mixedContent = ` |
| 14 | +This is English text. |
| 15 | +
|
| 16 | +هذا نص عربي مع **تنسيق غامق** و *مائل*. |
| 17 | +
|
| 18 | +Mixed paragraph: Hello مرحبا World عالم. |
| 19 | + `; |
| 20 | + |
| 21 | + const { container } = render(<Streamdown>{mixedContent}</Streamdown>); |
| 22 | + expect(container.textContent).toContain('هذا نص عربي'); |
| 23 | + expect(container.textContent).toContain('Hello مرحبا World عالم'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('renders RTL content in lists', () => { |
| 27 | + const rtlList = ` |
| 28 | +- عنصر القائمة الأول |
| 29 | +- עברית פריט רשימה |
| 30 | +- Third item in English |
| 31 | +- רابع عنصر بالعربية |
| 32 | + `; |
| 33 | + |
| 34 | + const { container } = render(<Streamdown>{rtlList}</Streamdown>); |
| 35 | + const listItems = container.querySelectorAll( |
| 36 | + '[data-streamdown="list-item"]' |
| 37 | + ); |
| 38 | + expect(listItems.length).toBe(4); |
| 39 | + expect(container.textContent).toContain('עברית פריט רשימה'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('renders RTL content in headings', () => { |
| 43 | + const rtlHeadings = ` |
| 44 | +# عنوان رئيسي بالعربية |
| 45 | +
|
| 46 | +## כותרת משנה בעברית |
| 47 | +
|
| 48 | +### Mixed Heading مختلط |
| 49 | + `; |
| 50 | + |
| 51 | + const { container } = render(<Streamdown>{rtlHeadings}</Streamdown>); |
| 52 | + const h1 = container.querySelector('[data-streamdown="heading-1"]'); |
| 53 | + const h2 = container.querySelector('[data-streamdown="heading-2"]'); |
| 54 | + const h3 = container.querySelector('[data-streamdown="heading-3"]'); |
| 55 | + |
| 56 | + expect(h1?.textContent).toBe('عنوان رئيسي بالعربية'); |
| 57 | + expect(h2?.textContent).toBe('כותרת משנה בעברית'); |
| 58 | + expect(h3?.textContent).toBe('Mixed Heading مختلط'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders RTL content in tables', () => { |
| 62 | + const rtlTable = ` |
| 63 | +| English | عربي | עברית | |
| 64 | +|---------|------|-------| |
| 65 | +| Hello | مرحبا | שלום | |
| 66 | +| World | عالم | עולם | |
| 67 | + `; |
| 68 | + |
| 69 | + const { container } = render(<Streamdown>{rtlTable}</Streamdown>); |
| 70 | + const cells = container.querySelectorAll('[data-streamdown="table-cell"]'); |
| 71 | + |
| 72 | + expect(cells[0]?.textContent).toBe('Hello'); |
| 73 | + expect(cells[1]?.textContent).toBe('مرحبا'); |
| 74 | + expect(cells[2]?.textContent).toBe('שלום'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('renders RTL content in blockquotes', () => { |
| 78 | + const rtlQuote = ` |
| 79 | +> هذا اقتباس بالعربية مع **تنسيق**. |
| 80 | +> |
| 81 | +> זה ציטוט בעברית. |
| 82 | + `; |
| 83 | + |
| 84 | + const { container } = render(<Streamdown>{rtlQuote}</Streamdown>); |
| 85 | + const blockquote = container.querySelector( |
| 86 | + '[data-streamdown="blockquote"]' |
| 87 | + ); |
| 88 | + |
| 89 | + expect(blockquote?.textContent).toContain('هذا اقتباس بالعربية'); |
| 90 | + expect(blockquote?.textContent).toContain('זה ציטוט בעברית'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('renders inline code with RTL text', () => { |
| 94 | + const inlineRtl = 'Use `مرحبا` for greeting in Arabic'; |
| 95 | + |
| 96 | + const { container } = render(<Streamdown>{inlineRtl}</Streamdown>); |
| 97 | + const inlineCode = container.querySelector( |
| 98 | + '[data-streamdown="inline-code"]' |
| 99 | + ); |
| 100 | + |
| 101 | + expect(inlineCode?.textContent).toBe('مرحبا'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('renders links with RTL text', () => { |
| 105 | + const rtlLink = '[نص الرابط العربي](https://example.com)'; |
| 106 | + |
| 107 | + const { container } = render(<Streamdown>{rtlLink}</Streamdown>); |
| 108 | + const link = container.querySelector('[data-streamdown="link"]'); |
| 109 | + |
| 110 | + expect(link?.textContent).toBe('نص الرابط العربي'); |
| 111 | + expect(link?.getAttribute('href')).toBe('https://example.com/'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('works with dir="rtl" CSS style', () => { |
| 115 | + const rtlContent = 'هذا نص عربي كامل'; |
| 116 | + |
| 117 | + const { container } = render( |
| 118 | + <div dir="rtl"> |
| 119 | + <Streamdown>{rtlContent}</Streamdown> |
| 120 | + </div> |
| 121 | + ); |
| 122 | + |
| 123 | + const wrapper = container.firstChild as HTMLElement; |
| 124 | + expect(wrapper.getAttribute('dir')).toBe('rtl'); |
| 125 | + expect(container.textContent).toContain('هذا نص عربي كامل'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('preserves bidirectional text ordering', () => { |
| 129 | + const bidiContent = 'The price is 50 ريال for the العربية edition'; |
| 130 | + |
| 131 | + const { container } = render(<Streamdown>{bidiContent}</Streamdown>); |
| 132 | + expect(container.textContent).toBe( |
| 133 | + 'The price is 50 ريال for the العربية edition' |
| 134 | + ); |
| 135 | + }); |
| 136 | +}); |
0 commit comments