Files
Angular-Docs-Cn/modules/@angular/common/test/directives/ng_style_spec.ts
T

156 lines
6.0 KiB
TypeScript
Raw Normal View History

import {
beforeEach,
beforeEachProviders,
ddescribe,
xdescribe,
describe,
expect,
iit,
inject,
it,
xit,
} from '@angular/core/testing/testing_internal';
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
2015-06-21 14:18:28 +02:00
import {StringMapWrapper} from '../../src/facade/collection';
2015-06-21 14:18:28 +02:00
import {Component} from '@angular/core';
2015-06-21 14:18:28 +02:00
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {NgStyle} from '@angular/common/src/directives/ng_style';
2015-06-21 14:18:28 +02:00
export function main() {
describe('binding to CSS styles', () => {
it('should add styles specified in an object literal',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = `<div [ngStyle]="{'max-width': '40px'}"></div>`;
2015-06-21 14:18:28 +02:00
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((fixture) => {
fixture.detectChanges();
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'max-width'))
2015-07-29 18:34:02 +02:00
.toEqual('40px');
2015-06-21 14:18:28 +02:00
async.done();
});
}));
it('should add and change styles specified in an object expression',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = `<div [ngStyle]="expr"></div>`;
2015-06-21 14:18:28 +02:00
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((fixture) => {
2015-06-21 14:18:28 +02:00
var expr: Map<string, any>;
fixture.debugElement.componentInstance.expr = {'max-width': '40px'};
fixture.detectChanges();
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'max-width'))
2015-07-29 18:34:02 +02:00
.toEqual('40px');
2015-06-21 14:18:28 +02:00
expr = fixture.debugElement.componentInstance.expr;
2015-07-29 18:34:02 +02:00
expr['max-width'] = '30%';
fixture.detectChanges();
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'max-width'))
2015-07-29 18:34:02 +02:00
.toEqual('30%');
2015-06-21 14:18:28 +02:00
async.done();
});
}));
it('should remove styles when deleting a key in an object expression',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = `<div [ngStyle]="expr"></div>`;
2015-06-21 14:18:28 +02:00
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((fixture) => {
fixture.debugElement.componentInstance.expr = {'max-width': '40px'};
fixture.detectChanges();
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'max-width'))
2015-07-29 18:34:02 +02:00
.toEqual('40px');
2015-06-21 14:18:28 +02:00
StringMapWrapper.delete(fixture.debugElement.componentInstance.expr, 'max-width');
fixture.detectChanges();
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'max-width'))
.toEqual('');
2015-06-21 14:18:28 +02:00
async.done();
});
}));
it('should co-operate with the style attribute',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = `<div style="font-size: 12px" [ngStyle]="expr"></div>`;
2015-06-21 14:18:28 +02:00
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((fixture) => {
fixture.debugElement.componentInstance.expr = {'max-width': '40px'};
fixture.detectChanges();
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'max-width'))
2015-07-29 18:34:02 +02:00
.toEqual('40px');
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'font-size'))
.toEqual('12px');
2015-06-21 14:18:28 +02:00
StringMapWrapper.delete(fixture.debugElement.componentInstance.expr, 'max-width');
fixture.detectChanges();
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'max-width'))
.toEqual('');
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'font-size'))
.toEqual('12px');
2015-06-21 14:18:28 +02:00
async.done();
});
}));
it('should co-operate with the style.[styleName]="expr" special-case in the compiler',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = `<div [style.font-size.px]="12" [ngStyle]="expr"></div>`;
2015-06-21 14:18:28 +02:00
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((fixture) => {
fixture.debugElement.componentInstance.expr = {'max-width': '40px'};
fixture.detectChanges();
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'max-width'))
2015-07-29 18:34:02 +02:00
.toEqual('40px');
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'font-size'))
.toEqual('12px');
2015-06-21 14:18:28 +02:00
StringMapWrapper.delete(fixture.debugElement.componentInstance.expr, 'max-width');
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'font-size'))
.toEqual('12px');
2015-06-21 14:18:28 +02:00
fixture.detectChanges();
2016-05-01 22:50:37 -07:00
expect(
getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'max-width'))
.toEqual('');
2015-06-21 14:18:28 +02:00
async.done();
});
}));
})
}
2016-03-08 13:36:48 -08:00
@Component({selector: 'test-cmp', directives: [NgStyle], template: ''})
2015-06-21 14:18:28 +02:00
class TestComponent {
expr;
}