From 509aa61619116afeb0140b762a92f5fd9e299b63 Mon Sep 17 00:00:00 2001 From: Andrew Kushnir Date: Tue, 18 Dec 2018 10:30:14 -0800 Subject: [PATCH] fix(ivy): apply all overrides from TestBed, not the last one only (#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 #27734 --- packages/core/testing/src/resolvers.ts | 16 +++++++++++----- .../platform-browser/test/testing_public_spec.ts | 11 +++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/core/testing/src/resolvers.ts b/packages/core/testing/src/resolvers.ts index 262d387d02..2a15dd7bc5 100644 --- a/packages/core/testing/src/resolvers.ts +++ b/packages/core/testing/src/resolvers.ts @@ -22,14 +22,18 @@ export interface Resolver { resolve(type: Type): T|null; } * Allows to override ivy metadata for tests (via the `TestBed`). */ abstract class OverrideResolver implements Resolver { - private overrides = new Map, MetadataOverride>(); + private overrides = new Map, MetadataOverride[]>(); private resolved = new Map, T|null>(); abstract get type(): any; setOverrides(overrides: Array<[Type, MetadataOverride]>) { this.overrides.clear(); - overrides.forEach(([type, override]) => this.overrides.set(type, override)); + overrides.forEach(([type, override]) => { + const overrides = this.overrides.get(type) || []; + overrides.push(override); + this.overrides.set(type, overrides); + }); } getAnnotation(type: Type): T|null { @@ -42,10 +46,12 @@ abstract class OverrideResolver implements Resolver { if (!resolved) { resolved = this.getAnnotation(type); if (resolved) { - const override = this.overrides.get(type); - if (override) { + const overrides = this.overrides.get(type); + if (overrides) { const overrider = new MetadataOverrider(); - resolved = overrider.overrideMetadata(this.type, resolved, override); + overrides.forEach(override => { + resolved = overrider.overrideMetadata(this.type, resolved !, override); + }); } } this.resolved.set(type, resolved); diff --git a/packages/platform-browser/test/testing_public_spec.ts b/packages/platform-browser/test/testing_public_spec.ts index 12445154f3..e41fbcbbcb 100644 --- a/packages/platform-browser/test/testing_public_spec.ts +++ b/packages/platform-browser/test/testing_public_spec.ts @@ -388,12 +388,11 @@ class CompWithUrlTemplate { .overridePipe(SomePipe, {set: {name: 'somePipe'}}) .overridePipe(SomePipe, {add: {pure: false}}); }); - fixmeIvy('FW-788: Support metadata override in TestBed (for AOT-compiled components)') - .it('should work', () => { - const compFixture = TestBed.createComponent(SomeComponent); - compFixture.detectChanges(); - expect(compFixture.nativeElement).toHaveText('transformed hello'); - }); + it('should work', () => { + const compFixture = TestBed.createComponent(SomeComponent); + compFixture.detectChanges(); + expect(compFixture.nativeElement).toHaveText('transformed hello'); + }); }); describe('template', () => {