|
| 1 | +import {async, ComponentFixture, TestBed, flushMicrotasks, fakeAsync} from '@angular/core/testing'; |
| 2 | +import {Component, ViewChild, TemplateRef, ViewContainerRef} from '@angular/core'; |
| 3 | +import {LayoutDirection, Dir} from '../core/rtl/dir'; |
| 4 | +import {TemplatePortal} from '../core/portal/portal'; |
| 5 | +import {MdTabBody} from './tab-body'; |
| 6 | +import {MdRippleModule} from '../core/ripple/ripple'; |
| 7 | +import {CommonModule} from '@angular/common'; |
| 8 | +import {PortalModule} from '../core'; |
| 9 | + |
| 10 | + |
| 11 | +describe('MdTabBody', () => { |
| 12 | + let dir: LayoutDirection = 'ltr'; |
| 13 | + |
| 14 | + beforeEach(async(() => { |
| 15 | + dir = 'ltr'; |
| 16 | + TestBed.configureTestingModule({ |
| 17 | + imports: [CommonModule, PortalModule, MdRippleModule], |
| 18 | + declarations: [ |
| 19 | + MdTabBody, |
| 20 | + SimpleTabBodyApp, |
| 21 | + ], |
| 22 | + providers: [ |
| 23 | + { provide: Dir, useFactory: () => { return {value: dir}; } |
| 24 | + }] |
| 25 | + }); |
| 26 | + |
| 27 | + TestBed.compileComponents(); |
| 28 | + })); |
| 29 | + |
| 30 | + describe('when initialized as center', () => { |
| 31 | + let fixture: ComponentFixture<SimpleTabBodyApp>; |
| 32 | + |
| 33 | + describe('in LTR direction', () => { |
| 34 | + beforeEach(() => { |
| 35 | + dir = 'ltr'; |
| 36 | + fixture = TestBed.createComponent(SimpleTabBodyApp); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should be center position without origin', () => { |
| 40 | + fixture.componentInstance.position = 0; |
| 41 | + fixture.detectChanges(); |
| 42 | + |
| 43 | + expect(fixture.componentInstance.mdTabBody._position).toBe('center'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should be left-origin-center position with negative or zero origin', () => { |
| 47 | + fixture.componentInstance.position = 0; |
| 48 | + fixture.componentInstance.origin = 0; |
| 49 | + fixture.detectChanges(); |
| 50 | + |
| 51 | + expect(fixture.componentInstance.mdTabBody._position).toBe('left-origin-center'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should be right-origin-center position with positive nonzero origin', () => { |
| 55 | + fixture.componentInstance.position = 0; |
| 56 | + fixture.componentInstance.origin = 1; |
| 57 | + fixture.detectChanges(); |
| 58 | + |
| 59 | + expect(fixture.componentInstance.mdTabBody._position).toBe('right-origin-center'); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('in RTL direction', () => { |
| 64 | + beforeEach(() => { |
| 65 | + dir = 'rtl'; |
| 66 | + fixture = TestBed.createComponent(SimpleTabBodyApp); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should be right-origin-center position with negative or zero origin', () => { |
| 70 | + fixture.componentInstance.position = 0; |
| 71 | + fixture.componentInstance.origin = 0; |
| 72 | + fixture.detectChanges(); |
| 73 | + |
| 74 | + expect(fixture.componentInstance.mdTabBody._position).toBe('right-origin-center'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should be left-origin-center position with positive nonzero origin', () => { |
| 78 | + fixture.componentInstance.position = 0; |
| 79 | + fixture.componentInstance.origin = 1; |
| 80 | + fixture.detectChanges(); |
| 81 | + |
| 82 | + expect(fixture.componentInstance.mdTabBody._position).toBe('left-origin-center'); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('should properly set the position in LTR', () => { |
| 88 | + let fixture: ComponentFixture<SimpleTabBodyApp>; |
| 89 | + |
| 90 | + beforeEach(() => { |
| 91 | + dir = 'ltr'; |
| 92 | + fixture = TestBed.createComponent(SimpleTabBodyApp); |
| 93 | + fixture.detectChanges(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('to be left position with negative position', () => { |
| 97 | + fixture.componentInstance.position = -1; |
| 98 | + fixture.detectChanges(); |
| 99 | + |
| 100 | + expect(fixture.componentInstance.mdTabBody._position).toBe('left'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('to be center position with zero position', () => { |
| 104 | + fixture.componentInstance.position = 0; |
| 105 | + fixture.detectChanges(); |
| 106 | + |
| 107 | + expect(fixture.componentInstance.mdTabBody._position).toBe('center'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('to be left position with positive position', () => { |
| 111 | + fixture.componentInstance.position = 1; |
| 112 | + fixture.detectChanges(); |
| 113 | + |
| 114 | + expect(fixture.componentInstance.mdTabBody._position).toBe('right'); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('should properly set the position in RTL', () => { |
| 119 | + let fixture: ComponentFixture<SimpleTabBodyApp>; |
| 120 | + |
| 121 | + beforeEach(() => { |
| 122 | + dir = 'rtl'; |
| 123 | + fixture = TestBed.createComponent(SimpleTabBodyApp); |
| 124 | + fixture.detectChanges(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('to be right position with negative position', () => { |
| 128 | + fixture.componentInstance.position = -1; |
| 129 | + fixture.detectChanges(); |
| 130 | + |
| 131 | + expect(fixture.componentInstance.mdTabBody._position).toBe('right'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('to be center position with zero position', () => { |
| 135 | + fixture.componentInstance.position = 0; |
| 136 | + fixture.detectChanges(); |
| 137 | + |
| 138 | + expect(fixture.componentInstance.mdTabBody._position).toBe('center'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('to be left position with positive position', () => { |
| 142 | + fixture.componentInstance.position = 1; |
| 143 | + fixture.detectChanges(); |
| 144 | + |
| 145 | + expect(fixture.componentInstance.mdTabBody._position).toBe('left'); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('on centered', () => { |
| 150 | + let fixture: ComponentFixture<SimpleTabBodyApp>; |
| 151 | + |
| 152 | + beforeEach(() => { |
| 153 | + fixture = TestBed.createComponent(SimpleTabBodyApp); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should attach the content when centered and detach when not', fakeAsync(() => { |
| 157 | + fixture.componentInstance.position = 1; |
| 158 | + fixture.detectChanges(); |
| 159 | + expect(fixture.componentInstance.mdTabBody._portalHost.hasAttached()).toBe(false); |
| 160 | + |
| 161 | + fixture.componentInstance.position = 0; |
| 162 | + fixture.detectChanges(); |
| 163 | + expect(fixture.componentInstance.mdTabBody._portalHost.hasAttached()).toBe(true); |
| 164 | + |
| 165 | + fixture.componentInstance.position = 1; |
| 166 | + fixture.detectChanges(); |
| 167 | + flushMicrotasks(); // Finish animation and let it detach in animation done handler |
| 168 | + expect(fixture.componentInstance.mdTabBody._portalHost.hasAttached()).toBe(false); |
| 169 | + })); |
| 170 | + }); |
| 171 | +}); |
| 172 | + |
| 173 | + |
| 174 | +@Component({ |
| 175 | + template: ` |
| 176 | + <template>Tab Body Content</template> |
| 177 | + <md-tab-body [md-tab-body-content]="content" |
| 178 | + [md-tab-body-position]="position" |
| 179 | + [md-tab-body-origin]="origin"> |
| 180 | + </md-tab-body> |
| 181 | + ` |
| 182 | +}) |
| 183 | +class SimpleTabBodyApp { |
| 184 | + content: TemplatePortal; |
| 185 | + position: number; |
| 186 | + origin: number; |
| 187 | + |
| 188 | + @ViewChild(MdTabBody) mdTabBody: MdTabBody; |
| 189 | + @ViewChild(TemplateRef) template: TemplateRef<any>; |
| 190 | + |
| 191 | + constructor(private _viewContainerRef: ViewContainerRef) { } |
| 192 | + |
| 193 | + ngAfterContentInit() { |
| 194 | + this.content = new TemplatePortal(this.template, this._viewContainerRef); |
| 195 | + } |
| 196 | +} |
0 commit comments