From ab3f2365fd8e723dd2af40c04d729ee30bcc04d8 Mon Sep 17 00:00:00 2001 From: Julie Ralph Date: Fri, 26 Jun 2015 16:16:19 -0700 Subject: [PATCH] chore(test): move ng-style tests to TestComponentBuilder Part of #2354 --- .../angular2/test/directives/ng_style_spec.ts | 110 ++++++++++-------- 1 file changed, 64 insertions(+), 46 deletions(-) diff --git a/modules/angular2/test/directives/ng_style_spec.ts b/modules/angular2/test/directives/ng_style_spec.ts index 0425143090..201f54d503 100644 --- a/modules/angular2/test/directives/ng_style_spec.ts +++ b/modules/angular2/test/directives/ng_style_spec.ts @@ -1,5 +1,6 @@ import { AsyncTestCompleter, + TestComponentBuilder, beforeEach, beforeEachBindings, ddescribe, @@ -17,7 +18,6 @@ import {StringMapWrapper} from 'angular2/src/facade/collection'; import {Component, View} from 'angular2/angular2'; -import {TestBed} from 'angular2/src/test_lib/test_bed'; import {DOM} from 'angular2/src/dom/dom_adapter'; import {NgStyle} from 'angular2/src/directives/ng_style'; @@ -25,93 +25,111 @@ export function main() { describe('binding to CSS styles', () => { it('should add styles specified in an object literal', - inject([TestBed, AsyncTestCompleter], (tb: TestBed, async) => { + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { var template = `
`; - tb.createView(TestComponent, {html: template}) - .then((view) => { - view.detectChanges(); - expect(DOM.getStyle(view.rootNodes[0], 'text-align')).toEqual('right'); + tcb.overrideTemplate(TestComponent, template) + .createAsync(TestComponent) + .then((rootTC) => { + rootTC.detectChanges(); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'text-align')) + .toEqual('right'); async.done(); }); })); it('should add and change styles specified in an object expression', - inject([TestBed, AsyncTestCompleter], (tb: TestBed, async) => { + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { var template = `
`; - tb.createView(TestComponent, {html: template}) - .then((view) => { + tcb.overrideTemplate(TestComponent, template) + .createAsync(TestComponent) + .then((rootTC) => { var expr: Map; - view.context.expr = {'text-align': 'right'}; - view.detectChanges(); - expect(DOM.getStyle(view.rootNodes[0], 'text-align')).toEqual('right'); + rootTC.componentInstance.expr = {'text-align': 'right'}; + rootTC.detectChanges(); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'text-align')) + .toEqual('right'); - expr = view.context.expr; + expr = rootTC.componentInstance.expr; expr['text-align'] = 'left'; - view.detectChanges(); - expect(DOM.getStyle(view.rootNodes[0], 'text-align')).toEqual('left'); + rootTC.detectChanges(); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'text-align')) + .toEqual('left'); async.done(); }); })); it('should remove styles when deleting a key in an object expression', - inject([TestBed, AsyncTestCompleter], (tb: TestBed, async) => { + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { var template = `
`; - tb.createView(TestComponent, {html: template}) - .then((view) => { - view.context.expr = {'text-align': 'right'}; - view.detectChanges(); - expect(DOM.getStyle(view.rootNodes[0], 'text-align')).toEqual('right'); + tcb.overrideTemplate(TestComponent, template) + .createAsync(TestComponent) + .then((rootTC) => { + rootTC.componentInstance.expr = {'text-align': 'right'}; + rootTC.detectChanges(); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'text-align')) + .toEqual('right'); - StringMapWrapper.delete(view.context.expr, 'text-align'); - view.detectChanges(); - expect(DOM.getStyle(view.rootNodes[0], 'text-align')).toEqual(''); + StringMapWrapper.delete(rootTC.componentInstance.expr, 'text-align'); + rootTC.detectChanges(); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'text-align')) + .toEqual(''); async.done(); }); })); it('should co-operate with the style attribute', - inject([TestBed, AsyncTestCompleter], (tb: TestBed, async) => { + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { var template = `
`; - tb.createView(TestComponent, {html: template}) - .then((view) => { - view.context.expr = {'text-align': 'right'}; - view.detectChanges(); - expect(DOM.getStyle(view.rootNodes[0], 'text-align')).toEqual('right'); - expect(DOM.getStyle(view.rootNodes[0], 'font-size')).toEqual('12px'); + tcb.overrideTemplate(TestComponent, template) + .createAsync(TestComponent) + .then((rootTC) => { + rootTC.componentInstance.expr = {'text-align': 'right'}; + rootTC.detectChanges(); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'text-align')) + .toEqual('right'); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'font-size')) + .toEqual('12px'); - StringMapWrapper.delete(view.context.expr, 'text-align'); - view.detectChanges(); - expect(DOM.getStyle(view.rootNodes[0], 'text-align')).toEqual(''); - expect(DOM.getStyle(view.rootNodes[0], 'font-size')).toEqual('12px'); + StringMapWrapper.delete(rootTC.componentInstance.expr, 'text-align'); + rootTC.detectChanges(); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'text-align')) + .toEqual(''); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'font-size')) + .toEqual('12px'); async.done(); }); })); it('should co-operate with the style.[styleName]="expr" special-case in the compiler', - inject([TestBed, AsyncTestCompleter], (tb: TestBed, async) => { + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { var template = `
`; - tb.createView(TestComponent, {html: template}) - .then((view) => { - view.context.expr = {'text-align': 'right'}; - view.detectChanges(); - expect(DOM.getStyle(view.rootNodes[0], 'text-align')).toEqual('right'); - expect(DOM.getStyle(view.rootNodes[0], 'font-size')).toEqual('12px'); + tcb.overrideTemplate(TestComponent, template) + .createAsync(TestComponent) + .then((rootTC) => { + rootTC.componentInstance.expr = {'text-align': 'right'}; + rootTC.detectChanges(); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'text-align')) + .toEqual('right'); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'font-size')) + .toEqual('12px'); - StringMapWrapper.delete(view.context.expr, 'text-align'); - expect(DOM.getStyle(view.rootNodes[0], 'font-size')).toEqual('12px'); + StringMapWrapper.delete(rootTC.componentInstance.expr, 'text-align'); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'font-size')) + .toEqual('12px'); - view.detectChanges(); - expect(DOM.getStyle(view.rootNodes[0], 'text-align')).toEqual(''); + rootTC.detectChanges(); + expect(DOM.getStyle(rootTC.componentViewChildren[0].nativeElement, 'text-align')) + .toEqual(''); async.done(); });