From ce2af65f7dca853274434e5a06802e2aae53fb10 Mon Sep 17 00:00:00 2001 From: satanTime Date: Fri, 10 Mar 2023 10:15:32 +0100 Subject: [PATCH] =?UTF-8?q?fix(core):=20removing=20dependency=20on=20?= =?UTF-8?q?=C9=B5ivyEnabled=20#5118?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../promise/handle-entry-components.ts | 32 +++++++++---------- .../without-selector-ivy.spec.ts | 3 +- .../double-decorator/without-selector.spec.ts | 3 +- tests/issue-333/test.spec.ts | 20 ++++++------ 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/libs/ng-mocks/src/lib/mock-builder/promise/handle-entry-components.ts b/libs/ng-mocks/src/lib/mock-builder/promise/handle-entry-components.ts index 229936984c..b68c3233f8 100644 --- a/libs/ng-mocks/src/lib/mock-builder/promise/handle-entry-components.ts +++ b/libs/ng-mocks/src/lib/mock-builder/promise/handle-entry-components.ts @@ -1,11 +1,4 @@ -import * as module from '@angular/core'; - -let isIvy = false; -try { - isIvy = module.ɵivyEnabled; -} catch { - // nothing to do -} +import { ComponentFactoryResolver, NgModule, Optional } from '@angular/core'; import coreDefineProperty from '../../common/core.define-property'; import { extendClass } from '../../common/core.helpers'; @@ -16,20 +9,26 @@ import helperCreateClone from '../../mock-service/helper.create-clone'; import { NgMeta } from './types'; class EntryComponentsModule { - protected originCFR: module.ComponentFactoryResolver['resolveComponentFactory']; + public constructor(map: Map, componentFactoryResolver?: ComponentFactoryResolver) { + // istanbul ignore if + if (!componentFactoryResolver) { + return; + } - public constructor(map: Map, protected componentFactoryResolver: module.ComponentFactoryResolver) { - this.originCFR = componentFactoryResolver.resolveComponentFactory; + const originCFR = componentFactoryResolver.resolveComponentFactory; componentFactoryResolver.resolveComponentFactory = helperCreateClone( - this.originCFR, + originCFR, undefined, undefined, (component: any, ...args: any[]) => - this.originCFR.apply(componentFactoryResolver, [map.get(component) ?? component, ...args] as any), + originCFR.apply(componentFactoryResolver, [map.get(component) ?? component, ...args] as any), ); } } -coreDefineProperty(EntryComponentsModule, 'parameters', [[NG_MOCKS], [module.ComponentFactoryResolver]]); +coreDefineProperty(EntryComponentsModule, 'parameters', [[NG_MOCKS], [ComponentFactoryResolver, new Optional()]]); + +class IvyModule {} +NgModule()(IvyModule); export default (ngModule: NgMeta): void => { const entryComponents: any[] = []; @@ -40,10 +39,11 @@ export default (ngModule: NgMeta): void => { } // the way to cause entryComponents to do its work const entryModule = extendClass(EntryComponentsModule); - module.NgModule({ + NgModule({ // Ivy knows how to make any component an entry point, // but we still would like to patch resolveComponentFactory in order to provide mocks. - entryComponents: isIvy ? [] : /* istanbul ignore next */ entryComponents, + // ɵmod is added only if Ivy has been enabled. + entryComponents: (IvyModule as any).ɵmod ? [] : /* istanbul ignore next */ entryComponents, })(entryModule); ngModule.imports.push(entryModule); }; diff --git a/tests/double-decorator/without-selector-ivy.spec.ts b/tests/double-decorator/without-selector-ivy.spec.ts index eec86f3bc3..468d01609c 100644 --- a/tests/double-decorator/without-selector-ivy.spec.ts +++ b/tests/double-decorator/without-selector-ivy.spec.ts @@ -1,4 +1,3 @@ -import * as core from '@angular/core'; import { Component, Directive, @@ -35,7 +34,7 @@ class ModuleWithComponent {} describe('double-decorator-ivy:without-selector', () => { // Because of junit issue we need to return before beforeEach // https://github.com/karma-runner/karma-junit-reporter/issues/186 - if (!(core as any).ɵivyEnabled) { + if (!(ModuleWithComponent as any).ɵmod) { it('ivy fails differently', () => { // pending('ivy fails differently'); expect(true).toBeTruthy(); diff --git a/tests/double-decorator/without-selector.spec.ts b/tests/double-decorator/without-selector.spec.ts index 1430a3ed26..cd5443bf65 100644 --- a/tests/double-decorator/without-selector.spec.ts +++ b/tests/double-decorator/without-selector.spec.ts @@ -4,7 +4,6 @@ import { Injectable, NgModule, } from '@angular/core'; -import * as core from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { MockBuilder, MockRender } from 'ng-mocks'; @@ -35,7 +34,7 @@ class ModuleWithComponent {} describe('double-decorator:without-selector', () => { // Because of junit issue we need to return before beforeEach // https://github.com/karma-runner/karma-junit-reporter/issues/186 - if ((core as any).ɵivyEnabled) { + if ((ModuleWithComponent as any).ɵmod) { it('ivy fails differently', () => { // pending('ivy fails differently'); expect(true).toBeTruthy(); diff --git a/tests/issue-333/test.spec.ts b/tests/issue-333/test.spec.ts index 38334c048b..548d85e574 100644 --- a/tests/issue-333/test.spec.ts +++ b/tests/issue-333/test.spec.ts @@ -1,36 +1,36 @@ import { CommonModule } from '@angular/common'; -import * as core from '@angular/core'; +import { Component, NgModule, Type } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { isMockOf, MockBuilder, MockRender, ngMocks } from 'ng-mocks'; -@core.Component({ +@Component({ selector: 'dynamic-overlay', template: '', }) export class DynamicOverlayComponent { - public component?: core.Type; + public component?: Type; - public attachComponent(component: core.Type) { + public attachComponent(component: Type) { this.component = component; } } -@core.NgModule({ +@NgModule({ declarations: [DynamicOverlayComponent], exports: [DynamicOverlayComponent], imports: [CommonModule], }) export class OverlayModule {} -@core.Component({ +@Component({ selector: 'dep-component', template: 'Dependency', }) class DepComponent {} -@core.Component({ +@Component({ selector: 'mock-component', template: '

', }) @@ -38,7 +38,7 @@ class MockComponent { public flag = true; } -@core.NgModule({ +@NgModule({ declarations: [MockComponent, DepComponent], entryComponents: [MockComponent], exports: [MockComponent], @@ -103,7 +103,7 @@ describe('issue-333', () => { expect(isMockOf(instance, MockComponent)).toEqual(true); }); - if (!(core as any).ɵivyEnabled) { + if (!(MockModule as any).ɵmod) { it('fails on unknown', () => { const fixture = MockRender(DynamicOverlayComponent); fixture.point.componentInstance.attachComponent(DepComponent); @@ -117,7 +117,7 @@ describe('issue-333', () => { describe('3:mock', () => { // Because of junit issue we need to return before beforeEach // https://github.com/karma-runner/karma-junit-reporter/issues/186 - if ((core as any).ɵivyEnabled) { + if ((MockModule as any).ɵmod) { it('ivy fails differently', () => { // pending('ivy fails differently'); expect(true).toBeTruthy();