' +
+ const template = '
' +
'' +
'you have no messages.' +
'you have one message.' +
'
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.debugElement.componentInstance.switchValue = 0;
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('you have no messages.');
+ fixture = createTestComponent(template);
- fixture.debugElement.componentInstance.switchValue = 1;
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('you have one message.');
+ getComponent().switchValue = 0;
+ detectChangesAndExpectText('you have no messages.');
+
+ getComponent().switchValue = 1;
+ detectChangesAndExpectText('you have one message.');
}));
it('should display the template according to the category', async(() => {
- var template = '
' +
+ const template = '
' +
'
' +
'- you have a few messages.
' +
'- you have many messages.
' +
'
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.debugElement.componentInstance.switchValue = 2;
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('you have a few messages.');
+ fixture = createTestComponent(template);
- fixture.debugElement.componentInstance.switchValue = 8;
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('you have many messages.');
+ getComponent().switchValue = 2;
+ detectChangesAndExpectText('you have a few messages.');
+
+ getComponent().switchValue = 8;
+ detectChangesAndExpectText('you have many messages.');
}));
it('should default to other when no matches are found', async(() => {
- var template = '
' +
+ const template = '
' +
'
' +
'- you have a few messages.
' +
'- default message.
' +
'
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.debugElement.componentInstance.switchValue = 100;
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('default message.');
+ fixture = createTestComponent(template);
+
+ getComponent().switchValue = 100;
+ detectChangesAndExpectText('default message.');
}));
it('should prioritize value matches over category matches', async(() => {
- var template = '
' +
+ const template = '
' +
'
' +
'- you have a few messages.
' +
'you have two messages.' +
'
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.debugElement.componentInstance.switchValue = 2;
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('you have two messages.');
+ fixture = createTestComponent(template);
- fixture.debugElement.componentInstance.switchValue = 3;
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('you have a few messages.');
+ getComponent().switchValue = 2;
+ detectChangesAndExpectText('you have two messages.');
+
+ getComponent().switchValue = 3;
+ detectChangesAndExpectText('you have a few messages.');
}));
});
}
@@ -131,6 +132,7 @@ class TestLocalization extends NgLocalization {
if (value > 1 && value < 4) {
return 'few';
}
+
if (value >= 4 && value < 10) {
return 'many';
}
@@ -143,3 +145,8 @@ class TestLocalization extends NgLocalization {
class TestComponent {
switchValue: number = null;
}
+
+function createTestComponent(template: string): ComponentFixture
{
+ return TestBed.overrideComponent(TestComponent, {set: {template: template}})
+ .createComponent(TestComponent);
+}
diff --git a/modules/@angular/common/test/directives/ng_style_spec.ts b/modules/@angular/common/test/directives/ng_style_spec.ts
index 375b60dd9f..3c1329acdc 100644
--- a/modules/@angular/common/test/directives/ng_style_spec.ts
+++ b/modules/@angular/common/test/directives/ng_style_spec.ts
@@ -10,69 +10,70 @@ import {CommonModule} from '@angular/common';
import {Component} from '@angular/core';
import {ComponentFixture, TestBed, async} from '@angular/core/testing';
-function expectNativeEl(fixture: ComponentFixture) {
- return expect(fixture.debugElement.children[0].nativeElement);
-}
-
export function main() {
- describe('binding to CSS styles', () => {
+ describe('NgStyle', () => {
+ let fixture: ComponentFixture;
+
+ function getComponent(): TestComponent { return fixture.componentInstance; }
+
+ function expectNativeEl(fixture: ComponentFixture): any {
+ return expect(fixture.debugElement.children[0].nativeElement);
+ }
+
+ afterEach(() => { fixture = null; });
beforeEach(() => {
TestBed.configureTestingModule({declarations: [TestComponent], imports: [CommonModule]});
});
it('should add styles specified in an object literal', async(() => {
- var template = ``;
+ const template = ``;
+ fixture = createTestComponent(template);
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'});
}));
it('should add and change styles specified in an object expression', async(() => {
- var template = ``;
+ const template = ``;
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- var expr: Map;
+ fixture = createTestComponent(template);
+ let expr: {[k: string]: string};
- fixture.debugElement.componentInstance.expr = {'max-width': '40px'};
+ getComponent().expr = {'max-width': '40px'};
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'});
- expr = fixture.debugElement.componentInstance.expr;
- (expr as any)['max-width'] = '30%';
+ expr = getComponent().expr;
+ expr['max-width'] = '30%';
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'max-width': '30%'});
}));
it('should add and remove styles specified using style.unit notation', async(() => {
- var template = ``;
+ const template = ``;
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
+ fixture = createTestComponent(template);
- fixture.debugElement.componentInstance.expr = '40';
+ getComponent().expr = '40';
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'});
- fixture.debugElement.componentInstance.expr = null;
+ getComponent().expr = null;
fixture.detectChanges();
expectNativeEl(fixture).not.toHaveCssStyle('max-width');
}));
it('should update styles using style.unit notation when unit changes', async(() => {
- var template = ``;
+ const template = ``;
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
+ fixture = createTestComponent(template);
- fixture.debugElement.componentInstance.expr = {'max-width.px': '40'};
+ getComponent().expr = {'max-width.px': '40'};
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'});
- fixture.debugElement.componentInstance.expr = {'max-width.em': '40'};
+ getComponent().expr = {'max-width.em': '40'};
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40em'});
}));
@@ -81,9 +82,9 @@ export function main() {
it('should change styles specified in an object expression', async(() => {
const template = ``;
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.debugElement.componentInstance.expr = {
+ fixture = createTestComponent(template);
+
+ getComponent().expr = {
// height, width order is important here
height: '10px',
width: '10px'
@@ -92,7 +93,7 @@ export function main() {
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'height': '10px', 'width': '10px'});
- fixture.debugElement.componentInstance.expr = {
+ getComponent().expr = {
// width, height order is important here
width: '5px',
height: '5px',
@@ -103,29 +104,29 @@ export function main() {
}));
it('should remove styles when deleting a key in an object expression', async(() => {
- var template = ``;
+ const template = ``;
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.debugElement.componentInstance.expr = {'max-width': '40px'};
+ fixture = createTestComponent(template);
+
+ getComponent().expr = {'max-width': '40px'};
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'});
- delete fixture.debugElement.componentInstance.expr['max-width'];
+ delete getComponent().expr['max-width'];
fixture.detectChanges();
expectNativeEl(fixture).not.toHaveCssStyle('max-width');
}));
it('should co-operate with the style attribute', async(() => {
- var template = ``;
+ const template = ``;
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.debugElement.componentInstance.expr = {'max-width': '40px'};
+ fixture = createTestComponent(template);
+
+ getComponent().expr = {'max-width': '40px'};
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px', 'font-size': '12px'});
- delete fixture.debugElement.componentInstance.expr['max-width'];
+ delete getComponent().expr['max-width'];
fixture.detectChanges();
expectNativeEl(fixture).not.toHaveCssStyle('max-width');
expectNativeEl(fixture).toHaveCssStyle({'font-size': '12px'});
@@ -133,15 +134,15 @@ export function main() {
it('should co-operate with the style.[styleName]="expr" special-case in the compiler',
async(() => {
- var template = ``;
+ const template = ``;
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.debugElement.componentInstance.expr = {'max-width': '40px'};
+ fixture = createTestComponent(template);
+
+ getComponent().expr = {'max-width': '40px'};
fixture.detectChanges();
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px', 'font-size': '12px'});
- delete fixture.debugElement.componentInstance.expr['max-width'];
+ delete getComponent().expr['max-width'];
fixture.detectChanges();
expectNativeEl(fixture).not.toHaveCssStyle('max-width');
expectNativeEl(fixture).toHaveCssStyle({'font-size': '12px'});
@@ -153,3 +154,8 @@ export function main() {
class TestComponent {
expr: any;
}
+
+function createTestComponent(template: string): ComponentFixture {
+ return TestBed.overrideComponent(TestComponent, {set: {template: template}})
+ .createComponent(TestComponent);
+}
diff --git a/modules/@angular/common/test/directives/ng_switch_spec.ts b/modules/@angular/common/test/directives/ng_switch_spec.ts
index 38b1455ede..978a367f16 100644
--- a/modules/@angular/common/test/directives/ng_switch_spec.ts
+++ b/modules/@angular/common/test/directives/ng_switch_spec.ts
@@ -8,83 +8,86 @@
import {CommonModule} from '@angular/common';
import {Component} from '@angular/core';
-import {TestBed, async} from '@angular/core/testing';
+import {ComponentFixture, TestBed, async} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/matchers';
export function main() {
- describe('switch', () => {
+ describe('NgSwitch', () => {
+ let fixture: ComponentFixture;
+
+ function getComponent(): TestComponent { return fixture.componentInstance; }
+
+ function detectChangesAndExpectText(text: string): void {
+ fixture.detectChanges();
+ expect(fixture.nativeElement).toHaveText(text);
+ }
+
+ afterEach(() => { fixture = null; });
beforeEach(() => {
- TestBed.configureTestingModule({declarations: [TestComponent], imports: [CommonModule]});
+ TestBed.configureTestingModule({
+ declarations: [TestComponent],
+ imports: [CommonModule],
+ });
});
describe('switch value changes', () => {
it('should switch amongst when values', async(() => {
- var template = '' +
+ const template = '
' +
'
' +
'- when a
' +
'- when b
' +
'
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('');
+ fixture = createTestComponent(template);
- fixture.debugElement.componentInstance.switchValue = 'a';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when a');
+ detectChangesAndExpectText('');
- fixture.debugElement.componentInstance.switchValue = 'b';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when b');
+ getComponent().switchValue = 'a';
+ detectChangesAndExpectText('when a');
+
+ getComponent().switchValue = 'b';
+ detectChangesAndExpectText('when b');
}));
// TODO(robwormald): deprecate and remove
it('should switch amongst when values using switchCase', async(() => {
- var template = '
' +
+ const template = '
' +
'
' +
'- when a
' +
'- when b
' +
'
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('');
+ fixture = createTestComponent(template);
- fixture.debugElement.componentInstance.switchValue = 'a';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when a');
+ detectChangesAndExpectText('');
- fixture.debugElement.componentInstance.switchValue = 'b';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when b');
+ getComponent().switchValue = 'a';
+ detectChangesAndExpectText('when a');
+
+ getComponent().switchValue = 'b';
+ detectChangesAndExpectText('when b');
}));
it('should switch amongst when values with fallback to default', async(() => {
- var template = '
' +
+ const template = '
' +
'
' +
'- when a
' +
'- when default
' +
'
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when default');
+ fixture = createTestComponent(template);
+ detectChangesAndExpectText('when default');
- fixture.debugElement.componentInstance.switchValue = 'a';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when a');
+ getComponent().switchValue = 'a';
+ detectChangesAndExpectText('when a');
- fixture.debugElement.componentInstance.switchValue = 'b';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when default');
+ getComponent().switchValue = 'b';
+ detectChangesAndExpectText('when default');
}));
it('should support multiple whens with the same value', async(() => {
- var template = '
' +
+ const template = '
' +
'
' +
'- when a1;
' +
'- when b1;
' +
@@ -94,53 +97,43 @@ export function main() {
'- when default2;
' +
'
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when default1;when default2;');
+ fixture = createTestComponent(template);
+ detectChangesAndExpectText('when default1;when default2;');
- fixture.debugElement.componentInstance.switchValue = 'a';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when a1;when a2;');
+ getComponent().switchValue = 'a';
+ detectChangesAndExpectText('when a1;when a2;');
- fixture.debugElement.componentInstance.switchValue = 'b';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when b1;when b2;');
+ getComponent().switchValue = 'b';
+ detectChangesAndExpectText('when b1;when b2;');
}));
});
describe('when values changes', () => {
it('should switch amongst when values', async(() => {
- var template = '
' +
+ const template = '
' +
'
' +
'- when 1;
' +
'- when 2;
' +
'- when default;
' +
'
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.debugElement.componentInstance.when1 = 'a';
- fixture.debugElement.componentInstance.when2 = 'b';
- fixture.debugElement.componentInstance.switchValue = 'a';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when 1;');
+ fixture = createTestComponent(template);
+ getComponent().when1 = 'a';
+ getComponent().when2 = 'b';
+ getComponent().switchValue = 'a';
+ detectChangesAndExpectText('when 1;');
- fixture.debugElement.componentInstance.switchValue = 'b';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when 2;');
+ getComponent().switchValue = 'b';
+ detectChangesAndExpectText('when 2;');
- fixture.debugElement.componentInstance.switchValue = 'c';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when default;');
+ getComponent().switchValue = 'c';
+ detectChangesAndExpectText('when default;');
- fixture.debugElement.componentInstance.when1 = 'c';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when 1;');
+ getComponent().when1 = 'c';
+ detectChangesAndExpectText('when 1;');
- fixture.debugElement.componentInstance.when1 = 'd';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('when default;');
+ getComponent().when1 = 'd';
+ detectChangesAndExpectText('when default;');
}));
});
});
@@ -148,13 +141,12 @@ export function main() {
@Component({selector: 'test-cmp', template: ''})
class TestComponent {
- switchValue: any;
- when1: any;
- when2: any;
-
- constructor() {
- this.switchValue = null;
- this.when1 = null;
- this.when2 = null;
- }
+ switchValue: any = null;
+ when1: any = null;
+ when2: any = null;
+}
+
+function createTestComponent(template: string): ComponentFixture
{
+ return TestBed.overrideComponent(TestComponent, {set: {template: template}})
+ .createComponent(TestComponent);
}
diff --git a/modules/@angular/common/test/directives/ng_template_outlet_spec.ts b/modules/@angular/common/test/directives/ng_template_outlet_spec.ts
index a5a8f17d25..ac1a17b816 100644
--- a/modules/@angular/common/test/directives/ng_template_outlet_spec.ts
+++ b/modules/@angular/common/test/directives/ng_template_outlet_spec.ts
@@ -8,155 +8,140 @@
import {CommonModule} from '@angular/common';
import {Component, ContentChildren, Directive, NO_ERRORS_SCHEMA, QueryList, TemplateRef} from '@angular/core';
-import {TestBed, async} from '@angular/core/testing';
+import {ComponentFixture, TestBed, async} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/matchers';
export function main() {
- describe('insert', () => {
+ describe('NgTemplateOutlet', () => {
+ let fixture: ComponentFixture;
+
+ function setTplRef(value: any): void { fixture.componentInstance.currentTplRef = value; }
+
+ function detectChangesAndExpectText(text: string): void {
+ fixture.detectChanges();
+ expect(fixture.debugElement.nativeElement).toHaveText(text);
+ }
+
+ afterEach(() => { fixture = null; });
beforeEach(() => {
- TestBed.configureTestingModule(
- {declarations: [TestComponent, CaptureTplRefs], imports: [CommonModule]});
+ TestBed.configureTestingModule({
+ declarations: [
+ TestComponent,
+ CaptureTplRefs,
+ ],
+ imports: [CommonModule],
+ });
});
it('should do nothing if templateRef is null', async(() => {
const template = ``;
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
+ fixture = createTestComponent(template);
- fixture.detectChanges();
- expect(fixture.nativeElement).toHaveText('');
+ detectChangesAndExpectText('');
}));
it('should insert content specified by TemplateRef', async(() => {
const template =
`foo`;
- TestBed.overrideComponent(TestComponent, {set: {template: template}})
- .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]});
+ fixture = createTestComponent(template);
- let fixture = TestBed.createComponent(TestComponent);
+ detectChangesAndExpectText('');
- fixture.detectChanges();
- expect(fixture.nativeElement).toHaveText('');
+ const refs = fixture.debugElement.children[0].references['refs'];
- var refs = fixture.debugElement.children[0].references['refs'];
-
- fixture.componentInstance.currentTplRef = refs.tplRefs.first;
- fixture.detectChanges();
- expect(fixture.nativeElement).toHaveText('foo');
+ setTplRef(refs.tplRefs.first);
+ detectChangesAndExpectText('foo');
}));
it('should clear content if TemplateRef becomes null', async(() => {
const template =
`foo`;
- TestBed.overrideComponent(TestComponent, {set: {template: template}})
- .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]});
- let fixture = TestBed.createComponent(TestComponent);
-
+ fixture = createTestComponent(template);
fixture.detectChanges();
- var refs = fixture.debugElement.children[0].references['refs'];
+ const refs = fixture.debugElement.children[0].references['refs'];
- fixture.componentInstance.currentTplRef = refs.tplRefs.first;
- fixture.detectChanges();
- expect(fixture.nativeElement).toHaveText('foo');
+ setTplRef(refs.tplRefs.first);
+ detectChangesAndExpectText('foo');
- fixture.componentInstance.currentTplRef = null;
- fixture.detectChanges();
- expect(fixture.nativeElement).toHaveText('');
+ setTplRef(null);
+ detectChangesAndExpectText('');
}));
it('should swap content if TemplateRef changes', async(() => {
const template =
`foobar`;
- TestBed.overrideComponent(TestComponent, {set: {template: template}})
- .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]});
- let fixture = TestBed.createComponent(TestComponent);
+ fixture = createTestComponent(template);
fixture.detectChanges();
- var refs = fixture.debugElement.children[0].references['refs'];
+ const refs = fixture.debugElement.children[0].references['refs'];
- fixture.componentInstance.currentTplRef = refs.tplRefs.first;
- fixture.detectChanges();
- expect(fixture.nativeElement).toHaveText('foo');
+ setTplRef(refs.tplRefs.first);
+ detectChangesAndExpectText('foo');
- fixture.componentInstance.currentTplRef = refs.tplRefs.last;
- fixture.detectChanges();
- expect(fixture.nativeElement).toHaveText('bar');
+ setTplRef(refs.tplRefs.last);
+ detectChangesAndExpectText('bar');
}));
it('should display template if context is null', async(() => {
const template =
`foo`;
- TestBed.overrideComponent(TestComponent, {set: {template: template}})
- .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]});
- let fixture = TestBed.createComponent(TestComponent);
+ fixture = createTestComponent(template);
+ detectChangesAndExpectText('');
- fixture.detectChanges();
- expect(fixture.nativeElement).toHaveText('');
+ const refs = fixture.debugElement.children[0].references['refs'];
- var refs = fixture.debugElement.children[0].references['refs'];
-
- fixture.componentInstance.currentTplRef = refs.tplRefs.first;
- fixture.detectChanges();
- expect(fixture.nativeElement).toHaveText('foo');
+ setTplRef(refs.tplRefs.first);
+ detectChangesAndExpectText('foo');
}));
it('should reflect initial context and changes', async(() => {
const template =
`{{foo}}`;
- TestBed.overrideComponent(TestComponent, {set: {template: template}})
- .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]});
- let fixture = TestBed.createComponent(TestComponent);
- fixture.detectChanges();
-
- var refs = fixture.debugElement.children[0].references['refs'];
- fixture.componentInstance.currentTplRef = refs.tplRefs.first;
+ fixture = createTestComponent(template);
fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('bar');
+
+ const refs = fixture.debugElement.children[0].references['refs'];
+ setTplRef(refs.tplRefs.first);
+
+ detectChangesAndExpectText('bar');
fixture.componentInstance.context.foo = 'alter-bar';
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('alter-bar');
+ detectChangesAndExpectText('alter-bar');
}));
it('should reflect user defined $implicit property in the context', async(() => {
const template =
`{{ctx.foo}}`;
- TestBed.overrideComponent(TestComponent, {set: {template: template}})
- .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]});
- let fixture = TestBed.createComponent(TestComponent);
+ fixture = createTestComponent(template);
+
fixture.detectChanges();
- var refs = fixture.debugElement.children[0].references['refs'];
- fixture.componentInstance.currentTplRef = refs.tplRefs.first;
+ const refs = fixture.debugElement.children[0].references['refs'];
+ setTplRef(refs.tplRefs.first);
fixture.componentInstance.context = {$implicit: fixture.componentInstance.context};
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('bar');
+ detectChangesAndExpectText('bar');
}));
it('should reflect context re-binding', async(() => {
const template =
`{{shawshank}}`;
- TestBed.overrideComponent(TestComponent, {set: {template: template}})
- .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]});
+ fixture = createTestComponent(template);
- let fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
- var refs = fixture.debugElement.children[0].references['refs'];
- fixture.componentInstance.currentTplRef = refs.tplRefs.first;
+ const refs = fixture.debugElement.children[0].references['refs'];
+ setTplRef(refs.tplRefs.first);
fixture.componentInstance.context = {shawshank: 'brooks'};
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('brooks');
+ detectChangesAndExpectText('brooks');
fixture.componentInstance.context = {shawshank: 'was here'};
- fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('was here');
+ detectChangesAndExpectText('was here');
}));
});
}
@@ -172,3 +157,9 @@ class TestComponent {
currentTplRef: TemplateRef;
context: any = {foo: 'bar'};
}
+
+function createTestComponent(template: string): ComponentFixture {
+ return TestBed.overrideComponent(TestComponent, {set: {template: template}})
+ .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]})
+ .createComponent(TestComponent);
+}
\ No newline at end of file
diff --git a/modules/@angular/common/test/directives/non_bindable_spec.ts b/modules/@angular/common/test/directives/non_bindable_spec.ts
index ba925aeef9..77da487371 100644
--- a/modules/@angular/common/test/directives/non_bindable_spec.ts
+++ b/modules/@angular/common/test/directives/non_bindable_spec.ts
@@ -8,7 +8,7 @@
import {Component, Directive} from '@angular/core';
import {ElementRef} from '@angular/core/src/linker/element_ref';
-import {TestBed, async} from '@angular/core/testing';
+import {ComponentFixture, TestBed, async} from '@angular/core/testing';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {expect} from '@angular/platform-browser/testing/matchers';
@@ -22,31 +22,29 @@ export function main() {
});
it('should not interpolate children', async(() => {
- var template = '{{text}}{{text}}
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
+ const template = '{{text}}{{text}}
';
+ const fixture = createTestComponent(template);
+
fixture.detectChanges();
- expect(fixture.debugElement.nativeElement).toHaveText('foo{{text}}');
+ expect(fixture.nativeElement).toHaveText('foo{{text}}');
}));
it('should ignore directives on child nodes', async(() => {
- var template = '{{text}}
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
+ const template = '{{text}}
';
+ const fixture = createTestComponent(template);
fixture.detectChanges();
// We must use getDOM().querySelector instead of fixture.query here
// since the elements inside are not compiled.
- var span = getDOM().querySelector(fixture.debugElement.nativeElement, '#child');
+ const span = getDOM().querySelector(fixture.nativeElement, '#child');
expect(getDOM().hasClass(span, 'compiled')).toBeFalsy();
}));
it('should trigger directives on the same node', async(() => {
- var template = '{{text}}
';
- TestBed.overrideComponent(TestComponent, {set: {template: template}});
- let fixture = TestBed.createComponent(TestComponent);
+ const template = '{{text}}
';
+ const fixture = createTestComponent(template);
fixture.detectChanges();
- var span = getDOM().querySelector(fixture.debugElement.nativeElement, '#child');
+ const span = getDOM().querySelector(fixture.nativeElement, '#child');
expect(getDOM().hasClass(span, 'compiled')).toBeTruthy();
}));
});
@@ -62,3 +60,8 @@ class TestComponent {
text: string;
constructor() { this.text = 'foo'; }
}
+
+function createTestComponent(template: string): ComponentFixture {
+ return TestBed.overrideComponent(TestComponent, {set: {template: template}})
+ .createComponent(TestComponent);
+}