From b4996110d36af28412a79bfe30500473a6c62ec6 Mon Sep 17 00:00:00 2001 From: satanTime Date: Sat, 10 Dec 2022 12:04:56 +0100 Subject: [PATCH] test: covering nested replacement #4486 --- tests/issue-4486/angular.spec.ts | 133 +++++++++++++++++++++++++++++++ tests/issue-4486/test.spec.ts | 80 +++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 tests/issue-4486/angular.spec.ts create mode 100644 tests/issue-4486/test.spec.ts diff --git a/tests/issue-4486/angular.spec.ts b/tests/issue-4486/angular.spec.ts new file mode 100644 index 0000000000..fdcba6f285 --- /dev/null +++ b/tests/issue-4486/angular.spec.ts @@ -0,0 +1,133 @@ +import { Component, NgModule, VERSION } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; + +@Component({ + selector: 'real', + template: 'real', +}) +class RealComponent {} + +@NgModule({ + declarations: [RealComponent], + exports: [RealComponent], +}) +class RealModule {} + +@Component({ + selector: 'nested', + template: '', +}) +class NestedComponent {} + +@NgModule({ + imports: [RealModule], + declarations: [NestedComponent], + exports: [NestedComponent], +}) +class NestedModule {} + +@Component({ + selector: 'target', + standalone: true, + imports: [NestedModule], + template: ``, +} as never) +class StandaloneComponent {} + +@Component({ + selector: 'real', + template: 'test', +}) +class RealTestingComponent {} + +@NgModule({ + declarations: [RealTestingComponent], + exports: [RealTestingComponent], +}) +class RealTestingModule {} + +@NgModule({ + declarations: [NestedComponent], + exports: [NestedComponent], + imports: [RealTestingModule], +}) +class NestedTestingModule {} + +// @see https://github.com/help-me-mom/ng-mocks/issues/4486 +describe('issue-4486:angular', () => { + if (Number.parseInt(VERSION.major, 10) < 14) { + it('a14', () => { + // pending('Need Angular >= 14'); + expect(true).toBeTruthy(); + }); + + return; + } + + // Here we check default behavior of the standalone component. + describe('default', () => { + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [StandaloneComponent], + }).compileComponents(); + }); + + it('renders RealComponent', () => { + const fixture = TestBed.createComponent(StandaloneComponent); + fixture.detectChanges(); + + expect(fixture.debugElement.nativeElement.textContent).toEqual( + 'real', + ); + }); + }); + + // Here we check whether overrideComponent.set removes imports, and it does. + describe('overrideComponent:set:empty', () => { + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [StandaloneComponent], + }) + .overrideComponent(StandaloneComponent, { + set: { + imports: [], + } as never, + }) + .compileComponents(); + }); + + it('renders nothing', () => { + const fixture = TestBed.createComponent(StandaloneComponent); + fixture.detectChanges(); + + expect(fixture.debugElement.nativeElement.textContent).toEqual( + '', + ); + }); + }); + + // Here we check whether overrideComponent.set changes imports, and it does not, however, it has to. + describe('overrideComponent:set:NestedTestingModule', () => { + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [StandaloneComponent], + }) + .overrideComponent(StandaloneComponent, { + set: { + imports: [NestedTestingModule], + } as never, + }) + .compileComponents(); + }); + + it('renders RealTestingComponent', () => { + const fixture = TestBed.createComponent(StandaloneComponent); + fixture.detectChanges(); + + // The failure is here. + expect(fixture.debugElement.nativeElement.textContent).toEqual( + 'test', + ); + }); + }); +}); diff --git a/tests/issue-4486/test.spec.ts b/tests/issue-4486/test.spec.ts new file mode 100644 index 0000000000..0d06486faf --- /dev/null +++ b/tests/issue-4486/test.spec.ts @@ -0,0 +1,80 @@ +import { Component, NgModule, VERSION } from '@angular/core'; + +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; + +@Component({ + selector: 'real', + template: 'real', +}) +class RealComponent {} + +@NgModule({ + declarations: [RealComponent], + exports: [RealComponent], +}) +class RealModule {} + +@Component({ + selector: 'nested', + template: '', +}) +class NestedComponent {} + +@NgModule({ + imports: [RealModule], + declarations: [NestedComponent], + exports: [NestedComponent], +}) +class NestedModule {} + +@Component({ + selector: 'target', + standalone: true, + imports: [NestedModule], + template: ``, +} as never) +class StandaloneComponent {} + +@Component({ + selector: 'real', + template: 'test', +}) +class RealTestingComponent {} + +@NgModule({ + declarations: [RealTestingComponent], + exports: [RealTestingComponent], +}) +class RealTestingModule {} + +@NgModule({ + declarations: [NestedComponent], + exports: [NestedComponent], + imports: [RealTestingModule], +}) +class NestedTestingModule {} + +// @see https://github.com/help-me-mom/ng-mocks/issues/4486 +describe('issue-4486', () => { + if (Number.parseInt(VERSION.major, 10) < 14) { + it('a14', () => { + // pending('Need Angular >= 14'); + expect(true).toBeTruthy(); + }); + + return; + } + + beforeEach(() => + MockBuilder(StandaloneComponent).replace( + NestedModule, + NestedTestingModule, + ), + ); + + it('renders RealTestingComponent', () => { + const fixture = MockRender(StandaloneComponent); + + expect(ngMocks.formatText(fixture)).toEqual('test'); + }); +});