Skip to content

Commit

Permalink
Merge pull request #751 from satanTime/issues/736
Browse files Browse the repository at this point in the history
fix(core): allowing spies on ComponentFactoryResolver.resolveComponentFactory #736
  • Loading branch information
satanTime authored Jun 28, 2021
2 parents 1750bbd + fda714e commit e8b0234
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import coreDefineProperty from '../../common/core.define-property';
import { extendClass } from '../../common/core.helpers';
import { NG_MOCKS } from '../../common/core.tokens';
import { isNgDef } from '../../common/func.is-ng-def';
import helperCreateClone from '../../mock-service/helper.create-clone';

import { NgMeta } from './types';

Expand All @@ -21,8 +22,12 @@ class EntryComponentsModule {

public constructor(map: Map<any, any>, protected componentFactoryResolver: ComponentFactoryResolver) {
this.origin = componentFactoryResolver.resolveComponentFactory;
componentFactoryResolver.resolveComponentFactory = component =>
this.origin.call(componentFactoryResolver, map.get(component) ?? component) as any;
componentFactoryResolver.resolveComponentFactory = helperCreateClone(
this.origin,
undefined,
undefined,
(component: any) => this.origin.call(componentFactoryResolver, map.get(component) ?? component) as any,
);
}
}
coreDefineProperty(EntryComponentsModule, 'parameters', [[NG_MOCKS], [ComponentFactoryResolver]]);
Expand Down
4 changes: 2 additions & 2 deletions libs/ng-mocks/src/lib/mock-service/helper.create-clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import helperExtractMethodsFromPrototype from './helper.extract-methods-from-pro
import helperExtractPropertiesFromPrototype from './helper.extract-properties-from-prototype';
import helperExtractPropertyDescriptor from './helper.extract-property-descriptor';

export default (service: any, bindFrom?: object, bindTo?: object): any => {
export default (service: any, bindFrom?: object, bindTo?: object, mock?: any): any => {
const instance = function () {
// tslint:disable-next-line:ban-ts-ignore
// @ts-ignore
return service.apply(bindFrom === this ? bindTo : this, arguments);
return (mock || service).apply(bindFrom === this ? bindTo : this, arguments);
};

for (const prop of [
Expand Down
75 changes: 75 additions & 0 deletions tests/issue-736/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// tslint:disable strict-type-predicates

import {
Component,
ComponentFactoryResolver,
OnInit,
} from '@angular/core';
import { getMockedNgDefOf, MockBuilder, MockRender } from 'ng-mocks';

@Component({
selector: 'modal',
template: 'modal',
})
class ModalComponent {}

@Component({
selector: 'target',
template: 'target',
})
class TargetComponent implements OnInit {
public constructor(
public readonly componentFactoryResolver: ComponentFactoryResolver,
) {}

public ngOnInit(): void {
this.componentFactoryResolver.resolveComponentFactory(
ModalComponent,
);
}
}

describe('issue-736', () => {
beforeEach(() =>
MockBuilder(TargetComponent)
.mock(ModalComponent)
.provide({
provide: ComponentFactoryResolver,
useValue: {
resolveComponentFactory:
typeof jest !== 'undefined'
? jest
.fn()
.mockName(
'ComponentFactoryResolver.resolveComponentFactory',
)
: jasmine.createSpy(
'ComponentFactoryResolver.resolveComponentFactory',
),
},
}),
);

it('allows to mock resolveComponentFactory', () => {
// creating fixture without a render
const fixture = MockRender(TargetComponent, undefined, false);

// getting current instance of mock ComponentFactoryResolver
const componentFactoryResolver =
fixture.debugElement.injector.get(ComponentFactoryResolver);

// its spied resolveComponentFactory shouldn't be called
// the bug was that it is not a spy anymore.
expect(
componentFactoryResolver.resolveComponentFactory,
).not.toHaveBeenCalled();

// triggering ngOnInit
fixture.detectChanges();

// resolveComponentFactory should have been called
expect(
componentFactoryResolver.resolveComponentFactory,
).toHaveBeenCalledWith(getMockedNgDefOf(ModalComponent));
});
});

0 comments on commit e8b0234

Please sign in to comment.