From 231ed695075b494d892c16e8c98765955b9d8868 Mon Sep 17 00:00:00 2001 From: mgiambalvo Date: Mon, 15 Aug 2016 13:52:57 -0700 Subject: [PATCH] refactor(common): Remove uses of deprecated TestComponentBuilder. (#10754) * ng_class_spec * Working through ng_for_spec. * Finishing up ng_for_spec. * Finish the rest of the specs. * Convert pipes tests. --- .../common/test/directives/ng_class_spec.ts | 714 ++++++---------- .../common/test/directives/ng_for_spec.ts | 799 +++++++----------- .../common/test/directives/ng_if_spec.ts | 328 +++---- .../common/test/directives/ng_plural_spec.ts | 217 ++--- .../common/test/directives/ng_style_spec.ts | 257 +++--- .../common/test/directives/ng_switch_spec.ts | 245 +++--- .../directives/ng_template_outlet_spec.ts | 265 +++--- .../test/directives/non_bindable_spec.ts | 79 +- .../common/test/pipes/json_pipe_spec.ts | 36 +- .../common/test/pipes/slice_pipe_spec.ts | 34 +- 10 files changed, 1186 insertions(+), 1788 deletions(-) diff --git a/modules/@angular/common/test/directives/ng_class_spec.ts b/modules/@angular/common/test/directives/ng_class_spec.ts index 4d4d7237c0..4861d3de5b 100644 --- a/modules/@angular/common/test/directives/ng_class_spec.ts +++ b/modules/@angular/common/test/directives/ng_class_spec.ts @@ -8,8 +8,9 @@ import {NgClass, NgFor} from '@angular/common'; import {Component} from '@angular/core'; -import {ComponentFixture, TestComponentBuilder} from '@angular/core/testing'; -import {AsyncTestCompleter, beforeEach, beforeEachProviders, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal'; +import {ComponentFixture, TestBed, async} from '@angular/core/testing'; +import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal'; + import {ListWrapper, StringMapWrapper} from '../../src/facade/collection'; function detectChangesAndCheck(fixture: ComponentFixture, classes: string) { @@ -19,567 +20,360 @@ function detectChangesAndCheck(fixture: ComponentFixture, classes: string) export function main() { describe('binding to CSS class list', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + declarations: [TestComponent], + }); + }); - it('should clean up when the directive is destroyed', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture: ComponentFixture) => { - fixture.debugElement.componentInstance.items = [['0']]; - fixture.detectChanges(); - fixture.debugElement.componentInstance.items = [['1']]; + it('should clean up when the directive is destroyed', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - detectChangesAndCheck(fixture, '1'); - - async.done(); - }); - })); + fixture.debugElement.componentInstance.items = [['0']]; + fixture.detectChanges(); + fixture.debugElement.componentInstance.items = [['1']]; + detectChangesAndCheck(fixture, '1'); + })); describe('expressions evaluating to objects', () => { - it('should add classes specified in an object literal', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + it('should add classes specified in an object literal', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); - async.done(); - }); - })); + detectChangesAndCheck(fixture, 'foo'); + })); it('should add classes specified in an object literal without change in class names', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = `
`; + async(() => { + let template = `
`; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo-bar fooBar'); - async.done(); - }); - })); + detectChangesAndCheck(fixture, 'foo-bar fooBar'); + })); - it('should add and remove classes based on changes in object literal values', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + it('should add and remove classes based on changes in object literal values', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); + detectChangesAndCheck(fixture, 'foo'); - fixture.debugElement.componentInstance.condition = false; - detectChangesAndCheck(fixture, 'bar'); + fixture.debugElement.componentInstance.condition = false; + detectChangesAndCheck(fixture, 'bar'); + })); - async.done(); - }); - })); + it('should add and remove classes based on changes to the expression object', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should add and remove classes based on changes to the expression object', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + detectChangesAndCheck(fixture, 'foo'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'bar', true); + detectChangesAndCheck(fixture, 'foo bar'); - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'bar', true); - detectChangesAndCheck(fixture, 'foo bar'); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'baz', true); + detectChangesAndCheck(fixture, 'foo bar baz'); - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'baz', true); - detectChangesAndCheck(fixture, 'foo bar baz'); - - StringMapWrapper.delete(fixture.debugElement.componentInstance.objExpr, 'bar'); - detectChangesAndCheck(fixture, 'foo baz'); - - async.done(); - }); - })); + StringMapWrapper.delete(fixture.debugElement.componentInstance.objExpr, 'bar'); + detectChangesAndCheck(fixture, 'foo baz'); + })); it('should add and remove classes based on reference changes to the expression object', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); + detectChangesAndCheck(fixture, 'foo'); - fixture.debugElement.componentInstance.objExpr = {foo: true, bar: true}; - detectChangesAndCheck(fixture, 'foo bar'); + fixture.debugElement.componentInstance.objExpr = {foo: true, bar: true}; + detectChangesAndCheck(fixture, 'foo bar'); - fixture.debugElement.componentInstance.objExpr = {baz: true}; - detectChangesAndCheck(fixture, 'baz'); + fixture.debugElement.componentInstance.objExpr = {baz: true}; + detectChangesAndCheck(fixture, 'baz'); + })); - async.done(); - }); - })); + it('should remove active classes when expression evaluates to null', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should remove active classes when expression evaluates to null', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + detectChangesAndCheck(fixture, 'foo'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); + fixture.debugElement.componentInstance.objExpr = null; + detectChangesAndCheck(fixture, ''); - fixture.debugElement.componentInstance.objExpr = null; - detectChangesAndCheck(fixture, ''); - - fixture.debugElement.componentInstance.objExpr = {'foo': false, 'bar': true}; - detectChangesAndCheck(fixture, 'bar'); - - async.done(); - }); - })); + fixture.debugElement.componentInstance.objExpr = {'foo': false, 'bar': true}; + detectChangesAndCheck(fixture, 'bar'); + })); - it('should allow multiple classes per expression', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + it('should allow multiple classes per expression', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { + fixture.debugElement.componentInstance.objExpr = {'bar baz': true, 'bar1 baz1': true}; + detectChangesAndCheck(fixture, 'bar baz bar1 baz1'); - fixture.debugElement.componentInstance.objExpr = { - 'bar baz': true, - 'bar1 baz1': true - }; - detectChangesAndCheck(fixture, 'bar baz bar1 baz1'); + fixture.debugElement.componentInstance.objExpr = {'bar baz': false, 'bar1 baz1': true}; + detectChangesAndCheck(fixture, 'bar1 baz1'); + })); - fixture.debugElement.componentInstance.objExpr = { - 'bar baz': false, - 'bar1 baz1': true - }; - detectChangesAndCheck(fixture, 'bar1 baz1'); + it('should split by one or more spaces between classes', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - - async.done(); - }); - })); - - it('should split by one or more spaces between classes', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; - - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - - fixture.debugElement.componentInstance.objExpr = {'foo bar baz': true}; - detectChangesAndCheck(fixture, 'foo bar baz'); - - async.done(); - }); - })); + fixture.debugElement.componentInstance.objExpr = {'foo bar baz': true}; + detectChangesAndCheck(fixture, 'foo bar baz'); + })); }); describe('expressions evaluating to lists', () => { - it('should add classes specified in a list literal', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = `
`; + it('should add classes specified in a list literal', async(() => { + let template = `
`; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo bar foo-bar fooBar'); - async.done(); - }); - })); + detectChangesAndCheck(fixture, 'foo bar foo-bar fooBar'); + })); - it('should add and remove classes based on changes to the expression', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + it('should add and remove classes based on changes to the expression', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + var arrExpr: string[] = fixture.debugElement.componentInstance.arrExpr; + detectChangesAndCheck(fixture, 'foo'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - var arrExpr: string[] = fixture.debugElement.componentInstance.arrExpr; - detectChangesAndCheck(fixture, 'foo'); + arrExpr.push('bar'); + detectChangesAndCheck(fixture, 'foo bar'); - arrExpr.push('bar'); - detectChangesAndCheck(fixture, 'foo bar'); + arrExpr[1] = 'baz'; + detectChangesAndCheck(fixture, 'foo baz'); - arrExpr[1] = 'baz'; - detectChangesAndCheck(fixture, 'foo baz'); + ListWrapper.remove(fixture.debugElement.componentInstance.arrExpr, 'baz'); + detectChangesAndCheck(fixture, 'foo'); + })); - ListWrapper.remove(fixture.debugElement.componentInstance.arrExpr, 'baz'); - detectChangesAndCheck(fixture, 'foo'); + it('should add and remove classes when a reference changes', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + detectChangesAndCheck(fixture, 'foo'); - async.done(); - }); - })); + fixture.debugElement.componentInstance.arrExpr = ['bar']; + detectChangesAndCheck(fixture, 'bar'); + })); - it('should add and remove classes when a reference changes', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + it('should take initial classes into account when a reference changes', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + detectChangesAndCheck(fixture, 'foo'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); + fixture.debugElement.componentInstance.arrExpr = ['bar']; + detectChangesAndCheck(fixture, 'foo bar'); + })); - fixture.debugElement.componentInstance.arrExpr = ['bar']; - detectChangesAndCheck(fixture, 'bar'); + it('should ignore empty or blank class names', async(() => { + let template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.debugElement.componentInstance.arrExpr = ['', ' ']; + detectChangesAndCheck(fixture, 'foo'); + })); - async.done(); - }); - })); + it('should trim blanks from class names', async(() => { + var template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should take initial classes into account when a reference changes', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; - - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); - - fixture.debugElement.componentInstance.arrExpr = ['bar']; - detectChangesAndCheck(fixture, 'foo bar'); - - async.done(); - }); - })); - - it('should ignore empty or blank class names', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; - - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - - fixture.debugElement.componentInstance.arrExpr = ['', ' ']; - detectChangesAndCheck(fixture, 'foo'); - - async.done(); - }); - })); - - it('should trim blanks from class names', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; - - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - - fixture.debugElement.componentInstance.arrExpr = [' bar ']; - detectChangesAndCheck(fixture, 'foo bar'); - - async.done(); - }); - })); + fixture.debugElement.componentInstance.arrExpr = [' bar ']; + detectChangesAndCheck(fixture, 'foo bar'); + })); - it('should allow multiple classes per item in arrays', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + it('should allow multiple classes per item in arrays', async(() => { + var template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { + fixture.debugElement.componentInstance.arrExpr = ['foo bar baz', 'foo1 bar1 baz1']; + detectChangesAndCheck(fixture, 'foo bar baz foo1 bar1 baz1'); - fixture.debugElement.componentInstance.arrExpr = - ['foo bar baz', 'foo1 bar1 baz1']; - detectChangesAndCheck(fixture, 'foo bar baz foo1 bar1 baz1'); - - fixture.debugElement.componentInstance.arrExpr = ['foo bar baz foobar']; - detectChangesAndCheck(fixture, 'foo bar baz foobar'); - - async.done(); - }); - })); + fixture.debugElement.componentInstance.arrExpr = ['foo bar baz foobar']; + detectChangesAndCheck(fixture, 'foo bar baz foobar'); + })); }); describe('expressions evaluating to sets', () => { - it('should add and remove classes if the set instance changed', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + it('should add and remove classes if the set instance changed', async(() => { + var template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + var setExpr = new Set(); + setExpr.add('bar'); + fixture.debugElement.componentInstance.setExpr = setExpr; + detectChangesAndCheck(fixture, 'bar'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - var setExpr = new Set(); - setExpr.add('bar'); - fixture.debugElement.componentInstance.setExpr = setExpr; - detectChangesAndCheck(fixture, 'bar'); - - setExpr = new Set(); - setExpr.add('baz'); - fixture.debugElement.componentInstance.setExpr = setExpr; - detectChangesAndCheck(fixture, 'baz'); - - async.done(); - }); - })); + setExpr = new Set(); + setExpr.add('baz'); + fixture.debugElement.componentInstance.setExpr = setExpr; + detectChangesAndCheck(fixture, 'baz'); + })); }); + describe('expressions evaluating to string', () => { - it('should add classes specified in a string literal', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = `
`; + it('should add classes specified in a string literal', async(() => { + var template = `
`; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + detectChangesAndCheck(fixture, 'foo bar foo-bar fooBar'); + })); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo bar foo-bar fooBar'); - async.done(); - }); - })); + it('should add and remove classes based on changes to the expression', async(() => { + var template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + detectChangesAndCheck(fixture, 'foo'); - it('should add and remove classes based on changes to the expression', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; - - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); - - fixture.debugElement.componentInstance.strExpr = 'foo bar'; - detectChangesAndCheck(fixture, 'foo bar'); + fixture.debugElement.componentInstance.strExpr = 'foo bar'; + detectChangesAndCheck(fixture, 'foo bar'); - fixture.debugElement.componentInstance.strExpr = 'baz'; - detectChangesAndCheck(fixture, 'baz'); + fixture.debugElement.componentInstance.strExpr = 'baz'; + detectChangesAndCheck(fixture, 'baz'); - async.done(); - }); - })); + })); - it('should remove active classes when switching from string to null', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = `
`; + it('should remove active classes when switching from string to null', async(() => { + var template = `
`; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + detectChangesAndCheck(fixture, 'foo'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); + fixture.debugElement.componentInstance.strExpr = null; + detectChangesAndCheck(fixture, ''); - fixture.debugElement.componentInstance.strExpr = null; - detectChangesAndCheck(fixture, ''); - - async.done(); - }); - })); + })); it('should take initial classes into account when switching from string to null', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = `
`; + async(() => { + var template = `
`; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + detectChangesAndCheck(fixture, 'foo'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'foo'); + fixture.debugElement.componentInstance.strExpr = null; + detectChangesAndCheck(fixture, 'foo'); - fixture.debugElement.componentInstance.strExpr = null; - detectChangesAndCheck(fixture, 'foo'); + })); - async.done(); - }); - })); + it('should ignore empty and blank strings', async(() => { + var template = `
`; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.debugElement.componentInstance.strExpr = ''; + detectChangesAndCheck(fixture, 'foo'); - it('should ignore empty and blank strings', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = `
`; - - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - fixture.debugElement.componentInstance.strExpr = ''; - detectChangesAndCheck(fixture, 'foo'); - - async.done(); - }); - })); + })); }); describe('cooperation with other class-changing constructs', () => { - it('should co-operate with the class attribute', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + it('should co-operate with the class attribute', async(() => { + var template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'bar', true); + detectChangesAndCheck(fixture, 'init foo bar'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'bar', true); - detectChangesAndCheck(fixture, 'init foo bar'); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'foo', false); + detectChangesAndCheck(fixture, 'init bar'); - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'foo', false); - detectChangesAndCheck(fixture, 'init bar'); + fixture.debugElement.componentInstance.objExpr = null; + detectChangesAndCheck(fixture, 'init foo'); - fixture.debugElement.componentInstance.objExpr = null; - detectChangesAndCheck(fixture, 'init foo'); + })); - async.done(); - }); - })); + it('should co-operate with the interpolated class attribute', async(() => { + var template = `
`; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'bar', true); + detectChangesAndCheck(fixture, `init foo bar`); - it('should co-operate with the interpolated class attribute', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = `
`; + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'foo', false); + detectChangesAndCheck(fixture, `init bar`); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'bar', true); - detectChangesAndCheck(fixture, `init foo bar`); + fixture.debugElement.componentInstance.objExpr = null; + detectChangesAndCheck(fixture, `init foo`); - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'foo', false); - detectChangesAndCheck(fixture, `init bar`); + })); - fixture.debugElement.componentInstance.objExpr = null; - detectChangesAndCheck(fixture, `init foo`); + it('should co-operate with the class attribute and binding to it', async(() => { + var template = `
`; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'bar', true); + detectChangesAndCheck(fixture, `init foo bar`); - async.done(); - }); - })); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'foo', false); + detectChangesAndCheck(fixture, `init bar`); - it('should co-operate with the class attribute and binding to it', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = `
`; + fixture.debugElement.componentInstance.objExpr = null; + detectChangesAndCheck(fixture, `init foo`); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'bar', true); - detectChangesAndCheck(fixture, `init foo bar`); + })); - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'foo', false); - detectChangesAndCheck(fixture, `init bar`); + it('should co-operate with the class attribute and class.name binding', async(() => { + var template = + '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + detectChangesAndCheck(fixture, 'init foo baz'); - fixture.debugElement.componentInstance.objExpr = null; - detectChangesAndCheck(fixture, `init foo`); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'bar', true); + detectChangesAndCheck(fixture, 'init foo baz bar'); - async.done(); - }); - })); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'foo', false); + detectChangesAndCheck(fixture, 'init baz bar'); - it('should co-operate with the class attribute and class.name binding', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = - '
'; + fixture.debugElement.componentInstance.condition = false; + detectChangesAndCheck(fixture, 'init bar'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'init foo baz'); - - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'bar', true); - detectChangesAndCheck(fixture, 'init foo baz bar'); - - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'foo', false); - detectChangesAndCheck(fixture, 'init baz bar'); - - fixture.debugElement.componentInstance.condition = false; - detectChangesAndCheck(fixture, 'init bar'); - - async.done(); - }); - })); + })); it('should co-operate with initial class and class attribute binding when binding changes', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - var template = '
'; + async(() => { + var template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + detectChangesAndCheck(fixture, 'init foo'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - detectChangesAndCheck(fixture, 'init foo'); + StringMapWrapper.set(fixture.debugElement.componentInstance.objExpr, 'bar', true); + detectChangesAndCheck(fixture, 'init foo bar'); - StringMapWrapper.set( - fixture.debugElement.componentInstance.objExpr, 'bar', true); - detectChangesAndCheck(fixture, 'init foo bar'); - - fixture.debugElement.componentInstance.strExpr = 'baz'; - detectChangesAndCheck(fixture, 'init bar baz foo'); - - fixture.debugElement.componentInstance.objExpr = null; - detectChangesAndCheck(fixture, 'init baz'); - - async.done(); - }); - })); + fixture.debugElement.componentInstance.strExpr = 'baz'; + detectChangesAndCheck(fixture, 'init bar baz foo'); + fixture.debugElement.componentInstance.objExpr = null; + detectChangesAndCheck(fixture, 'init baz'); + })); }); }); } diff --git a/modules/@angular/common/test/directives/ng_for_spec.ts b/modules/@angular/common/test/directives/ng_for_spec.ts index 283979ecf9..7035ea19fb 100644 --- a/modules/@angular/common/test/directives/ng_for_spec.ts +++ b/modules/@angular/common/test/directives/ng_for_spec.ts @@ -8,8 +8,8 @@ import {NgFor, NgIf} from '@angular/common'; import {Component, ContentChild, TemplateRef} from '@angular/core'; -import {TestComponentBuilder} from '@angular/core/testing'; -import {AsyncTestCompleter, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xit} from '@angular/core/testing/testing_internal'; +import {TestBed, async} from '@angular/core/testing'; +import {ddescribe, describe, iit, inject, it, xit} from '@angular/core/testing/testing_internal'; import {By} from '@angular/platform-browser/src/dom/debug/by'; import {expect} from '@angular/platform-browser/testing/matchers'; @@ -22,545 +22,396 @@ export function main() { const TEMPLATE = '
{{item.toString()}};
'; - it('should reflect initial elements', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, TEMPLATE) - .createAsync(TestComponent) - .then((fixture) => { - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('1;2;'); - async.done(); - }); - })); + beforeEach(() => { + TestBed.configureTestingModule({ + declarations: [TestComponent, ComponentUsingTestComponent], + }); + }); - it('should reflect added elements', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, TEMPLATE) - .createAsync(TestComponent) - .then((fixture) => { - fixture.detectChanges(); + it('should reflect initial elements', async(() => { + TestBed.overrideComponent(TestComponent, {set: {template: TEMPLATE}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('1;2;'); + })); - (fixture.debugElement.componentInstance.items).push(3); - fixture.detectChanges(); + it('should reflect added elements', async(() => { + TestBed.overrideComponent(TestComponent, {set: {template: TEMPLATE}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('1;2;3;'); - async.done(); - }); - })); + (fixture.debugElement.componentInstance.items).push(3); + fixture.detectChanges(); - it('should reflect removed elements', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, TEMPLATE) - .createAsync(TestComponent) - .then((fixture) => { - fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('1;2;3;'); + })); - ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 1); - fixture.detectChanges(); + it('should reflect removed elements', async(() => { + TestBed.overrideComponent(TestComponent, {set: {template: TEMPLATE}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('1;'); - async.done(); - }); - })); + ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 1); + fixture.detectChanges(); - it('should reflect moved elements', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, TEMPLATE) - .createAsync(TestComponent) - .then((fixture) => { - fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('1;'); + })); - ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 0); - (fixture.debugElement.componentInstance.items).push(1); - fixture.detectChanges(); + it('should reflect moved elements', async(() => { + TestBed.overrideComponent(TestComponent, {set: {template: TEMPLATE}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('2;1;'); - async.done(); - }); - })); + ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 0); + (fixture.debugElement.componentInstance.items).push(1); + fixture.detectChanges(); - it('should reflect a mix of all changes (additions/removals/moves)', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, TEMPLATE) - .createAsync(TestComponent) - .then((fixture) => { - fixture.debugElement.componentInstance.items = [0, 1, 2, 3, 4, 5]; - fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('2;1;'); + })); - fixture.debugElement.componentInstance.items = [6, 2, 7, 0, 4, 8]; - fixture.detectChanges(); + it('should reflect a mix of all changes (additions/removals/moves)', async(() => { + TestBed.overrideComponent(TestComponent, {set: {template: TEMPLATE}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.debugElement.componentInstance.items = [0, 1, 2, 3, 4, 5]; + fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('6;2;7;0;4;8;'); - async.done(); - }); - })); + fixture.debugElement.componentInstance.items = [6, 2, 7, 0, 4, 8]; + fixture.detectChanges(); - it('should iterate over an array of objects', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = - '
  • {{item["name"]}};
'; + expect(fixture.debugElement.nativeElement).toHaveText('6;2;7;0;4;8;'); + })); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { + it('should iterate over an array of objects', async(() => { + const template = '
  • {{item["name"]}};
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - // INIT - fixture.debugElement.componentInstance.items = - [{'name': 'misko'}, {'name': 'shyam'}]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('misko;shyam;'); + // INIT + fixture.debugElement.componentInstance.items = [{'name': 'misko'}, {'name': 'shyam'}]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('misko;shyam;'); - // GROW - (fixture.debugElement.componentInstance.items).push({'name': 'adam'}); - fixture.detectChanges(); + // GROW + (fixture.debugElement.componentInstance.items).push({'name': 'adam'}); + fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('misko;shyam;adam;'); + expect(fixture.debugElement.nativeElement).toHaveText('misko;shyam;adam;'); - // SHRINK - ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 2); - ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 0); - fixture.detectChanges(); + // SHRINK + ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 2); + ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 0); + fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('shyam;'); - async.done(); - }); - })); + expect(fixture.debugElement.nativeElement).toHaveText('shyam;'); + })); - it('should gracefully handle nulls', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = '
  • {{item}};
'; - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText(''); - async.done(); - }); - })); + it('should gracefully handle nulls', async(() => { + const template = '
  • {{item}};
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText(''); + })); - it('should gracefully handle ref changing to null and back', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, TEMPLATE) - .createAsync(TestComponent) - .then((fixture) => { - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('1;2;'); + it('should gracefully handle ref changing to null and back', async(() => { + TestBed.overrideComponent(TestComponent, {set: {template: TEMPLATE}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('1;2;'); - fixture.debugElement.componentInstance.items = null; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText(''); + fixture.debugElement.componentInstance.items = null; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText(''); - fixture.debugElement.componentInstance.items = [1, 2, 3]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('1;2;3;'); - async.done(); - }); - })); + fixture.debugElement.componentInstance.items = [1, 2, 3]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('1;2;3;'); + })); - it('should throw on non-iterable ref and suggest using an array', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, TEMPLATE).createAsync(TestComponent).then((fixture) => { - fixture.debugElement.componentInstance.items = 'whaaa'; - expect(() => fixture.detectChanges()) - .toThrowError( - /Cannot find a differ supporting object 'whaaa' of type 'string'. NgFor only supports binding to Iterables such as Arrays/); - async.done(); - }); - })); + it('should throw on non-iterable ref and suggest using an array', async(() => { + TestBed.overrideComponent(TestComponent, {set: {template: TEMPLATE}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.debugElement.componentInstance.items = 'whaaa'; + expect(() => fixture.detectChanges()) + .toThrowError( + /Cannot find a differ supporting object 'whaaa' of type 'string'. NgFor only supports binding to Iterables such as Arrays/); + })); - it('should throw on ref changing to string', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, TEMPLATE) - .createAsync(TestComponent) - .then((fixture) => { - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('1;2;'); + it('should throw on ref changing to string', async(() => { + TestBed.overrideComponent(TestComponent, {set: {template: TEMPLATE}}); + let fixture = TestBed.createComponent(TestComponent); + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('1;2;'); - fixture.debugElement.componentInstance.items = 'whaaa'; - expect(() => fixture.detectChanges()).toThrowError(); - async.done(); - }); - })); + fixture.debugElement.componentInstance.items = 'whaaa'; + expect(() => fixture.detectChanges()).toThrowError(); + })); - it('should works with duplicates', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, TEMPLATE) - .createAsync(TestComponent) - .then((fixture) => { - var a = new Foo(); - fixture.debugElement.componentInstance.items = [a, a]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('foo;foo;'); - async.done(); - }); - })); + it('should works with duplicates', async(() => { + TestBed.overrideComponent(TestComponent, {set: {template: TEMPLATE}}); + let fixture = TestBed.createComponent(TestComponent); + var a = new Foo(); + fixture.debugElement.componentInstance.items = [a, a]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('foo;foo;'); + })); - it('should repeat over nested arrays', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = '
' + - '
' + - '
' + - '{{subitem}}-{{item.length}};' + - '
|' + - '
' + - '
'; + it('should repeat over nested arrays', async(() => { + const template = '
' + + '
' + + '
' + + '{{subitem}}-{{item.length}};' + + '
|' + + '
' + + '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - fixture.debugElement.componentInstance.items = [['a', 'b'], ['c']]; - fixture.detectChanges(); - fixture.detectChanges(); - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('a-2;b-2;|c-1;|'); + fixture.debugElement.componentInstance.items = [['a', 'b'], ['c']]; + fixture.detectChanges(); + fixture.detectChanges(); + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('a-2;b-2;|c-1;|'); - fixture.debugElement.componentInstance.items = [['e'], ['f', 'g']]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('e-1;|f-2;g-2;|'); + fixture.debugElement.componentInstance.items = [['e'], ['f', 'g']]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('e-1;|f-2;g-2;|'); + })); - async.done(); - }); - })); + it('should repeat over nested arrays with no intermediate element', async(() => { + const template = '
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should repeat over nested arrays with no intermediate element', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = '
'; + fixture.debugElement.componentInstance.items = [['a', 'b'], ['c']]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('a-2;b-2;c-1;'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - fixture.debugElement.componentInstance.items = [['a', 'b'], ['c']]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('a-2;b-2;c-1;'); + fixture.debugElement.componentInstance.items = [['e'], ['f', 'g']]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('e-1;f-2;g-2;'); + })); - fixture.debugElement.componentInstance.items = [['e'], ['f', 'g']]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('e-1;f-2;g-2;'); - async.done(); - }); - })); + it('should repeat over nested ngIf that are the last node in the ngFor temlate', async(() => { + const template = + `
`; - it('should repeat over nested ngIf that are the last node in the ngFor temlate', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = - `
`; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - const el = fixture.debugElement.nativeElement; - const items = [1]; - fixture.debugElement.componentInstance.items = items; - fixture.detectChanges(); - expect(el).toHaveText('0|even|'); + const el = fixture.debugElement.nativeElement; + const items = [1]; + fixture.debugElement.componentInstance.items = items; + fixture.detectChanges(); + expect(el).toHaveText('0|even|'); - items.push(1); - fixture.detectChanges(); - expect(el).toHaveText('0|even|1|'); + items.push(1); + fixture.detectChanges(); + expect(el).toHaveText('0|even|1|'); - items.push(1); - fixture.detectChanges(); - expect(el).toHaveText('0|even|1|2|even|'); + items.push(1); + fixture.detectChanges(); + expect(el).toHaveText('0|even|1|2|even|'); + })); - async.done(); - }); - })); + it('should display indices correctly', async(() => { + const template = + '
{{i.toString()}}
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should display indices correctly', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = - '
{{i.toString()}}
'; + fixture.debugElement.componentInstance.items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('0123456789'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - fixture.debugElement.componentInstance.items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('0123456789'); + fixture.debugElement.componentInstance.items = [1, 2, 6, 7, 4, 3, 5, 8, 9, 0]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('0123456789'); + })); - fixture.debugElement.componentInstance.items = [1, 2, 6, 7, 4, 3, 5, 8, 9, 0]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('0123456789'); - async.done(); - }); - })); + it('should display first item correctly', async(() => { + const template = + '
{{isFirst.toString()}}
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should display first item correctly', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = - '
{{isFirst.toString()}}
'; + fixture.debugElement.componentInstance.items = [0, 1, 2]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('truefalsefalse'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - fixture.debugElement.componentInstance.items = [0, 1, 2]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('truefalsefalse'); + fixture.debugElement.componentInstance.items = [2, 1]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('truefalse'); + })); - fixture.debugElement.componentInstance.items = [2, 1]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('truefalse'); - async.done(); - }); - })); + it('should display last item correctly', async(() => { + const template = + '
{{isLast.toString()}}
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should display last item correctly', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = - '
{{isLast.toString()}}
'; + fixture.debugElement.componentInstance.items = [0, 1, 2]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('falsefalsetrue'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - fixture.debugElement.componentInstance.items = [0, 1, 2]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('falsefalsetrue'); + fixture.debugElement.componentInstance.items = [2, 1]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('falsetrue'); + })); - fixture.debugElement.componentInstance.items = [2, 1]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('falsetrue'); - async.done(); - }); - })); + it('should display even items correctly', async(() => { + const template = + '
{{isEven.toString()}}
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should display even items correctly', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = - '
{{isEven.toString()}}
'; + fixture.debugElement.componentInstance.items = [0, 1, 2]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('truefalsetrue'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - fixture.debugElement.componentInstance.items = [0, 1, 2]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('truefalsetrue'); + fixture.debugElement.componentInstance.items = [2, 1]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('truefalse'); + })); - fixture.debugElement.componentInstance.items = [2, 1]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('truefalse'); - async.done(); - }); - })); + it('should display odd items correctly', async(() => { + const template = + '
{{isOdd.toString()}}
'; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should display odd items correctly', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = - '
{{isOdd.toString()}}
'; + fixture.debugElement.componentInstance.items = [0, 1, 2, 3]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('falsetruefalsetrue'); - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - fixture.debugElement.componentInstance.items = [0, 1, 2, 3]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('falsetruefalsetrue'); + fixture.debugElement.componentInstance.items = [2, 1]; + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('falsetrue'); + })); - fixture.debugElement.componentInstance.items = [2, 1]; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement).toHaveText('falsetrue'); - async.done(); - }); - })); + it('should allow to use a custom template', async(() => { + const tcTemplate = + '
'; + TestBed.overrideComponent(TestComponent, {set: {template: tcTemplate}}); + const cutTemplate = + '
  • {{i}}: {{item}};
  • '; + TestBed.overrideComponent(ComponentUsingTestComponent, {set: {template: cutTemplate}}); + let fixture = TestBed.createComponent(ComponentUsingTestComponent); - it('should allow to use a custom template', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate( - TestComponent, - '
    ') - .overrideTemplate( - ComponentUsingTestComponent, - '
  • {{i}}: {{item}};
  • ') - .createAsync(ComponentUsingTestComponent) - .then((fixture) => { - const testComponent = fixture.debugElement.children[0]; - testComponent.componentInstance.items = ['a', 'b', 'c']; - fixture.detectChanges(); - expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;'); + const testComponent = fixture.debugElement.children[0]; + testComponent.componentInstance.items = ['a', 'b', 'c']; + fixture.detectChanges(); + expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;'); + })); - async.done(); - }); - })); + it('should use a default template if a custom one is null', async(() => { + const testTemplate = `
    `; + TestBed.overrideComponent(TestComponent, {set: {template: testTemplate}}); + const cutTemplate = + '
  • {{i}}: {{item}};
  • '; + TestBed.overrideComponent(ComponentUsingTestComponent, {set: {template: cutTemplate}}); + let fixture = TestBed.createComponent(ComponentUsingTestComponent); - it('should use a default template if a custom one is null', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, `
    `) - .overrideTemplate(ComponentUsingTestComponent, '') - .createAsync(ComponentUsingTestComponent) - .then((fixture) => { - const testComponent = fixture.debugElement.children[0]; - testComponent.componentInstance.items = ['a', 'b', 'c']; - fixture.detectChanges(); - expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;'); + const testComponent = fixture.debugElement.children[0]; + testComponent.componentInstance.items = ['a', 'b', 'c']; + fixture.detectChanges(); + expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;'); + })); - async.done(); - }); - })); + it('should use a custom template when both default and a custom one are present', async(() => { + const testTemplate = `
    `; + TestBed.overrideComponent(TestComponent, {set: {template: testTemplate}}); + const cutTemplate = + '
  • {{i}}: {{item}};
  • '; + TestBed.overrideComponent(ComponentUsingTestComponent, {set: {template: cutTemplate}}); + let fixture = TestBed.createComponent(ComponentUsingTestComponent); - it('should use a custom template when both default and a custom one are present', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - tcb.overrideTemplate(TestComponent, `
    `) - .overrideTemplate( - ComponentUsingTestComponent, - '
  • {{i}}: {{item}};
  • ') - .createAsync(ComponentUsingTestComponent) - .then((fixture) => { - const testComponent = fixture.debugElement.children[0]; - testComponent.componentInstance.items = ['a', 'b', 'c']; - fixture.detectChanges(); - expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;'); - - async.done(); - }); - })); + const testComponent = fixture.debugElement.children[0]; + testComponent.componentInstance.items = ['a', 'b', 'c']; + fixture.detectChanges(); + expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;'); + })); describe('track by', () => { - it('should set the context to the component instance', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = - ``; - tcb.overrideTemplate(TestComponent, template) - .createAsync(TestComponent) - .then((fixture) => { - thisArg = null; - fixture.detectChanges(); - expect(thisArg).toBe(fixture.debugElement.componentInstance); - async.done(); - }); - })); + it('should set the context to the component instance', async(() => { + const template = + ``; + TestBed.overrideComponent(TestComponent, {set: {template: template}}); + let fixture = TestBed.createComponent(TestComponent); - it('should not replace tracked items', - inject( - [TestComponentBuilder, AsyncTestCompleter], - (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { - const template = - `