Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testing): add overrideTemplate method #13372

Merged
merged 1 commit into from Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/@angular/core/testing/test_bed.ts
Expand Up @@ -134,6 +134,11 @@ export class TestBed implements Injector {
return TestBed;
}

static overrideTemplate(component: Type<any>, 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);
}
Expand Down
17 changes: 16 additions & 1 deletion modules/@angular/platform-browser/test/testing_public_spec.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/core/testing/index.d.ts
Expand Up @@ -89,6 +89,7 @@ export declare class TestBed implements Injector {
static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): typeof TestBed;
static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): typeof TestBed;
static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): typeof TestBed;
static overrideTemplate(component: Type<any>, template: string): typeof TestBed;
/** @experimental */ static resetTestEnvironment(): void;
static resetTestingModule(): typeof TestBed;
}
Expand Down