Skip to content

Commit

Permalink
fix(core): parsing only own declarations #1587
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jan 17, 2022
1 parent 771a7db commit 978bdbc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
32 changes: 18 additions & 14 deletions libs/ng-mocks/src/lib/resolve/collect-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,30 @@ const parse = (def: any): any => {
coreDefineProperty(def, '__ngMocksParsed', true);

// <=13.0.2
for (const annotation of def.__annotations__ || []) {
const ngMetadataName = annotation?.ngMetadataName;
if (!ngMetadataName) {
continue;
}
if (def.hasOwnProperty('__annotations__')) {
for (const annotation of def.__annotations__) {
const ngMetadataName = annotation?.ngMetadataName;
if (!ngMetadataName) {
continue;
}

declarations[ngMetadataName] = { ...annotation };
declarations[ngMetadataName] = { ...annotation };
}
}

// >13.0.2
for (const decorator of def.decorators || []) {
const ngMetadataName = decorator?.type?.prototype?.ngMetadataName;
if (!ngMetadataName) {
continue;
}
if (def.hasOwnProperty('decorators')) {
for (const decorator of def.decorators) {
const ngMetadataName = decorator?.type?.prototype?.ngMetadataName;
if (!ngMetadataName) {
continue;
}

declarations[ngMetadataName] = decorator.args ? { ...decorator.args[0] } : {};
declarations[ngMetadataName] = decorator.args ? { ...decorator.args[0] } : {};
}
}

{
if (def.hasOwnProperty('propDecorators')) {
const props: string[] = [];
for (const key of def.propDecorators ? Object.getOwnPropertyNames(def.propDecorators) : []) {
if (props.indexOf(key) === -1) {
Expand Down Expand Up @@ -123,7 +127,7 @@ const parse = (def: any): any => {
}
}
}
{
if (def.hasOwnProperty('__prop__metadata__')) {
const props: string[] = [];
for (const key of def.__prop__metadata__ ? Object.getOwnPropertyNames(def.__prop__metadata__) : []) {
if (props.indexOf(key) === -1) {
Expand Down
32 changes: 32 additions & 0 deletions tests/issue-1587/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { CommonModule, DatePipe } from '@angular/common';
import { Component, Injectable, NgModule, Pipe } from '@angular/core';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Injectable()
@Pipe({
name: 'target',
})
class TargetPipe extends DatePipe {}

@NgModule({
declarations: [TargetPipe],
exports: [TargetPipe],
imports: [CommonModule],
})
class TargetModule {}

@Component({
selector: 'target',
template: `{{ '2022-01-17' | target }}`,
})
class TargetComponent {}

// https://github.com/ike18t/ng-mocks/issues/1587
describe('issue-1587', () => {
beforeEach(() => MockBuilder(TargetComponent, TargetModule));

it('should contain valid html without user', () => {
const fixture = MockRender(TargetComponent);
expect(ngMocks.formatHtml(fixture)).toEqual('<target></target>');
});
});

0 comments on commit 978bdbc

Please sign in to comment.