|
1 | 1 | import { isRTL } from './dir'; |
2 | 2 |
|
3 | 3 | describe('rtl: dir', () => { |
4 | | - describe('with host element', () => { |
5 | | - it('should return true', () => { |
6 | | - expect(isRTL({ dir: 'rtl' })).toBe(true); |
| 4 | + /** |
| 5 | + * Renders the given markup and returns the element with `id="target"`. |
| 6 | + */ |
| 7 | + const render = (html: string): Element => { |
| 8 | + document.body.innerHTML = html; |
| 9 | + |
| 10 | + const target = document.body.querySelector('#target'); |
| 11 | + if (target === null) { |
| 12 | + throw new Error('Test markup must contain an element with id="target".'); |
| 13 | + } |
| 14 | + |
| 15 | + return target; |
| 16 | + }; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + /** |
| 20 | + * Reset to the state of a document that never set a direction, rather than |
| 21 | + * to `ltr`, so that tests relying on the default are not masked. |
| 22 | + */ |
| 23 | + document.dir = ''; |
| 24 | + document.body.innerHTML = ''; |
| 25 | + }); |
| 26 | + |
| 27 | + describe('with a host element', () => { |
| 28 | + it('should use the dir on the element itself', () => { |
| 29 | + expect(isRTL(render('<div id="target" dir="rtl"></div>'))).toBe(true); |
| 30 | + expect(isRTL(render('<div id="target" dir="ltr"></div>'))).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should use the nearest ancestor that declares a dir', () => { |
| 34 | + expect(isRTL(render('<div dir="rtl"><div><div id="target"></div></div></div>'))).toBe(true); |
| 35 | + expect(isRTL(render('<div dir="ltr"><div><div id="target"></div></div></div>'))).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should let an inner dir override an outer one', () => { |
| 39 | + expect(isRTL(render('<div dir="rtl"><div dir="ltr" id="target"></div></div>'))).toBe(false); |
| 40 | + expect(isRTL(render('<div dir="ltr"><div dir="rtl" id="target"></div></div>'))).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should ignore casing', () => { |
| 44 | + expect(isRTL(render('<div id="target" dir="RTL"></div>'))).toBe(true); |
| 45 | + expect(isRTL(render('<div dir="RTL"><div id="target" dir="LTR"></div></div>'))).toBe(false); |
7 | 46 | }); |
8 | 47 |
|
9 | | - it('should return false', () => { |
10 | | - expect(isRTL({ dir: 'ltr' })).toBe(false); |
11 | | - expect(isRTL({ dir: '' })).toBe(false); |
| 48 | + it('should skip values that do not declare a direction', () => { |
| 49 | + /** |
| 50 | + * `dir=""`, `dir="auto"` and unknown values are not used as a direction, |
| 51 | + * so the nearest ancestor that does declare one still wins. |
| 52 | + */ |
| 53 | + expect(isRTL(render('<div dir="rtl"><div id="target" dir=""></div></div>'))).toBe(true); |
| 54 | + expect(isRTL(render('<div dir="rtl"><div id="target" dir="auto"></div></div>'))).toBe(true); |
| 55 | + expect(isRTL(render('<div dir="rtl"><div id="target" dir="sideways"></div></div>'))).toBe(true); |
12 | 56 | }); |
13 | 57 | }); |
14 | 58 |
|
15 | | - describe('without host element', () => { |
16 | | - it('should return true', () => { |
17 | | - global.document.dir = 'rtl'; |
18 | | - expect(isRTL()).toBe(true); |
| 59 | + describe('falling back to the document', () => { |
| 60 | + it('should use the document dir when no ancestor declares one', () => { |
| 61 | + document.dir = 'rtl'; |
| 62 | + expect(isRTL(render('<div><div id="target"></div></div>'))).toBe(true); |
| 63 | + |
| 64 | + document.dir = 'ltr'; |
| 65 | + expect(isRTL(render('<div><div id="target"></div></div>'))).toBe(false); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should use the document dir for a detached element', () => { |
| 69 | + document.dir = 'rtl'; |
| 70 | + expect(isRTL(document.createElement('div'))).toBe(true); |
19 | 71 | }); |
20 | 72 |
|
21 | | - it('should return false', () => { |
22 | | - global.document.dir = 'ltr'; |
| 73 | + it('should default to ltr when no dir is set anywhere', () => { |
| 74 | + // Ensure the default is actually being tested rather than a |
| 75 | + // value left behind by another test. |
| 76 | + expect(document.dir).toBe(''); |
| 77 | + |
| 78 | + expect(isRTL()).toBe(false); |
| 79 | + expect(isRTL(null)).toBe(false); |
| 80 | + expect(isRTL(document.createElement('div'))).toBe(false); |
| 81 | + expect(isRTL(render('<div><div id="target"></div></div>'))).toBe(false); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('without a host element', () => { |
| 86 | + it('should use the document dir', () => { |
| 87 | + document.dir = 'rtl'; |
| 88 | + expect(isRTL()).toBe(true); |
| 89 | + |
| 90 | + document.dir = 'ltr'; |
23 | 91 | expect(isRTL()).toBe(false); |
24 | 92 | }); |
25 | 93 | }); |
|
0 commit comments