Skip to content

Commit

Permalink
fix(MockRender): respecting customizations for declarations without s…
Browse files Browse the repository at this point in the history
…electors help-me-mom#4032
  • Loading branch information
satanTime committed Oct 30, 2022
1 parent 038b3a0 commit 95c20e4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
37 changes: 18 additions & 19 deletions libs/ng-mocks/src/lib/mock-render/func.reflect-template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, Directive } from '@angular/core';
import { TestBed } from '@angular/core/testing';

import coreDefineProperty from '../common/core.define-property';
import { extendClass } from '../common/core.helpers';
import coreReflectDirectiveResolve from '../common/core.reflect.directive-resolve';
import { AnyType } from '../common/core.types';
Expand All @@ -13,34 +14,31 @@ const registerTemplateMiddleware = (template: AnyType<any>, meta: Directive): vo
provide: template,
useExisting: child,
};
const providers = [...(meta.providers || []), alias];
meta.providers = providers;

if (isNgDef(template, 'c')) {
Component(meta)(child);
} else {
Directive(meta)(child);
}
TestBed.configureTestingModule({
declarations: [child],
});
meta.providers = [...(meta.providers || []), alias];

// /~https://github.com/help-me-mom/ng-mocks/issues/1876
// We need to apply overrides to our cloned declaration.
let set: any = {};
try {
const ngMocksOverrides: Map<any, any> = (TestBed as any).ngMocksOverrides;
const { override } = ngMocksOverrides.get(template);
const { set } = override;
ngMocksOverrides.set(child, { set: meta });
TestBed.overrideComponent(child, {
set: {
...set,
providers: [...set.providers, alias],
},
});
set = override.set;
if (set.providers) {
set.providers.push(alias);
}
} catch {
// nothing to do
}

const standalone = (meta as any).__ngMocksStandalone === true;
(isNgDef(template, 'c') ? Component : Directive)({
...meta,
...set,
...(standalone ? { standalone } : {}),
})(child);
TestBed.configureTestingModule({
[standalone ? 'imports' : 'declarations']: [child],
});
};

export default (template: AnyType<any>): Directive => {
Expand All @@ -52,6 +50,7 @@ export default (template: AnyType<any>): Directive => {
const override: Directive = {};
for (const key of Object.keys(meta)) {
if (key === 'standalone') {
coreDefineProperty(override, '__ngMocksStandalone', meta[key as never] || false);
continue;
}

Expand Down
58 changes: 58 additions & 0 deletions tests/issue-4032/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { RouterModule } from '@angular/router';

import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Component({
selector: 'my-test-one',
standalone: true,
imports: [CommonModule, RouterModule],
template: `<a [routerLink]="['test']">Test</a>`,
})
class TestOneComponent {}

@Component({
standalone: true,
imports: [CommonModule, RouterModule],
template: `<a [routerLink]="['test']">Test</a>`,
})
class TestTwoComponent {}

// @see /~https://github.com/help-me-mom/ng-mocks/issues/4032
describe('issue-4032', () => {
ngMocks.throwOnConsole();

describe('w/ selector', () => {
beforeEach(() => MockBuilder(TestOneComponent));

it('should render w/ MockRender', () => {
expect(
MockRender(TestOneComponent).point.componentInstance,
).toBeDefined();
});

it('should render w/ TestBed', () => {
const fixture = TestBed.createComponent(TestOneComponent);
fixture.detectChanges();
expect(fixture.componentInstance).toBeDefined();
});
});

describe('w/o selector', () => {
beforeEach(() => MockBuilder(TestTwoComponent));

it('should render w/ MockRender', () => {
expect(
MockRender(TestTwoComponent).point.componentInstance,
).toBeDefined();
});

it('should render w/ TestBed', () => {
const fixture = TestBed.createComponent(TestTwoComponent);
fixture.detectChanges();
expect(fixture.componentInstance).toBeDefined();
});
});
});

0 comments on commit 95c20e4

Please sign in to comment.