Skip to content

Commit

Permalink
feat(tests): add TestComponentBuilder
Browse files Browse the repository at this point in the history
Adds a TestComponentBuilder for use in component level tests.
For usage examples, see test_component_builder_spec

Closes #1812
  • Loading branch information
juliemr committed May 28, 2015
1 parent 30b6542 commit c32dbad
Show file tree
Hide file tree
Showing 6 changed files with 725 additions and 17 deletions.
25 changes: 13 additions & 12 deletions modules/angular2/src/mock/template_resolver_mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import {Type, isPresent, BaseException, stringify, isBlank} from 'angular2/src/f
import {View} from 'angular2/src/core/annotations_impl/view';
import {TemplateResolver} from 'angular2/src/core/compiler/template_resolver';


export class MockTemplateResolver extends TemplateResolver {
_templates: Map<Type, View>;
_views: Map<Type, View>;
_inlineTemplates: Map<Type, string>;
_templateCache: Map<Type, View>;
_viewCache: Map<Type, View>;
_directiveOverrides: Map<Type, Map<Type, Type>>;

constructor() {
super();
this._templates = MapWrapper.create();
this._views = MapWrapper.create();
this._inlineTemplates = MapWrapper.create();
this._templateCache = MapWrapper.create();
this._viewCache = MapWrapper.create();
this._directiveOverrides = MapWrapper.create();
}

Expand All @@ -26,7 +27,7 @@ export class MockTemplateResolver extends TemplateResolver {
*/
setView(component: Type, view: View): void {
this._checkOverrideable(component);
MapWrapper.set(this._templates, component, view);
MapWrapper.set(this._views, component, view);
}

/**
Expand All @@ -47,7 +48,7 @@ export class MockTemplateResolver extends TemplateResolver {
* @param {Type} from
* @param {Type} to
*/
overrideTemplateDirective(component: Type, from: Type, to: Type): void {
overrideViewDirective(component: Type, from: Type, to: Type): void {
this._checkOverrideable(component);

var overrides = MapWrapper.get(this._directiveOverrides, component);
Expand All @@ -62,20 +63,20 @@ export class MockTemplateResolver extends TemplateResolver {

/**
* Returns the {@link View} for a component:
* - Set the {@link View} to the overridden template when it exists or fallback to the default
* - Set the {@link View} to the overridden view when it exists or fallback to the default
* `TemplateResolver`,
* see `setView`.
* - Override the directives, see `overrideTemplateDirective`.
* - Override the directives, see `overrideViewDirective`.
* - Override the @View definition, see `setInlineTemplate`.
*
* @param component
* @returns {ViewDefinition}
*/
resolve(component: Type): View {
var view = MapWrapper.get(this._templateCache, component);
var view = MapWrapper.get(this._viewCache, component);
if (isPresent(view)) return view;

view = MapWrapper.get(this._templates, component);
view = MapWrapper.get(this._views, component);
if (isBlank(view)) {
view = super.resolve(component);
}
Expand Down Expand Up @@ -106,7 +107,7 @@ export class MockTemplateResolver extends TemplateResolver {
view = new View({template: inlineTemplate, templateUrl: null, directives: view.directives});
}

MapWrapper.set(this._templateCache, component, view);
MapWrapper.set(this._viewCache, component, view);
return view;
}

Expand All @@ -119,7 +120,7 @@ export class MockTemplateResolver extends TemplateResolver {
* @param {Type} component
*/
_checkOverrideable(component: Type): void {
var cached = MapWrapper.get(this._templateCache, component);
var cached = MapWrapper.get(this._viewCache, component);

if (isPresent(cached)) {
throw new BaseException(
Expand Down
4 changes: 3 additions & 1 deletion modules/angular2/src/test_lib/test_bed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {DOM} from 'angular2/src/dom/dom_adapter';

/**
* @exportedAs angular2/test
* TODO(juliemr): Deprecate in favor of TestComponentBuilder
*/
@Injectable()
export class TestBed {
Expand Down Expand Up @@ -61,7 +62,7 @@ export class TestBed {
* @param {Type} to
*/
overrideDirective(component: Type, from: Type, to: Type): void {
this._injector.get(TemplateResolver).overrideTemplateDirective(component, from, to);
this._injector.get(TemplateResolver).overrideViewDirective(component, from, to);
}

/**
Expand Down Expand Up @@ -107,6 +108,7 @@ export class TestBed {
/**
* Proxy to `AppView` return by `createView` in {@link TestBed} which offers a high level API for
* tests.
* TODO(juliemr): Deprecate in favor of TestElement
*/
export class ViewProxy {
_componentRef: ComponentRef;
Expand Down
Loading

0 comments on commit c32dbad

Please sign in to comment.