fix(testing): allow test component builder to override directives from lists

When a component uses a list of directives, such as `ROUTER_DIRECTIVES`,
make `TestComponentBuilder#overrideDirective` work properly for members
of the list.

Closes #7397

Closes #8217
This commit is contained in:
Julie Ralph
2016-04-24 23:11:30 -07:00
parent e1058a4d8a
commit ff2ae7a2e1
2 changed files with 48 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import {resolveForwardRef} from 'angular2/src/core/di';
import {Injectable} from 'angular2/src/core/di';
import {Map, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
import {Type, isPresent, stringify, isBlank} from 'angular2/src/facade/lang';
import {Type, isPresent, isArray, stringify, isBlank} from 'angular2/src/facade/lang';
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
import {ViewMetadata} from '../core/metadata';
@ -81,11 +82,11 @@ export class MockViewResolver extends ViewResolver {
view = super.resolve(component);
}
var directives = view.directives;
var directives = [];
var overrides = this._directiveOverrides.get(component);
if (isPresent(overrides) && isPresent(directives)) {
directives = ListWrapper.clone(view.directives);
if (isPresent(overrides) && isPresent(view.directives)) {
flattenArray(view.directives, directives);
overrides.forEach((to, from) => {
var srcIndex = directives.indexOf(from);
if (srcIndex == -1) {
@ -127,3 +128,14 @@ export class MockViewResolver extends ViewResolver {
}
}
}
function flattenArray(tree: any[], out: Array<Type | any[]>): void {
for (var i = 0; i < tree.length; i++) {
var item = resolveForwardRef(tree[i]);
if (isArray(item)) {
flattenArray(item, out);
} else {
out.push(item);
}
}
}