Skip to content

Commit 509aa61

Browse files
AndrewKushnirmatsko
authored andcommitted
fix(ivy): apply all overrides from TestBed, not the last one only (angular#27734)
In some cases in our tests we can define multiple overrides for a given class. As a result, only the last override is actually applied due to the fact that we store overrides in a Type<->Override map. This update changes the logic to keep all overrides defined in a given test for a Type (i.e. Type<->Override[] map) and applies them one by one at resolution phase. This behavior is more inline with the previous TestBed. PR Close angular#27734
1 parent cdd737e commit 509aa61

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

‎packages/core/testing/src/resolvers.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ export interface Resolver<T> { resolve(type: Type<any>): T|null; }
2222
* Allows to override ivy metadata for tests (via the `TestBed`).
2323
*/
2424
abstract class OverrideResolver<T> implements Resolver<T> {
25-
private overrides = new Map<Type<any>, MetadataOverride<T>>();
25+
private overrides = new Map<Type<any>, MetadataOverride<T>[]>();
2626
private resolved = new Map<Type<any>, T|null>();
2727

2828
abstract get type(): any;
2929

3030
setOverrides(overrides: Array<[Type<any>, MetadataOverride<T>]>) {
3131
this.overrides.clear();
32-
overrides.forEach(([type, override]) => this.overrides.set(type, override));
32+
overrides.forEach(([type, override]) => {
33+
const overrides = this.overrides.get(type) || [];
34+
overrides.push(override);
35+
this.overrides.set(type, overrides);
36+
});
3337
}
3438

3539
getAnnotation(type: Type<any>): T|null {
@@ -42,10 +46,12 @@ abstract class OverrideResolver<T> implements Resolver<T> {
4246
if (!resolved) {
4347
resolved = this.getAnnotation(type);
4448
if (resolved) {
45-
const override = this.overrides.get(type);
46-
if (override) {
49+
const overrides = this.overrides.get(type);
50+
if (overrides) {
4751
const overrider = new MetadataOverrider();
48-
resolved = overrider.overrideMetadata(this.type, resolved, override);
52+
overrides.forEach(override => {
53+
resolved = overrider.overrideMetadata(this.type, resolved !, override);
54+
});
4955
}
5056
}
5157
this.resolved.set(type, resolved);

‎packages/platform-browser/test/testing_public_spec.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,11 @@ class CompWithUrlTemplate {
388388
.overridePipe(SomePipe, {set: {name: 'somePipe'}})
389389
.overridePipe(SomePipe, {add: {pure: false}});
390390
});
391-
fixmeIvy('FW-788: Support metadata override in TestBed (for AOT-compiled components)')
392-
.it('should work', () => {
393-
const compFixture = TestBed.createComponent(SomeComponent);
394-
compFixture.detectChanges();
395-
expect(compFixture.nativeElement).toHaveText('transformed hello');
396-
});
391+
it('should work', () => {
392+
const compFixture = TestBed.createComponent(SomeComponent);
393+
compFixture.detectChanges();
394+
expect(compFixture.nativeElement).toHaveText('transformed hello');
395+
});
397396
});
398397

399398
describe('template', () => {

0 commit comments

Comments
 (0)