Skip to content

Commit

Permalink
fix(core): removing dependency on ɵivyEnabled help-me-mom#5118
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Mar 10, 2023
1 parent 41f2b92 commit ce2af65
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<any, any>, componentFactoryResolver?: ComponentFactoryResolver) {
// istanbul ignore if
if (!componentFactoryResolver) {
return;
}

public constructor(map: Map<any, any>, 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[] = [];
Expand All @@ -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);
};
3 changes: 1 addition & 2 deletions tests/double-decorator/without-selector-ivy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as core from '@angular/core';
import {
Component,
Directive,
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions tests/double-decorator/without-selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 10 additions & 10 deletions tests/issue-333/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
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:
'<ng-container *ngComponentOutlet="component"></ng-container>',
})
export class DynamicOverlayComponent {
public component?: core.Type<any>;
public component?: Type<any>;

public attachComponent(component: core.Type<any>) {
public attachComponent(component: Type<any>) {
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: '<h1 *ngIf="flag"><dep-component></dep-component></h1>',
})
class MockComponent {
public flag = true;
}

@core.NgModule({
@NgModule({
declarations: [MockComponent, DepComponent],
entryComponents: [MockComponent],
exports: [MockComponent],
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit ce2af65

Please sign in to comment.