Skip to content

Commit ff2ae7a

Browse files
committed
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 angular#7397 Closes angular#8217
1 parent e1058a4 commit ff2ae7a

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

modules/angular2/src/mock/view_resolver_mock.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import {resolveForwardRef} from 'angular2/src/core/di';
12
import {Injectable} from 'angular2/src/core/di';
23
import {Map, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
3-
import {Type, isPresent, stringify, isBlank} from 'angular2/src/facade/lang';
4+
import {Type, isPresent, isArray, stringify, isBlank} from 'angular2/src/facade/lang';
45
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
56

67
import {ViewMetadata} from '../core/metadata';
@@ -81,11 +82,11 @@ export class MockViewResolver extends ViewResolver {
8182
view = super.resolve(component);
8283
}
8384

84-
var directives = view.directives;
85+
var directives = [];
8586
var overrides = this._directiveOverrides.get(component);
8687

87-
if (isPresent(overrides) && isPresent(directives)) {
88-
directives = ListWrapper.clone(view.directives);
88+
if (isPresent(overrides) && isPresent(view.directives)) {
89+
flattenArray(view.directives, directives);
8990
overrides.forEach((to, from) => {
9091
var srcIndex = directives.indexOf(from);
9192
if (srcIndex == -1) {
@@ -127,3 +128,14 @@ export class MockViewResolver extends ViewResolver {
127128
}
128129
}
129130
}
131+
132+
function flattenArray(tree: any[], out: Array<Type | any[]>): void {
133+
for (var i = 0; i < tree.length; i++) {
134+
var item = resolveForwardRef(tree[i]);
135+
if (isArray(item)) {
136+
flattenArray(item, out);
137+
} else {
138+
out.push(item);
139+
}
140+
}
141+
}

modules/angular2/test/testing/test_component_builder_spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
TestComponentBuilder
1515
} from 'angular2/testing_internal';
1616

17+
import {CONST_EXPR} from 'angular2/src/facade/lang';
1718
import {Injectable, provide} from 'angular2/core';
1819
import {NgIf} from 'angular2/common';
1920
import {Directive, Component, ViewMetadata} from 'angular2/src/core/metadata';
@@ -99,6 +100,25 @@ class TestViewBindingsComp {
99100
constructor(private fancyService: FancyService) {}
100101
}
101102

103+
@Component({selector: 'li1', template: `<span>One</span>`})
104+
class ListDir1 {
105+
}
106+
107+
@Component({selector: 'li1', template: `<span>Alternate One</span>`})
108+
class ListDir1Alt {
109+
}
110+
111+
@Component({selector: 'li2', template: `<span>Two</span>`})
112+
class ListDir2 {
113+
}
114+
115+
const LIST_CHILDREN = CONST_EXPR([ListDir1, ListDir2]);
116+
117+
@Component(
118+
{selector: 'directive-list-comp', template: `(<li1></li1>)(<li2></li2>)`, directives: [LIST_CHILDREN]})
119+
class DirectiveListComp {
120+
}
121+
102122

103123
export function main() {
104124
describe('test component builder', function() {
@@ -168,6 +188,18 @@ export function main() {
168188
});
169189
}));
170190

191+
it('should override items from a list',
192+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
193+
194+
tcb.overrideDirective(DirectiveListComp, ListDir1, ListDir1Alt)
195+
.createAsync(DirectiveListComp)
196+
.then((componentFixture) => {
197+
componentFixture.detectChanges();
198+
expect(componentFixture.nativeElement).toHaveText('(Alternate One)(Two)');
199+
200+
async.done();
201+
});
202+
}));
171203

172204
it("should override child component's dependencies",
173205
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {

0 commit comments

Comments
 (0)