2015-05-26 17:22:35 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
2015-06-24 16:07:15 -04:00
|
|
|
TestComponentBuilder,
|
2015-05-26 17:22:35 -04:00
|
|
|
beforeEach,
|
|
|
|
beforeEachBindings,
|
|
|
|
ddescribe,
|
|
|
|
xdescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
} from 'angular2/test_lib';
|
2015-06-21 05:54:21 -04:00
|
|
|
import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
2015-05-26 17:22:35 -04:00
|
|
|
import {Component, View} from 'angular2/angular2';
|
2015-06-21 05:54:21 -04:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-05-26 17:22:35 -04:00
|
|
|
import {CSSClass} from 'angular2/src/directives/class';
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('binding to CSS class list', () => {
|
|
|
|
|
2015-06-21 05:54:21 -04:00
|
|
|
describe('expressions evaluating to objects', () => {
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-21 05:54:21 -04:00
|
|
|
it('should add classes specified in an object literal',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div [class]="{foo: true, bar: false}"></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-21 05:54:21 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-21 05:54:21 -04:00
|
|
|
it('should add and remove classes based on changes in object literal values',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div [class]="{foo: condition, bar: !condition}"></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.componentInstance.condition = false;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding bar');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should add and remove classes based on changes to the expression object',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div [class]="objExpr"></div>';
|
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'bar', true);
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo bar');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'baz', true);
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo bar baz');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
StringMapWrapper.delete(rootTC.componentInstance.objExpr, 'bar');
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo baz');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should add and remove classes based on reference changes to the expression object',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div [class]="objExpr"></div>';
|
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.componentInstance.objExpr = {foo: true, bar: true};
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo bar');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.componentInstance.objExpr = {baz: true};
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding baz');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-21 05:54:21 -04:00
|
|
|
describe('expressions evaluating to lists', () => {
|
|
|
|
|
|
|
|
it('should add classes specified in a list literal',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = `<div [class]="['foo', 'bar']"></div>`;
|
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo bar');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should add and remove classes based on changes to the expression',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div [class]="arrExpr"></div>';
|
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
var arrExpr: List<string> = rootTC.componentInstance.arrExpr;
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
arrExpr.push('bar');
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo bar');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
arrExpr[1] = 'baz';
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo baz');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
ListWrapper.remove(rootTC.componentInstance.arrExpr, 'baz');
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should add and remove classes when a reference changes',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div [class]="arrExpr"></div>';
|
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.componentInstance.arrExpr = ['bar'];
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding bar');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('expressions evaluating to string', () => {
|
|
|
|
|
|
|
|
it('should add classes specified in a string literal',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = `<div [class]="'foo bar'"></div>`;
|
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo bar');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should add and remove classes based on changes to the expression',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div [class]="strExpr"></div>';
|
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.componentInstance.strExpr = 'foo bar';
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo bar');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.componentInstance.strExpr = 'baz';
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding baz');
|
2015-06-21 05:54:21 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove active classes when expression evaluates to null',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div [class]="objExpr"></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding foo');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.componentInstance.objExpr = null;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
rootTC.componentInstance.objExpr = {'foo': false, 'bar': true};
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('ng-binding bar');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-21 05:54:21 -04:00
|
|
|
it('should have no effect when activated by a static class attribute',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div class="init foo"></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
2015-06-21 05:54:21 -04:00
|
|
|
// TODO(pk): in CJS className isn't initialized properly if we don't mutate classes
|
2015-06-24 16:07:15 -04:00
|
|
|
expect(ListWrapper.join(DOM.classList(rootTC.componentViewChildren[0].nativeElement),
|
|
|
|
' '))
|
2015-06-21 05:54:21 -04:00
|
|
|
.toEqual('init foo ng-binding');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should co-operate with the class attribute',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div [class]="objExpr" class="init foo"></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'bar', true);
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('init foo ng-binding bar');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'foo', false);
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('init ng-binding bar');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should co-operate with the class attribute and class.name binding',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-06-21 05:54:21 -04:00
|
|
|
var template = '<div class="init foo" [class]="objExpr" [class.baz]="condition"></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((rootTC) => {
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('init foo ng-binding baz');
|
|
|
|
|
|
|
|
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'bar', true);
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('init foo ng-binding baz bar');
|
|
|
|
|
|
|
|
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'foo', false);
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('init ng-binding baz bar');
|
|
|
|
|
|
|
|
rootTC.componentInstance.condition = false;
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.componentViewChildren[0].nativeElement.className)
|
|
|
|
.toEqual('init ng-binding bar');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'test-cmp'})
|
|
|
|
@View({directives: [CSSClass]})
|
|
|
|
class TestComponent {
|
2015-06-21 05:54:21 -04:00
|
|
|
condition: boolean = true;
|
|
|
|
arrExpr: List<string> = ['foo'];
|
|
|
|
objExpr = {'foo': true, 'bar': false};
|
|
|
|
strExpr = 'foo';
|
2015-05-26 17:22:35 -04:00
|
|
|
}
|