angular-docs-cn/modules/angular2/test/directives/class_spec.js

153 lines
4.9 KiB
JavaScript
Raw Normal View History

import {
AsyncTestCompleter,
beforeEach,
beforeEachBindings,
ddescribe,
xdescribe,
describe,
el,
expect,
iit,
inject,
it,
xit,
} from 'angular2/test_lib';
import {StringMapWrapper} from 'angular2/src/facade/collection';
import {View} from 'angular2/src/core/annotations_impl/view';
import {Component} from 'angular2/src/core/annotations_impl/annotations';
2015-03-30 11:11:18 -04:00
import {TestBed} from 'angular2/src/test_lib/test_bed';
import {CSSClass} from 'angular2/src/directives/class';
export function main() {
describe('binding to CSS class list', () => {
2015-03-30 11:11:18 -04:00
it('should add classes specified in an object literal',
inject([TestBed, AsyncTestCompleter], (tb, async) => {
var template = '<div [class]="{foo: true, bar: false}"></div>';
2015-03-30 11:11:18 -04:00
tb.createView(TestComponent, {html: template}).then((view) => {
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding foo');
async.done();
});
}));
2015-03-30 11:11:18 -04:00
it('should add and remove classes based on changes in object literal values',
inject([TestBed, AsyncTestCompleter], (tb, async) => {
var template = '<div [class]="{foo: condition, bar: !condition}"></div>';
2015-03-30 11:11:18 -04:00
tb.createView(TestComponent, {html: template}).then((view) => {
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding foo');
2015-03-30 11:11:18 -04:00
view.context.condition = false;
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding bar');
async.done();
});
}));
2015-03-30 11:11:18 -04:00
it('should add and remove classes based on changes to the expression object',
inject([TestBed, AsyncTestCompleter], (tb, async) => {
var template = '<div [class]="expr"></div>';
2015-03-30 11:11:18 -04:00
tb.createView(TestComponent, {html: template}).then((view) => {
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding foo');
2015-03-30 11:11:18 -04:00
StringMapWrapper.set(view.context.expr, 'bar', true);
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding foo bar');
2015-03-30 11:11:18 -04:00
StringMapWrapper.set(view.context.expr, 'baz', true);
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding foo bar baz');
2015-03-30 11:11:18 -04:00
StringMapWrapper.delete(view.context.expr, 'bar');
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding foo baz');
async.done();
});
}));
2015-03-30 11:11:18 -04:00
it('should retain existing classes when expression evaluates to null',
inject([TestBed, AsyncTestCompleter], (tb, async) => {
var template = '<div [class]="expr"></div>';
2015-03-30 11:11:18 -04:00
tb.createView(TestComponent, {html: template}).then((view) => {
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding foo');
2015-03-30 11:11:18 -04:00
view.context.expr = null;
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding foo');
2015-03-30 11:11:18 -04:00
view.context.expr = {'foo': false, 'bar': true};
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('ng-binding bar');
async.done();
});
}));
2015-03-30 11:11:18 -04:00
it('should co-operate with the class attribute',
inject([TestBed, AsyncTestCompleter], (tb, async) => {
var template = '<div [class]="expr" class="init foo"></div>';
2015-03-30 11:11:18 -04:00
tb.createView(TestComponent, {html: template}).then((view) => {
StringMapWrapper.set(view.context.expr, 'bar', true);
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('init foo ng-binding bar');
2015-03-30 11:11:18 -04:00
StringMapWrapper.set(view.context.expr, 'foo', false);
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('init ng-binding bar');
async.done();
});
}));
2015-03-30 11:11:18 -04:00
it('should co-operate with the class attribute and class.name binding',
inject([TestBed, AsyncTestCompleter], (tb, async) => {
var template = '<div class="init foo" [class]="expr" [class.baz]="condition"></div>';
2015-03-30 11:11:18 -04:00
tb.createView(TestComponent, {html: template}).then((view) => {
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('init foo ng-binding baz');
2015-03-30 11:11:18 -04:00
StringMapWrapper.set(view.context.expr, 'bar', true);
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('init foo ng-binding baz bar');
2015-03-30 11:11:18 -04:00
StringMapWrapper.set(view.context.expr, 'foo', false);
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('init ng-binding baz bar');
2015-03-30 11:11:18 -04:00
view.context.condition = false;
view.detectChanges();
expect(view.rootNodes[0].className).toEqual('init ng-binding bar');
async.done();
});
}));
})
}
@Component({selector: 'test-cmp'})
@View({directives: [CSSClass]})
class TestComponent {
condition:boolean;
expr;
constructor() {
this.condition = true;
this.expr = {'foo': true, 'bar': false};
}
}