diff --git a/modules/angular2/src/mock/view_resolver_mock.ts b/modules/angular2/src/mock/view_resolver_mock.ts index 1c7eb1f2ac..d23affa5db 100644 --- a/modules/angular2/src/mock/view_resolver_mock.ts +++ b/modules/angular2/src/mock/view_resolver_mock.ts @@ -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): void { + for (var i = 0; i < tree.length; i++) { + var item = resolveForwardRef(tree[i]); + if (isArray(item)) { + flattenArray(item, out); + } else { + out.push(item); + } + } +} diff --git a/modules/angular2/test/testing/test_component_builder_spec.ts b/modules/angular2/test/testing/test_component_builder_spec.ts index 4a07a04c0f..e6c1b2bb3d 100644 --- a/modules/angular2/test/testing/test_component_builder_spec.ts +++ b/modules/angular2/test/testing/test_component_builder_spec.ts @@ -14,6 +14,7 @@ import { TestComponentBuilder } from 'angular2/testing_internal'; +import {CONST_EXPR} from 'angular2/src/facade/lang'; import {Injectable, provide} from 'angular2/core'; import {NgIf} from 'angular2/common'; import {Directive, Component, ViewMetadata} from 'angular2/src/core/metadata'; @@ -99,6 +100,25 @@ class TestViewBindingsComp { constructor(private fancyService: FancyService) {} } +@Component({selector: 'li1', template: `One`}) +class ListDir1 { +} + +@Component({selector: 'li1', template: `Alternate One`}) +class ListDir1Alt { +} + +@Component({selector: 'li2', template: `Two`}) +class ListDir2 { +} + +const LIST_CHILDREN = CONST_EXPR([ListDir1, ListDir2]); + +@Component( + {selector: 'directive-list-comp', template: `()()`, directives: [LIST_CHILDREN]}) +class DirectiveListComp { +} + export function main() { describe('test component builder', function() { @@ -168,6 +188,18 @@ export function main() { }); })); + it('should override items from a list', + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { + + tcb.overrideDirective(DirectiveListComp, ListDir1, ListDir1Alt) + .createAsync(DirectiveListComp) + .then((componentFixture) => { + componentFixture.detectChanges(); + expect(componentFixture.nativeElement).toHaveText('(Alternate One)(Two)'); + + async.done(); + }); + })); it("should override child component's dependencies", inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {