From 1e01f82714a6275b863f0f67046944d879250d16 Mon Sep 17 00:00:00 2001 From: satanTime Date: Sat, 24 Dec 2022 10:54:01 +0100 Subject: [PATCH] fix(core): correctly defines TestBed with multiple declarations with the same selector #4564 --- examples/MockActivatedRoute/test.spec.ts | 5 +- .../lib/common/ng-mocks-global-overrides.ts | 4 +- tests/issue-4564/test.spec.ts | 71 +++++++++++++------ 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/examples/MockActivatedRoute/test.spec.ts b/examples/MockActivatedRoute/test.spec.ts index 2d6f80e74e..1f6c03b57c 100644 --- a/examples/MockActivatedRoute/test.spec.ts +++ b/examples/MockActivatedRoute/test.spec.ts @@ -1,4 +1,4 @@ -import { Component, NgModule } from '@angular/core'; +import { Component, NgModule, OnInit } from '@angular/core'; import { ActivatedRoute, RouterModule } from '@angular/router'; import { MockBuilder, MockInstance, MockRender } from 'ng-mocks'; @@ -7,11 +7,10 @@ import { MockBuilder, MockInstance, MockRender } from 'ng-mocks'; selector: 'target', template: '{{ param }}', }) -class TargetComponent { +class TargetComponent implements OnInit { public param: string | null = null; public constructor(private route: ActivatedRoute) {} - ngOnInit() { this.param = this.route.snapshot.paramMap.get('paramId'); } diff --git a/libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts b/libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts index b2aeba2f14..d2a3231526 100644 --- a/libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts +++ b/libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts @@ -208,9 +208,7 @@ const configureTestingModule = isNgModuleDefWithProviders(declaration) ? declaration.ngModule : declaration, isMockNgDef(funcGetType(declaration)), ]); - if (key === 'imports') { - hasMocks |= mockBuilder[mockBuilder.length - 1][2] ? 0b10 : 0b01; - } + hasMocks |= mockBuilder[mockBuilder.length - 1][2] ? 0b10 : 0b01; } } // We should do magic only then both mock and real exist. diff --git a/tests/issue-4564/test.spec.ts b/tests/issue-4564/test.spec.ts index 34787d0d0a..e3dfdf948f 100644 --- a/tests/issue-4564/test.spec.ts +++ b/tests/issue-4564/test.spec.ts @@ -87,26 +87,57 @@ class SubjectUnderTestComponent {} describe('issue-4564', () => { ngMocks.throwOnConsole(); - beforeEach(() => - TestBed.configureTestingModule({ - imports: [ComponentModule, MockModule(PipeModule)], - declarations: [ - SubjectUnderTestComponent, - MockPipe(TargetPipe, () => 'mock'), - MockPipe(StandardPipe), - ], - providers: [ - MockProvider(TOKEN, 'mock'), - MockProvider(TargetService, { - func: () => 'mock', - }), - ], - schemas: [CUSTOM_ELEMENTS_SCHEMA], - }).compileComponents(), - ); + describe('issue', () => { + beforeEach(() => + TestBed.configureTestingModule({ + imports: [ComponentModule, MockModule(PipeModule)], + declarations: [ + SubjectUnderTestComponent, + MockPipe(TargetPipe, () => 'mock'), + MockPipe(StandardPipe), + ], + providers: [ + MockProvider(TOKEN, 'mock'), + MockProvider(TargetService, { + func: () => 'mock', + }), + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA], + }).compileComponents(), + ); - it('customizes pipe', () => { - const fixture = MockRender(SubjectUnderTestComponent); - expect(ngMocks.formatText(fixture)).toEqual('mock:mock:mock'); + it('customizes pipes', () => { + const fixture = MockRender(SubjectUnderTestComponent); + expect(ngMocks.formatText(fixture)).toEqual('mock:mock:mock'); + }); + }); + + describe('only mock imports', () => { + beforeEach(() => + TestBed.configureTestingModule({ + imports: [ + MockModule(ComponentModule), + MockModule(PipeModule), + ], + declarations: [ + TargetComponent, + SubjectUnderTestComponent, + MockPipe(TargetPipe, () => 'mock'), + MockPipe(StandardPipe), + ], + providers: [ + MockProvider(TOKEN, 'mock'), + MockProvider(TargetService, { + func: () => 'mock', + }), + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA], + }).compileComponents(), + ); + + it('customizes pipes', () => { + const fixture = MockRender(SubjectUnderTestComponent); + expect(ngMocks.formatText(fixture)).toEqual('mock:mock:mock'); + }); }); });