Skip to content

Commit

Permalink
fix(a13): creating known props and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jan 10, 2022
1 parent e0d217b commit 5386f77
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 27 deletions.
30 changes: 30 additions & 0 deletions e2e/a13/src/e2e/double-declarations/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@ import {
ViewChildren,
} from '@angular/core';

@Directive({
selector: 'base1',
})
export class BaseCls1 {}

@Directive({
selector: 'base2',
})
export class BaseCls2 {}

@Directive({
selector: 'base3',
})
export class BaseCls3 {}

@Directive({
selector: 'override1',
})
export class OverrideCls1 {}

@Directive({
selector: 'override2',
})
export class OverrideCls2 {}

@Directive({
selector: 'override3',
})
export class OverrideCls3 {}

@Directive({
selector: 'div',
})
Expand Down
106 changes: 85 additions & 21 deletions e2e/a13/src/e2e/double-declarations/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,72 @@
import { EventEmitter, HostBinding } from '@angular/core';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

import { BaseCls, DivCls, OverrideCls } from './fixtures';
import {
BaseCls,
BaseCls1,
BaseCls2,
BaseCls3,
DivCls,
OverrideCls,
OverrideCls1,
OverrideCls2,
OverrideCls3,
} from './fixtures';

describe('double-declarations', () => {
const cases: Array<[string, any[], any[]]> = [
['real', [BaseCls], [OverrideCls, DivCls]],
['mock', [null, BaseCls], [null, [OverrideCls, DivCls]]],
[
'real',
[
[
BaseCls,
BaseCls1,
BaseCls2,
BaseCls3,
OverrideCls1,
OverrideCls2,
OverrideCls3,
],
],
[
[
OverrideCls,
DivCls,
BaseCls1,
BaseCls2,
BaseCls3,
OverrideCls1,
OverrideCls2,
OverrideCls3,
],
],
],
[
'mock',
[
[
BaseCls1,
BaseCls2,
BaseCls3,
OverrideCls1,
OverrideCls2,
OverrideCls3,
],
BaseCls,
],
[
[
DivCls,
BaseCls1,
BaseCls2,
BaseCls3,
OverrideCls1,
OverrideCls2,
OverrideCls3,
],
OverrideCls,
],
],
];

for (const [contextName, case1, case2] of cases) {
Expand Down Expand Up @@ -127,24 +187,28 @@ describe('double-declarations', () => {
expect(instance).not.toBeDefined();
});

it('respects inputs', () => {
MockRender(
`<override1
[prop1]="'prop1'"
[prop2alias]="'prop2alias'"
[override2alias]="'override2alias'"
[prop3alias]="'prop3alias'"
[override3alias]="'override3alias'"
[propBase1]="'propBase1'"
[propOverride1]="'propOverride1'"
></override1>`,
);
const instance = ngMocks.findInstance(OverrideCls);
expect(instance.prop1).toEqual('prop1');
expect(instance.prop2).toEqual('override2alias');
expect(instance.prop3).toEqual('override3alias');
expect(instance.propBase1).toEqual('propBase1');
expect(instance.propOverride1).toEqual('propOverride1');
describe('props', () => {
ngMocks.ignoreOnConsole('error');

it('respects inputs', () => {
MockRender(
`<override1
[prop1]="'prop1'"
[prop2alias]="'prop2alias'"
[override2alias]="'override2alias'"
[prop3alias]="'prop3alias'"
[override3alias]="'override3alias'"
[propBase1]="'propBase1'"
[propOverride1]="'propOverride1'"
></override1>`,
);
const instance = ngMocks.findInstance(OverrideCls);
expect(instance.prop1).toEqual('prop1');
expect(instance.prop2).toEqual('override2alias');
expect(instance.prop3).toEqual('override3alias');
expect(instance.propBase1).toEqual('propBase1');
expect(instance.propOverride1).toEqual('propOverride1');
});
});

it('respects outputs', () => {
Expand Down
9 changes: 9 additions & 0 deletions libs/ng-mocks/src/lib/common/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ const applyProps = (instance: Mock & Record<keyof any, any>, prototype: AnyType<

export type ngMocksMockConfig = {
config?: IMockBuilderConfig;
hostBindings?: string[];
hostListeners?: string[];
init?: (instance: any) => void;
isControlValueAccessor?: boolean;
isValidator?: boolean;
Expand Down Expand Up @@ -152,6 +154,13 @@ export class Mock {
for (const key of this.__ngMocksConfig.queryScanKeys || /* istanbul ignore next */ []) {
coreDefineProperty(this, `__ngMocksVcr_${key}`, undefined);
}
for (const key of this.__ngMocksConfig.hostBindings || /* istanbul ignore next */ []) {
helperMockService.mock(this, key, 'get');
helperMockService.mock(this, key, 'set');
}
for (const key of this.__ngMocksConfig.hostListeners || /* istanbul ignore next */ []) {
helperMockService.mock(this, key);
}

// istanbul ignore else
if (funcIsMock(this)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const factory = (propName: string) => helperMockService.mockFunction(`console.${
export default (...methods: Array<keyof typeof console>): void => {
const backup: Array<[keyof typeof console, any]> = [];

beforeAll(() => {
beforeEach(() => {
if (methods.indexOf('log') === -1) {
methods.push('log');
}
Expand All @@ -18,7 +18,7 @@ export default (...methods: Array<keyof typeof console>): void => {
}
});

afterAll(() => {
afterEach(() => {
for (const [method, implementation] of backup) {
console[method] = implementation;
}
Expand Down
11 changes: 11 additions & 0 deletions libs/ng-mocks/src/lib/mock/decorate-declaration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// tslint:disable cyclomatic-complexity

import { Component, Directive, HostBinding, HostListener, Provider, ViewChild } from '@angular/core';

import { AnyType } from '../common/core.types';
Expand Down Expand Up @@ -62,11 +64,20 @@ export default <T extends Component | Directive>(
decorateOutputs(mock, meta.outputs);
config.queryScanKeys = decorateQueries(mock, meta.queries);

config.hostBindings = [];
for (const [key, ...args] of meta.hostBindings || []) {
HostBinding(...args)(mock.prototype, key);
if (config.hostBindings.indexOf(key) === -1) {
config.hostBindings.push(key);
}
}

config.hostListeners = [];
for (const [key, ...args] of meta.hostListeners || []) {
HostListener(...args)(mock.prototype, key);
if (config.hostListeners.indexOf(key) === -1) {
config.hostListeners.push(key);
}
}

return options;
Expand Down
8 changes: 4 additions & 4 deletions libs/ng-mocks/src/lib/resolve/collect-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ const parse = (def: any): any => {
};
}
} else if (ngMetadataName === 'HostBinding') {
const key = `[${decorator.args[0]}]`;
const key = `[${decorator.args?.[0] || prop}]`;
if (!declarations.host[key]) {
declarations.host[key] = prop;
}
declarations.hostBindings.push([prop, ...decorator.args]);
declarations.hostBindings.push([prop, ...(decorator.args || [])]);
} else if (ngMetadataName === 'HostListener') {
const key = `(${decorator.args[0]})`;
const key = `(${decorator.args?.[0] || prop})`;
if (!declarations.host[key]) {
declarations.host[key] = `${prop}($event)`;
}
declarations.hostListeners.push([prop, ...decorator.args]);
declarations.hostListeners.push([prop, ...(decorator.args || [])]);
}
}
}
Expand Down

0 comments on commit 5386f77

Please sign in to comment.