|
| 1 | +import {async, fakeAsync, TestBed, tick} from '@angular/core/testing'; |
| 2 | +import {Component, getDebugNode} from '@angular/core'; |
| 3 | +import {By} from '@angular/platform-browser'; |
| 4 | +import {Directionality, BidiModule} from './index'; |
| 5 | + |
| 6 | +describe('Directionality', () => { |
| 7 | + let documentElementDir, bodyDir; |
| 8 | + |
| 9 | + beforeAll(() => { |
| 10 | + documentElementDir = document.documentElement.dir; |
| 11 | + bodyDir = document.body.dir; |
| 12 | + }); |
| 13 | + |
| 14 | + afterAll(() => { |
| 15 | + document.documentElement.dir = documentElementDir; |
| 16 | + document.body.dir = bodyDir; |
| 17 | + }); |
| 18 | + |
| 19 | + beforeEach(async(() => { |
| 20 | + TestBed.configureTestingModule({ |
| 21 | + imports: [BidiModule], |
| 22 | + declarations: [ElementWithDir, InjectsDirectionality] |
| 23 | + }).compileComponents(); |
| 24 | + |
| 25 | + clearDocumentDirAttributes(); |
| 26 | + })); |
| 27 | + |
| 28 | + describe('Service', () => { |
| 29 | + it('should read dir from the html element if not specified on the body', () => { |
| 30 | + document.documentElement.dir = 'rtl'; |
| 31 | + |
| 32 | + let fixture = TestBed.createComponent(InjectsDirectionality); |
| 33 | + let testComponent = fixture.debugElement.componentInstance; |
| 34 | + |
| 35 | + expect(testComponent.dir.value).toBe('rtl'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should read dir from the body even it is also specified on the html element', () => { |
| 39 | + document.documentElement.dir = 'ltr'; |
| 40 | + document.body.dir = 'rtl'; |
| 41 | + |
| 42 | + let fixture = TestBed.createComponent(InjectsDirectionality); |
| 43 | + let testComponent = fixture.debugElement.componentInstance; |
| 44 | + |
| 45 | + expect(testComponent.dir.value).toBe('rtl'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should default to ltr if nothing is specified on either body or the html element', () => { |
| 49 | + let fixture = TestBed.createComponent(InjectsDirectionality); |
| 50 | + let testComponent = fixture.debugElement.componentInstance; |
| 51 | + |
| 52 | + expect(testComponent.dir.value).toBe('ltr'); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('Dir directive', () => { |
| 57 | + it('should provide itself as Directionality', () => { |
| 58 | + let fixture = TestBed.createComponent(ElementWithDir); |
| 59 | + const injectedDirectionality = |
| 60 | + fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir; |
| 61 | + |
| 62 | + fixture.detectChanges(); |
| 63 | + |
| 64 | + expect(injectedDirectionality.value).toBe('rtl'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should emit a change event when the value changes', fakeAsync(() => { |
| 68 | + let fixture = TestBed.createComponent(ElementWithDir); |
| 69 | + const injectedDirectionality = |
| 70 | + fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir; |
| 71 | + |
| 72 | + fixture.detectChanges(); |
| 73 | + |
| 74 | + expect(injectedDirectionality.value).toBe('rtl'); |
| 75 | + expect(fixture.componentInstance.changeCount).toBe(0); |
| 76 | + |
| 77 | + fixture.componentInstance.direction = 'ltr'; |
| 78 | + |
| 79 | + fixture.detectChanges(); |
| 80 | + tick(); |
| 81 | + |
| 82 | + expect(injectedDirectionality.value).toBe('ltr'); |
| 83 | + expect(fixture.componentInstance.changeCount).toBe(1); |
| 84 | + })); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | + |
| 89 | +function clearDocumentDirAttributes() { |
| 90 | + document.documentElement.dir = ''; |
| 91 | + document.body.dir = ''; |
| 92 | +} |
| 93 | + |
| 94 | +@Component({ |
| 95 | + template: ` |
| 96 | + <div [dir]="direction" (dirChange)="changeCount= changeCount + 1"> |
| 97 | + <injects-directionality></injects-directionality> |
| 98 | + </div> |
| 99 | + ` |
| 100 | +}) |
| 101 | +class ElementWithDir { |
| 102 | + direction = 'rtl'; |
| 103 | + changeCount = 0; |
| 104 | +} |
| 105 | + |
| 106 | +/** Test component with Dir directive. */ |
| 107 | +@Component({ |
| 108 | + selector: 'injects-directionality', |
| 109 | + template: `<div></div>` |
| 110 | +}) |
| 111 | +class InjectsDirectionality { |
| 112 | + constructor(public dir: Directionality) { |
| 113 | + } |
| 114 | +} |
0 commit comments