| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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-19 12:51:01 -07:00
										 |  |  | import {CommonModule} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {Component} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  | import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |   describe('NgStyle', () => { | 
					
						
							| 
									
										
										
										
											2020-01-11 11:23:53 -08:00
										 |  |  |     let fixture: ComponentFixture<TestComponent>; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     function getComponent(): TestComponent { | 
					
						
							|  |  |  |       return fixture.componentInstance; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function expectNativeEl(fixture: ComponentFixture<any>): any { | 
					
						
							|  |  |  |       return expect(fixture.debugElement.children[0].nativeElement); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     afterEach(() => { | 
					
						
							|  |  |  |       fixture = null!; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  |       TestBed.configureTestingModule({declarations: [TestComponent], imports: [CommonModule]}); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |     it('should add styles specified in an object literal', waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = `<div [ngStyle]="{'max-width': '40px'}"></div>`; | 
					
						
							|  |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |     it('should add and change styles specified in an object expression', waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = `<div [ngStyle]="expr"></div>`; | 
					
						
							|  |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().expr = {'max-width': '40px'}; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          let expr = getComponent().expr; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          expr['max-width'] = '30%'; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '30%'}); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |     it('should add and remove styles specified using style.unit notation', waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = `<div [ngStyle]="{'max-width.px': expr}"></div>`; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().expr = '40'; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().expr = null; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).not.toHaveCssStyle('max-width'); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-19 17:14:05 -08:00
										 |  |  |     // https://github.com/angular/angular/issues/21064
 | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |     it('should add and remove styles which names are not dash-cased', waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2018-01-19 17:14:05 -08:00
										 |  |  |          fixture = createTestComponent(`<div [ngStyle]="{'color': expr}"></div>`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().expr = 'green'; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'color': 'green'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().expr = null; | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).not.toHaveCssStyle('color'); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |     it('should update styles using style.unit notation when unit changes', waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = `<div [ngStyle]="expr"></div>`; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().expr = {'max-width.px': '40'}; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().expr = {'max-width.em': '40'}; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          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
 | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |     it('should change styles specified in an object expression', waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          const template = `<div [ngStyle]="expr"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().expr = { | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |            // height, width order is important here
 | 
					
						
							|  |  |  |            height: '10px', | 
					
						
							|  |  |  |            width: '10px' | 
					
						
							|  |  |  |          }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'height': '10px', 'width': '10px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().expr = { | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |            // width, height order is important here
 | 
					
						
							|  |  |  |            width: '5px', | 
					
						
							|  |  |  |            height: '5px', | 
					
						
							|  |  |  |          }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'height': '5px', 'width': '5px'}); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |     it('should remove styles when deleting a key in an object expression', waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = `<div [ngStyle]="expr"></div>`; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().expr = {'max-width': '40px'}; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          delete getComponent().expr['max-width']; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).not.toHaveCssStyle('max-width'); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |     it('should co-operate with the style attribute', waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = `<div style="font-size: 12px" [ngStyle]="expr"></div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().expr = {'max-width': '40px'}; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px', 'font-size': '12px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          delete getComponent().expr['max-width']; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          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', | 
					
						
							| 
									
										
										
										
											2020-08-01 04:43:18 +09:00
										 |  |  |        waitForAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          const template = `<div [style.font-size.px]="12" [ngStyle]="expr"></div>`; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().expr = {'max-width': '40px'}; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px', 'font-size': '12px'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          delete getComponent().expr['max-width']; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  |          expectNativeEl(fixture).not.toHaveCssStyle('max-width'); | 
					
						
							|  |  |  |          expectNativeEl(fixture).toHaveCssStyle({'font-size': '12px'}); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2019-12-16 10:17:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-11 11:23:53 -08:00
										 |  |  |     it('should not write to the native node unless the bound expression has changed', () => { | 
					
						
							|  |  |  |       const template = `<div [ngStyle]="{'color': expr}"></div>`; | 
					
						
							| 
									
										
										
										
											2019-12-16 10:17:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-08 20:19:28 -08:00
										 |  |  |       fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2020-01-11 11:23:53 -08:00
										 |  |  |       fixture.componentInstance.expr = 'red'; | 
					
						
							| 
									
										
										
										
											2019-12-16 10:17:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-08 20:19:28 -08:00
										 |  |  |       fixture.detectChanges(); | 
					
						
							|  |  |  |       expectNativeEl(fixture).toHaveCssStyle({'color': 'red'}); | 
					
						
							| 
									
										
										
										
											2019-12-16 10:17:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-11 11:23:53 -08:00
										 |  |  |       // Overwrite native styles so that we can check if ngStyle has performed DOM manupulation to
 | 
					
						
							|  |  |  |       // update it.
 | 
					
						
							| 
									
										
										
										
											2019-12-08 20:19:28 -08:00
										 |  |  |       fixture.debugElement.children[0].nativeElement.style.color = 'blue'; | 
					
						
							|  |  |  |       fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2020-01-11 11:23:53 -08:00
										 |  |  |       // Assert that the style hasn't been updated
 | 
					
						
							| 
									
										
										
										
											2019-12-08 20:19:28 -08:00
										 |  |  |       expectNativeEl(fixture).toHaveCssStyle({'color': 'blue'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-11 11:23:53 -08:00
										 |  |  |       fixture.componentInstance.expr = 'yellow'; | 
					
						
							|  |  |  |       fixture.detectChanges(); | 
					
						
							|  |  |  |       // Assert that the style has changed now that the model has changed
 | 
					
						
							|  |  |  |       expectNativeEl(fixture).toHaveCssStyle({'color': 'yellow'}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should correctly update style with units (.px) when the model is set to number', () => { | 
					
						
							|  |  |  |       const template = `<div [ngStyle]="{'width.px': expr}"></div>`; | 
					
						
							|  |  |  |       fixture = createTestComponent(template); | 
					
						
							|  |  |  |       fixture.componentInstance.expr = 400; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       fixture.detectChanges(); | 
					
						
							|  |  |  |       expectNativeEl(fixture).toHaveCssStyle({'width': '400px'}); | 
					
						
							| 
									
										
										
										
											2019-12-08 20:19:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-07-21 17:12:00 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-06-21 14:18:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  | @Component({selector: 'test-cmp', 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
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | function createTestComponent(template: string): ComponentFixture<TestComponent> { | 
					
						
							|  |  |  |   return TestBed.overrideComponent(TestComponent, {set: {template: template}}) | 
					
						
							|  |  |  |       .createComponent(TestComponent); | 
					
						
							|  |  |  | } |