| 
									
										
										
										
											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-07-11 15:27:57 -07:00
										 |  |  | import {Component} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | import {ComponentFixture, TestBed, async} from '@angular/core/testing'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | export function main() { | 
					
						
							|  |  |  |   describe('binding to CSS class list', () => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |     let fixture: ComponentFixture<any>; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-17 08:56:36 -08:00
										 |  |  |     function normalizeClassNames(classes: string) { | 
					
						
							|  |  |  |       return classes.trim().split(' ').sort().join(' '); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |     function detectChangesAndExpectClassName(classes: string): void { | 
					
						
							|  |  |  |       fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2017-02-17 08:56:36 -08:00
										 |  |  |       let nonNormalizedClassName = fixture.debugElement.children[0].nativeElement.className; | 
					
						
							|  |  |  |       expect(normalizeClassNames(nonNormalizedClassName)).toEqual(normalizeClassNames(classes)); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getComponent(): TestComponent { return fixture.debugElement.componentInstance; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     afterEach(() => { fixture = null; }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureTestingModule({ | 
					
						
							|  |  |  |         declarations: [TestComponent], | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     it('should clean up when the directive is destroyed', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |          fixture = createTestComponent('<div *ngFor="let item of items" [ngClass]="item"></div>'); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |          getComponent().items = [['0']]; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |          getComponent().items = [['1']]; | 
					
						
							|  |  |  |          detectChangesAndExpectClassName('1'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-07-23 16:22:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  |     describe('expressions evaluating to objects', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should add classes specified in an object literal', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="{foo: true, bar: false}"></div>'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 10:49:47 +02:00
										 |  |  |       it('should add classes specified in an object literal without change in class names', | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = | 
					
						
							|  |  |  |                createTestComponent(`<div [ngClass]="{'foo-bar': true, 'fooBar': true}"></div>`); | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo-bar fooBar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should add and remove classes based on changes in object literal values', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = | 
					
						
							|  |  |  |                createTestComponent('<div [ngClass]="{foo: condition, bar: !condition}"></div>'); | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().condition = false; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should add and remove classes based on changes to the expression object', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="objExpr"></div>'); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const objExpr = getComponent().objExpr; | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['bar'] = true; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar'); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['baz'] = true; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar baz'); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            delete (objExpr['bar']); | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo baz'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should add and remove classes based on reference changes to the expression object', | 
					
						
							|  |  |  |          async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="objExpr"></div>'); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = {foo: true, bar: true}; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar'); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = {baz: true}; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('baz'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should remove active classes when expression evaluates to null', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="objExpr"></div>'); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = null; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName(''); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = {'foo': false, 'bar': true}; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should allow multiple classes per expression', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="objExpr"></div>'); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = {'bar baz': true, 'bar1 baz1': true}; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('bar baz bar1 baz1'); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = {'bar baz': false, 'bar1 baz1': true}; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('bar1 baz1'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should split by one or more spaces between classes', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="objExpr"></div>'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = {'foo bar     baz': true}; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar baz'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     describe('expressions evaluating to lists', () => { | 
					
						
							| 
									
										
										
										
											2015-11-10 13:30:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should add classes specified in a list literal', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = | 
					
						
							|  |  |  |                createTestComponent(`<div [ngClass]="['foo', 'bar', 'foo-bar', 'fooBar']"></div>`); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo bar foo-bar fooBar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should add and remove classes based on changes to the expression', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="arrExpr"></div>'); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const arrExpr = getComponent().arrExpr; | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            arrExpr.push('bar'); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            arrExpr[1] = 'baz'; | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('foo baz'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().arrExpr = arrExpr.filter((v: string) => v !== 'baz'); | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should add and remove classes when a reference changes', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="arrExpr"></div>'); | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().arrExpr = ['bar']; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should take initial classes into account when a reference changes', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div class="foo" [ngClass]="arrExpr"></div>'); | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().arrExpr = ['bar']; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should ignore empty or blank class names', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div class="foo" [ngClass]="arrExpr"></div>'); | 
					
						
							|  |  |  |            getComponent().arrExpr = ['', '  ']; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should trim blanks from class names', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div class="foo" [ngClass]="arrExpr"></div>'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().arrExpr = [' bar  ']; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should allow multiple classes per item in arrays', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="arrExpr"></div>'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().arrExpr = ['foo bar baz', 'foo1 bar1   baz1']; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar baz foo1 bar1 baz1'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().arrExpr = ['foo bar   baz foobar']; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar baz foobar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2016-11-07 21:23:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should throw with descriptive error message when CSS class is not a string', () => { | 
					
						
							|  |  |  |         fixture = createTestComponent(`<div [ngClass]="['foo', {}]"></div>`); | 
					
						
							|  |  |  |         expect(() => fixture.detectChanges()) | 
					
						
							|  |  |  |             .toThrowError( | 
					
						
							|  |  |  |                 /NgClass can only toggle CSS classes expressed as strings, got \[object Object\]/); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 09:50:51 -07:00
										 |  |  |     describe('expressions evaluating to sets', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should add and remove classes if the set instance changed', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="setExpr"></div>'); | 
					
						
							|  |  |  |            let setExpr = new Set<string>(); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |            setExpr.add('bar'); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().setExpr = setExpr; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            setExpr = new Set<string>(); | 
					
						
							|  |  |  |            setExpr.add('baz'); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().setExpr = setExpr; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('baz'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-10-26 09:50:51 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  |     describe('expressions evaluating to string', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should add classes specified in a string literal', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent(`<div [ngClass]="'foo bar foo-bar fooBar'"></div>`); | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar foo-bar fooBar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should add and remove classes based on changes to the expression', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="strExpr"></div>'); | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().strExpr = 'foo bar'; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo bar'); | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().strExpr = 'baz'; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('baz'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should remove active classes when switching from string to null', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent(`<div [ngClass]="strExpr"></div>`); | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2015-07-08 17:41:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().strExpr = null; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName(''); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-07-08 17:41:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should take initial classes into account when switching from string to null', | 
					
						
							|  |  |  |          async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent(`<div class="foo" [ngClass]="strExpr"></div>`); | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2015-07-08 17:41:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().strExpr = null; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should ignore empty and blank strings', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent(`<div class="foo" [ngClass]="strExpr"></div>`); | 
					
						
							|  |  |  |            getComponent().strExpr = ''; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('foo'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-09-14 14:11:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  |     describe('cooperation with other class-changing constructs', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should co-operate with the class attribute', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent('<div [ngClass]="objExpr" class="init foo"></div>'); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const objExpr = getComponent().objExpr; | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['bar'] = true; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('init foo bar'); | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['foo'] = false; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('init bar'); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = null; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('init foo'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should co-operate with the interpolated class attribute', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent(`<div [ngClass]="objExpr" class="{{'init foo'}}"></div>`); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const objExpr = getComponent().objExpr; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['bar'] = true; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName(`init foo bar`); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['foo'] = false; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName(`init bar`); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = null; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName(`init foo`); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should co-operate with the class attribute and binding to it', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = | 
					
						
							|  |  |  |                createTestComponent(`<div [ngClass]="objExpr" class="init" [class]="'foo'"></div>`); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const objExpr = getComponent().objExpr; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['bar'] = true; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName(`init foo bar`); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['foo'] = false; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName(`init bar`); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().objExpr = null; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName(`init foo`); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should co-operate with the class attribute and class.name binding', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            const template = | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |                '<div class="init foo" [ngClass]="objExpr" [class.baz]="condition"></div>'; | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const objExpr = getComponent().objExpr; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('init foo baz'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['bar'] = true; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('init foo baz bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            objExpr['foo'] = false; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('init baz bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            getComponent().condition = false; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('init bar'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should co-operate with initial class and class attribute binding when binding changes', | 
					
						
							|  |  |  |          async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            const template = '<div class="init" [ngClass]="objExpr" [class]="strExpr"></div>'; | 
					
						
							|  |  |  |            fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const cmp = getComponent(); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            detectChangesAndExpectClassName('init foo'); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            cmp.objExpr['bar'] = true; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('init foo bar'); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |            cmp.strExpr = 'baz'; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('init bar baz foo'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            cmp.objExpr = null; | 
					
						
							|  |  |  |            detectChangesAndExpectClassName('init baz'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:25:46 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-07-21 17:12:00 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  | @Component({selector: 'test-cmp', template: ''}) | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | class TestComponent { | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  |   condition: boolean = true; | 
					
						
							| 
									
										
										
										
											2015-07-23 16:22:14 -07:00
										 |  |  |   items: any[]; | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   arrExpr: string[] = ['foo']; | 
					
						
							| 
									
										
										
										
											2015-10-26 09:50:51 -07:00
										 |  |  |   setExpr: Set<string> = new Set<string>(); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  |   objExpr: {[klass: string]: any} = {'foo': true, 'bar': false}; | 
					
						
							| 
									
										
										
										
											2015-06-21 11:54:21 +02:00
										 |  |  |   strExpr = 'foo'; | 
					
						
							| 
									
										
										
										
											2015-10-26 09:50:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   constructor() { this.setExpr.add('foo'); } | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-09 12:03:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | function createTestComponent(template: string): ComponentFixture<TestComponent> { | 
					
						
							|  |  |  |   return TestBed.overrideComponent(TestComponent, {set: {template: template}}) | 
					
						
							|  |  |  |       .createComponent(TestComponent); | 
					
						
							| 
									
										
										
										
											2016-11-07 21:23:31 +01:00
										 |  |  | } |