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