| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | import {beforeEach, ddescribe, xdescribe, describe, expect, iit, it, xit,} from '@angular/core/testing/testing_internal'; | 
					
						
							|  |  |  | import {async, TestBed, ComponentFixture} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {Component} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {NgStyle} from '@angular/common/src/directives/ng_style'; | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:28:08 +02:00
										 |  |  | function expectNativeEl(fixture: ComponentFixture<any>) { | 
					
						
							|  |  |  |   return <any>expect(fixture.debugElement.children[0].nativeElement); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | export function main() { | 
					
						
							|  |  |  |   describe('binding to CSS styles', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureTestingModule({ | 
					
						
							|  |  |  |         declarations: [TestComponent], | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should add styles specified in an object literal', async(() => { | 
					
						
							|  |  |  |          var template = `<div [ngStyle]="{'max-width': '40px'}"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          TestBed.overrideComponent(TestComponent, {set: {template: template}}); | 
					
						
							|  |  |  |          let fixture = TestBed.createComponent(TestComponent); | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should add and change styles specified in an object expression', async(() => { | 
					
						
							|  |  |  |          var template = `<div [ngStyle]="expr"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          TestBed.overrideComponent(TestComponent, {set: {template: template}}); | 
					
						
							|  |  |  |          let fixture = TestBed.createComponent(TestComponent); | 
					
						
							|  |  |  |          var expr: Map<string, any>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = {'max-width': '40px'}; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          expr = fixture.debugElement.componentInstance.expr; | 
					
						
							|  |  |  |          (expr as any)['max-width'] = '30%'; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '30%'}); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should add and remove styles specified using style.unit notation', async(() => { | 
					
						
							|  |  |  |          var template = `<div [ngStyle]="{'max-width.px': expr}"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          TestBed.overrideComponent(TestComponent, {set: {template: template}}); | 
					
						
							|  |  |  |          let fixture = TestBed.createComponent(TestComponent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = '40'; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = null; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).not.toHaveCssStyle('max-width'); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should update styles using style.unit notation when unit changes', async(() => { | 
					
						
							|  |  |  |          var template = `<div [ngStyle]="expr"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          TestBed.overrideComponent(TestComponent, {set: {template: template}}); | 
					
						
							|  |  |  |          let fixture = TestBed.createComponent(TestComponent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = {'max-width.px': '40'}; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = {'max-width.em': '40'}; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40em'}); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2016-08-04 20:00:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 16:26:54 -07:00
										 |  |  |     // keyValueDiffer is sensitive to key order #9115
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     it('should change styles specified in an object expression', async(() => { | 
					
						
							|  |  |  |          const template = `<div [ngStyle]="expr"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          TestBed.overrideComponent(TestComponent, {set: {template: template}}); | 
					
						
							|  |  |  |          let fixture = TestBed.createComponent(TestComponent); | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = { | 
					
						
							|  |  |  |            // height, width order is important here
 | 
					
						
							|  |  |  |            height: '10px', | 
					
						
							|  |  |  |            width: '10px' | 
					
						
							|  |  |  |          }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'height': '10px', 'width': '10px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = { | 
					
						
							|  |  |  |            // width, height order is important here
 | 
					
						
							|  |  |  |            width: '5px', | 
					
						
							|  |  |  |            height: '5px', | 
					
						
							|  |  |  |          }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'height': '5px', 'width': '5px'}); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should remove styles when deleting a key in an object expression', async(() => { | 
					
						
							|  |  |  |          var template = `<div [ngStyle]="expr"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          TestBed.overrideComponent(TestComponent, {set: {template: template}}); | 
					
						
							|  |  |  |          let fixture = TestBed.createComponent(TestComponent); | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = {'max-width': '40px'}; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          delete fixture.debugElement.componentInstance.expr['max-width']; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).not.toHaveCssStyle('max-width'); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should co-operate with the style attribute', async(() => { | 
					
						
							|  |  |  |          var template = `<div style="font-size: 12px" [ngStyle]="expr"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          TestBed.overrideComponent(TestComponent, {set: {template: template}}); | 
					
						
							|  |  |  |          let fixture = TestBed.createComponent(TestComponent); | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = {'max-width': '40px'}; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px', 'font-size': '12px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          delete fixture.debugElement.componentInstance.expr['max-width']; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).not.toHaveCssStyle('max-width'); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'font-size': '12px'}); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should co-operate with the style.[styleName]="expr" special-case in the compiler', | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        async(() => { | 
					
						
							|  |  |  |          var template = `<div [style.font-size.px]="12" [ngStyle]="expr"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          TestBed.overrideComponent(TestComponent, {set: {template: template}}); | 
					
						
							|  |  |  |          let fixture = TestBed.createComponent(TestComponent); | 
					
						
							|  |  |  |          fixture.debugElement.componentInstance.expr = {'max-width': '40px'}; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px', 'font-size': '12px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          delete fixture.debugElement.componentInstance.expr['max-width']; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).not.toHaveCssStyle('max-width'); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'font-size': '12px'}); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2016-07-21 17:12:00 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 13:36:48 -08:00
										 |  |  | @Component({selector: 'test-cmp', directives: [NgStyle], template: ''}) | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | class TestComponent { | 
					
						
							| 
									
										
										
										
											2016-06-11 21:23:37 -07:00
										 |  |  |   expr: any; | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | } |