diff --git a/modules/@angular/core/testing/test_bed.ts b/modules/@angular/core/testing/test_bed.ts index 263e1e4f5e..8139f5ecc4 100644 --- a/modules/@angular/core/testing/test_bed.ts +++ b/modules/@angular/core/testing/test_bed.ts @@ -135,6 +135,11 @@ export class TestBed implements Injector { return TestBed; } + static overrideTemplate(component: Type, template: string): typeof TestBed { + getTestBed().overrideComponent(component, {set: {template, templateUrl: null}}); + return TestBed; + } + static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) { return getTestBed().get(token, notFoundValue); } diff --git a/modules/@angular/platform-browser/test/testing_public_spec.ts b/modules/@angular/platform-browser/test/testing_public_spec.ts index 017c5fd7e2..782dab7262 100644 --- a/modules/@angular/platform-browser/test/testing_public_spec.ts +++ b/modules/@angular/platform-browser/test/testing_public_spec.ts @@ -8,7 +8,7 @@ import {CompilerConfig, ResourceLoader} from '@angular/compiler'; import {CUSTOM_ELEMENTS_SCHEMA, Component, Directive, Injectable, Input, NgModule, Pipe} from '@angular/core'; -import {TestBed, async, fakeAsync, inject, tick, withModule} from '@angular/core/testing'; +import {TestBed, async, fakeAsync, getTestBed, inject, tick, withModule} from '@angular/core/testing'; import {expect} from '@angular/platform-browser/testing/matchers'; import {stringify} from '../src/facade/lang'; @@ -356,6 +356,21 @@ export function main() { expect(compFixture.nativeElement).toHaveText('transformed hello'); }); }); + + describe('template', () => { + let testBedSpy: any; + beforeEach(() => { + testBedSpy = spyOn(getTestBed(), 'overrideComponent').and.callThrough(); + TestBed.overrideTemplate(SomeComponent, 'newText'); + }); + it(`should override component's template`, () => { + const fixture = TestBed.createComponent(SomeComponent); + expect(fixture.nativeElement).toHaveText('newText'); + expect(testBedSpy).toHaveBeenCalledWith(SomeComponent, { + set: {template: 'newText', templateUrl: null} + }); + }); + }); }); describe('setting up the compiler', () => { diff --git a/tools/public_api_guard/core/testing/index.d.ts b/tools/public_api_guard/core/testing/index.d.ts index 3dbd951a74..e29bd7c1b1 100644 --- a/tools/public_api_guard/core/testing/index.d.ts +++ b/tools/public_api_guard/core/testing/index.d.ts @@ -89,6 +89,7 @@ export declare class TestBed implements Injector { static overrideDirective(directive: Type, override: MetadataOverride): typeof TestBed; static overrideModule(ngModule: Type, override: MetadataOverride): typeof TestBed; static overridePipe(pipe: Type, override: MetadataOverride): typeof TestBed; + static overrideTemplate(component: Type, template: string): typeof TestBed; /** @experimental */ static resetTestEnvironment(): void; static resetTestingModule(): typeof TestBed; }