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