From 0a22e8eefd8d1d2e6208d0ed6fe1067cc2092197 Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Wed, 17 Aug 2016 07:15:35 -0700 Subject: [PATCH] refactor(core): Removing linker test references to TestComponentBuilder (#10865) Removed references to TestComponentBuilder from integration_spec.ts --- .../core/test/linker/integration_spec.ts | 2771 +++++++---------- 1 file changed, 1090 insertions(+), 1681 deletions(-) diff --git a/modules/@angular/core/test/linker/integration_spec.ts b/modules/@angular/core/test/linker/integration_spec.ts index 4a8aeaa320..01a2661123 100644 --- a/modules/@angular/core/test/linker/integration_spec.ts +++ b/modules/@angular/core/test/linker/integration_spec.ts @@ -18,8 +18,8 @@ import {EmbeddedViewRef} from '@angular/core/src/linker/view_ref'; import {Attribute, Component, ContentChildren, Directive, HostBinding, HostListener, Input, Output, Pipe} from '@angular/core/src/metadata'; import {ViewMetadata} from '@angular/core/src/metadata/view'; import {Renderer} from '@angular/core/src/render'; -import {ComponentFixture, TestBed, async, fakeAsync, tick} from '@angular/core/testing'; -import {AsyncTestCompleter, TestComponentBuilder, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal'; +import {ComponentFixture, TestBed, async, fakeAsync, getTestBed, tick} from '@angular/core/testing'; +import {AsyncTestCompleter, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal'; import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; import {dispatchEvent, el} from '@angular/platform-browser/testing/browser_util'; import {expect} from '@angular/platform-browser/testing/matchers'; @@ -39,791 +39,514 @@ export function main() { function declareTests({useJit}: {useJit: boolean}) { describe('integration tests', function() { - beforeEach(() => { - TestBed.configureCompiler({useJit: useJit}); - TestBed.configureTestingModule( - {providers: [{provide: ANCHOR_ELEMENT, useValue: el('
')}]}); - }); + beforeEach(() => { TestBed.configureCompiler({useJit: useJit}); }); describe('react to record changes', function() { - it('should consume text node changes', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView(MyComp, new ViewMetadata({template: '
{{ctxProp}}
'})) - .createAsync(MyComp) - .then((fixture) => { - fixture.debugElement.componentInstance.ctxProp = 'Hello World!'; + it('should consume text node changes', () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = '
{{ctxProp}}
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + fixture.debugElement.componentInstance.ctxProp = 'Hello World!'; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('Hello World!'); - async.done(); - }); - })); - - it('should update text node with a blank string when interpolation evaluates to null', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, new ViewMetadata({template: '
{{null}}{{ctxProp}}
'})) - .createAsync(MyComp) - .then((fixture) => { - fixture.debugElement.componentInstance.ctxProp = null; - - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText(''); - async.done(); - }); - })); - - it('should consume element binding changes', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView(MyComp, new ViewMetadata({template: '
'})) - .createAsync(MyComp) - .then((fixture) => { - - fixture.debugElement.componentInstance.ctxProp = 'Hello World!'; - fixture.detectChanges(); - - expect(fixture.debugElement.children[0].nativeElement.id) - .toEqual('Hello World!'); - async.done(); - }); - })); - - it('should consume binding to aria-* attributes', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, - new ViewMetadata({template: '
'})) - - .createAsync(MyComp) - .then((fixture) => { - fixture.debugElement.componentInstance.ctxProp = 'Initial aria label'; - fixture.detectChanges(); - expect(getDOM().getAttribute( - fixture.debugElement.children[0].nativeElement, 'aria-label')) - .toEqual('Initial aria label'); - - fixture.debugElement.componentInstance.ctxProp = 'Changed aria label'; - fixture.detectChanges(); - expect(getDOM().getAttribute( - fixture.debugElement.children[0].nativeElement, 'aria-label')) - .toEqual('Changed aria label'); - - async.done(); - }); - })); - - it('should remove an attribute when attribute expression evaluates to null', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, new ViewMetadata({template: '
'})) - - .createAsync(MyComp) - .then((fixture) => { - - fixture.debugElement.componentInstance.ctxProp = 'bar'; - fixture.detectChanges(); - expect(getDOM().getAttribute( - fixture.debugElement.children[0].nativeElement, 'foo')) - .toEqual('bar'); - - fixture.debugElement.componentInstance.ctxProp = null; - fixture.detectChanges(); - expect(getDOM().hasAttribute( - fixture.debugElement.children[0].nativeElement, 'foo')) - .toBeFalsy(); - - async.done(); - }); - })); - - it('should remove style when when style expression evaluates to null', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, - new ViewMetadata({template: '
'})) - - .createAsync(MyComp) - .then((fixture) => { - - fixture.debugElement.componentInstance.ctxProp = '10'; - fixture.detectChanges(); - expect(getDOM().getStyle( - fixture.debugElement.children[0].nativeElement, 'height')) - .toEqual('10px'); - - fixture.debugElement.componentInstance.ctxProp = null; - fixture.detectChanges(); - expect(getDOM().getStyle( - fixture.debugElement.children[0].nativeElement, 'height')) - .toEqual(''); - - async.done(); - }); - })); - - it('should consume binding to property names where attr name and property name do not match', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, new ViewMetadata({template: '
'})) - - .createAsync(MyComp) - .then((fixture) => { - - fixture.detectChanges(); - expect(fixture.debugElement.children[0].nativeElement.tabIndex).toEqual(0); - - fixture.debugElement.componentInstance.ctxNumProp = 5; - fixture.detectChanges(); - expect(fixture.debugElement.children[0].nativeElement.tabIndex).toEqual(5); - - async.done(); - }); - })); - - it('should consume binding to camel-cased properties', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, new ViewMetadata({template: ''})) - - .createAsync(MyComp) - .then((fixture) => { - - fixture.detectChanges(); - expect(fixture.debugElement.children[0].nativeElement.readOnly).toBeFalsy(); - - fixture.debugElement.componentInstance.ctxBoolProp = true; - fixture.detectChanges(); - expect(fixture.debugElement.children[0].nativeElement.readOnly).toBeTruthy(); - - async.done(); - }); - })); - - it('should consume binding to innerHtml', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, new ViewMetadata({template: '
'})) - - .createAsync(MyComp) - .then((fixture) => { - - fixture.debugElement.componentInstance.ctxProp = 'Some HTML'; - fixture.detectChanges(); - expect(getDOM().getInnerHTML(fixture.debugElement.children[0].nativeElement)) - .toEqual('Some HTML'); - - fixture.debugElement.componentInstance.ctxProp = 'Some other
HTML
'; - fixture.detectChanges(); - expect(getDOM().getInnerHTML(fixture.debugElement.children[0].nativeElement)) - .toEqual('Some other
HTML
'); - - async.done(); - }); - })); - - it('should consume binding to className using class alias', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, - new ViewMetadata({template: '
'})) - - .createAsync(MyComp) - .then((fixture) => { - var nativeEl = fixture.debugElement.children[0].nativeElement; - fixture.debugElement.componentInstance.ctxProp = 'foo bar'; - fixture.detectChanges(); - - expect(nativeEl).toHaveCssClass('foo'); - expect(nativeEl).toHaveCssClass('bar'); - expect(nativeEl).not.toHaveCssClass('initial'); - - async.done(); - }); - })); - - it('should consume directive watch expression change.', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var tpl = '' + - '
' + - '
' + - '
' + - '
' + - '
'; - tcb.overrideView(MyComp, new ViewMetadata({template: tpl, directives: [MyDir]})) - - .createAsync(MyComp) - .then((fixture) => { - fixture.debugElement.componentInstance.ctxProp = 'Hello World!'; - fixture.detectChanges(); - - var containerSpan = fixture.debugElement.children[0]; - - expect(containerSpan.children[0].injector.get(MyDir).dirProp) - .toEqual('Hello World!'); - expect(containerSpan.children[1].injector.get(MyDir).dirProp) - .toEqual('Hi there!'); - expect(containerSpan.children[2].injector.get(MyDir).dirProp) - .toEqual('Hi there!'); - expect(containerSpan.children[3].injector.get(MyDir).dirProp) - .toEqual('One more Hello World!'); - async.done(); - }); - })); - - describe('pipes', () => { - it('should support pipes in bindings', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, new ViewMetadata({ - template: '
', - directives: [MyDir], - pipes: [DoublePipe] - })) - - .createAsync(MyComp) - .then((fixture) => { - fixture.debugElement.componentInstance.ctxProp = 'a'; - fixture.detectChanges(); - - var dir = fixture.debugElement.children[0].references['dir']; - expect(dir.dirProp).toEqual('aa'); - async.done(); - }); - })); + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('Hello World!'); }); - it('should support nested components.', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, new ViewMetadata( - {template: '', directives: [ChildComp]})) + it('should update text node with a blank string when interpolation evaluates to null', () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = '
{{null}}{{ctxProp}}
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + fixture.debugElement.componentInstance.ctxProp = null; - .createAsync(MyComp) - .then((fixture) => { + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText(''); + }); - fixture.detectChanges(); + it('should consume element binding changes', () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); - expect(fixture.debugElement.nativeElement).toHaveText('hello'); - async.done(); - }); - })); + fixture.debugElement.componentInstance.ctxProp = 'Hello World!'; + fixture.detectChanges(); + + expect(fixture.debugElement.children[0].nativeElement.id).toEqual('Hello World!'); + }); + + it('should consume binding to aria-* attributes', () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + fixture.debugElement.componentInstance.ctxProp = 'Initial aria label'; + fixture.detectChanges(); + expect(getDOM().getAttribute(fixture.debugElement.children[0].nativeElement, 'aria-label')) + .toEqual('Initial aria label'); + + fixture.debugElement.componentInstance.ctxProp = 'Changed aria label'; + fixture.detectChanges(); + expect(getDOM().getAttribute(fixture.debugElement.children[0].nativeElement, 'aria-label')) + .toEqual('Changed aria label'); + }); + + it('should remove an attribute when attribute expression evaluates to null', () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + fixture.debugElement.componentInstance.ctxProp = 'bar'; + fixture.detectChanges(); + expect(getDOM().getAttribute(fixture.debugElement.children[0].nativeElement, 'foo')) + .toEqual('bar'); + + fixture.debugElement.componentInstance.ctxProp = null; + fixture.detectChanges(); + expect(getDOM().hasAttribute(fixture.debugElement.children[0].nativeElement, 'foo')) + .toBeFalsy(); + }); + + it('should remove style when when style expression evaluates to null', () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + fixture.debugElement.componentInstance.ctxProp = '10'; + fixture.detectChanges(); + expect(getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'height')) + .toEqual('10px'); + + fixture.debugElement.componentInstance.ctxProp = null; + fixture.detectChanges(); + expect(getDOM().getStyle(fixture.debugElement.children[0].nativeElement, 'height')) + .toEqual(''); + }); + + it('should consume binding to property names where attr name and property name do not match', + () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + fixture.detectChanges(); + expect(fixture.debugElement.children[0].nativeElement.tabIndex).toEqual(0); + + fixture.debugElement.componentInstance.ctxNumProp = 5; + fixture.detectChanges(); + expect(fixture.debugElement.children[0].nativeElement.tabIndex).toEqual(5); + }); + + it('should consume binding to camel-cased properties', () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = ''; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + fixture.detectChanges(); + expect(fixture.debugElement.children[0].nativeElement.readOnly).toBeFalsy(); + + fixture.debugElement.componentInstance.ctxBoolProp = true; + fixture.detectChanges(); + expect(fixture.debugElement.children[0].nativeElement.readOnly).toBeTruthy(); + }); + + it('should consume binding to innerHtml', () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + fixture.debugElement.componentInstance.ctxProp = 'Some HTML'; + fixture.detectChanges(); + expect(getDOM().getInnerHTML(fixture.debugElement.children[0].nativeElement)) + .toEqual('Some HTML'); + + fixture.debugElement.componentInstance.ctxProp = 'Some other
HTML
'; + fixture.detectChanges(); + expect(getDOM().getInnerHTML(fixture.debugElement.children[0].nativeElement)) + .toEqual('Some other
HTML
'); + }); + + it('should consume binding to className using class alias', () => { + TestBed.configureTestingModule({declarations: [MyComp]}); + const template = '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + var nativeEl = fixture.debugElement.children[0].nativeElement; + fixture.debugElement.componentInstance.ctxProp = 'foo bar'; + fixture.detectChanges(); + + expect(nativeEl).toHaveCssClass('foo'); + expect(nativeEl).toHaveCssClass('bar'); + expect(nativeEl).not.toHaveCssClass('initial'); + }); + + it('should consume directive watch expression change.', () => { + TestBed.configureTestingModule({declarations: [MyComp, MyDir]}); + const template = '' + + '
' + + '
' + + '
' + + '
' + + '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + fixture.debugElement.componentInstance.ctxProp = 'Hello World!'; + fixture.detectChanges(); + + var containerSpan = fixture.debugElement.children[0]; + + expect(containerSpan.children[0].injector.get(MyDir).dirProp).toEqual('Hello World!'); + expect(containerSpan.children[1].injector.get(MyDir).dirProp).toEqual('Hi there!'); + expect(containerSpan.children[2].injector.get(MyDir).dirProp).toEqual('Hi there!'); + expect(containerSpan.children[3].injector.get(MyDir).dirProp) + .toEqual('One more Hello World!'); + }); + + describe('pipes', () => { + it('should support pipes in bindings', () => { + TestBed.configureTestingModule({declarations: [MyComp, MyDir, DoublePipe]}); + const template = '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + fixture.debugElement.componentInstance.ctxProp = 'a'; + fixture.detectChanges(); + + var dir = fixture.debugElement.children[0].references['dir']; + expect(dir.dirProp).toEqual('aa'); + }); + }); + + it('should support nested components.', () => { + TestBed.configureTestingModule({declarations: [MyComp, ChildComp]}); + const template = ''; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + + fixture.detectChanges(); + + expect(fixture.debugElement.nativeElement).toHaveText('hello'); + }); // GH issue 328 - https://github.com/angular/angular/issues/328 - it('should support different directive types on a single node', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView(MyComp, new ViewMetadata({ - template: '', - directives: [MyDir, ChildComp] - })) + it('should support different directive types on a single node', () => { + TestBed.configureTestingModule({declarations: [MyComp, ChildComp, MyDir]}); + const template = ''; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); - .createAsync(MyComp) - .then((fixture) => { + fixture.debugElement.componentInstance.ctxProp = 'Hello World!'; + fixture.detectChanges(); - fixture.debugElement.componentInstance.ctxProp = 'Hello World!'; - fixture.detectChanges(); + var tc = fixture.debugElement.children[0]; - var tc = fixture.debugElement.children[0]; + expect(tc.injector.get(MyDir).dirProp).toEqual('Hello World!'); + expect(tc.injector.get(ChildComp).dirProp).toEqual(null); + }); - expect(tc.injector.get(MyDir).dirProp).toEqual('Hello World!'); - expect(tc.injector.get(ChildComp).dirProp).toEqual(null); + it('should support directives where a binding attribute is not given', () => { + TestBed.configureTestingModule({declarations: [MyComp, MyDir]}); + const template = '

'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + }); - async.done(); - }); - })); + it('should execute a given directive once, even if specified multiple times', () => { + TestBed.configureTestingModule( + {declarations: [MyComp, DuplicateDir, DuplicateDir, [DuplicateDir, [DuplicateDir]]]}); + const template = '

'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + expect(fixture.debugElement.nativeElement).toHaveText('noduplicate'); + }); - it('should support directives where a binding attribute is not given', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView(MyComp, new ViewMetadata({ - // No attribute "el-prop" specified. - template: '

', - directives: [MyDir] - })) + it('should support directives where a selector matches property binding', () => { + TestBed.configureTestingModule({declarations: [MyComp, IdDir]}); + const template = '

'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); - .createAsync(MyComp) - .then((fixture) => { async.done(); }); - })); + var tc = fixture.debugElement.children[0]; + var idDir = tc.injector.get(IdDir); - it('should execute a given directive once, even if specified multiple times', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, new ViewMetadata({ - template: '

', - directives: [DuplicateDir, DuplicateDir, [DuplicateDir, [DuplicateDir]]] - })) - .createAsync(MyComp) - .then((fixture) => { - expect(fixture.debugElement.nativeElement).toHaveText('noduplicate'); - async.done(); - }); - })); + fixture.debugElement.componentInstance.ctxProp = 'some_id'; + fixture.detectChanges(); + expect(idDir.id).toEqual('some_id'); - it('should support directives where a selector matches property binding', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideView( - MyComp, - new ViewMetadata({template: '

', directives: [IdDir]})) + fixture.debugElement.componentInstance.ctxProp = 'other_id'; + fixture.detectChanges(); + expect(idDir.id).toEqual('other_id'); + }); - .createAsync(MyComp) - .then((fixture) => { - var tc = fixture.debugElement.children[0]; - var idDir = tc.injector.get(IdDir); + it('should support directives where a selector matches event binding', () => { + TestBed.configureTestingModule({declarations: [MyComp, EventDir]}); + const template = '

'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); - fixture.debugElement.componentInstance.ctxProp = 'some_id'; - fixture.detectChanges(); - expect(idDir.id).toEqual('some_id'); + var tc = fixture.debugElement.children[0]; + expect(tc.injector.get(EventDir)).not.toBe(null); + }); - fixture.debugElement.componentInstance.ctxProp = 'other_id'; - fixture.detectChanges(); - expect(idDir.id).toEqual('other_id'); + it('should read directives metadata from their binding token', () => { + TestBed.configureTestingModule({declarations: [MyComp, PrivateImpl, NeedsPublicApi]}); + const template = '
'; + TestBed.overrideComponent(MyComp, {set: {template}}); + const fixture = TestBed.createComponent(MyComp); + }); - async.done(); - }); - })); + it('should support template directives via `