Skip to content

Commit

Permalink
fix(ngMocks.stubMember): forwarding stub values to point.componentIns…
Browse files Browse the repository at this point in the history
…tance #1165
  • Loading branch information
satanTime committed Mar 26, 2022
1 parent 866c941 commit 3450e1d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
17 changes: 15 additions & 2 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.stub-member.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
export default <T extends object>(instance: T, key: any, value: any, encapsulation?: 'get' | 'set'): any => {
const def = Object.getOwnPropertyDescriptor(instance, key) || {};
import helperExtractPropertyDescriptor from '../mock-service/helper.extract-property-descriptor';

export default <T extends object>(
instance: T & { __ngMocks__source?: object },
key: any,
value: any,
encapsulation?: 'get' | 'set',
): any => {
const def = helperExtractPropertyDescriptor(instance, key) ?? {};

if (!encapsulation && def.set && (def.set as any).__ngMocksProxy) {
def.set(value);

return value;
}

const descriptor: PropertyDescriptor = {
configurable: true,
Expand Down
17 changes: 13 additions & 4 deletions libs/ng-mocks/src/lib/mock-render/func.install-prop-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import coreDefineProperty from '../common/core.define-property';
import helperDefinePropertyDescriptor from '../mock-service/helper.define-property-descriptor';
import helperMockService from '../mock-service/helper.mock-service';

const createPropertyGet =
(key: keyof any & string, reader: Record<keyof any, any>, source: Record<keyof any, any>) => () => {
const createPropertyGet = (key: keyof any & string, reader: Record<keyof any, any>, source: Record<keyof any, any>) => {
const handler = () => {
if (typeof source[key] === 'function') {
if (reader[`__ngMocks_${key}__origin`] !== source[key]) {
const clone = helperMockService.createClone(source[key], reader, source);
Expand All @@ -16,9 +16,13 @@ const createPropertyGet =

return source[key];
};
coreDefineProperty(handler, '__ngMocksProxy', true, false);

const createPropertySet =
(key: keyof any & string, reader: Record<keyof any, any>, source: Record<keyof any, any>) => (newValue: any) => {
return handler;
};

const createPropertySet = (key: keyof any & string, reader: Record<keyof any, any>, source: Record<keyof any, any>) => {
const handler = (newValue: any) => {
if (reader[`__ngMocks_${key}`]) {
reader[`__ngMocks_${key}`] = undefined;
}
Expand All @@ -27,6 +31,10 @@ const createPropertySet =
}
source[key] = newValue;
};
coreDefineProperty(handler, '__ngMocksProxy', true, false);

return handler;
};

const extractAllKeys = (instance: object) => [
...helperMockService.extractPropertiesFromPrototype(Object.getPrototypeOf(instance)),
Expand All @@ -45,6 +53,7 @@ export default (
if (!source) {
return;
}
coreDefineProperty(reader, '__ngMocks__source', source, false);
const exists = extractOwnKeys(reader);
const fields = [...extractAllKeys(source), ...extra];
for (const key of fields) {
Expand Down
44 changes: 44 additions & 0 deletions tests/issue-1165/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Component } from '@angular/core';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Component({
selector: 'target',
template: `{{ value }}`,
})
class TargetComponent {
private valueOrigin = 0;

public get value(): number {
return this.valueOrigin;
}

public set value(value: number) {
this.valueOrigin = value;
}
}

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

it('sets values via proxy', () => {
const fixture = MockRender(TargetComponent);
expect(ngMocks.formatText(fixture)).toEqual('0');

fixture.componentInstance.value = 1;
fixture.detectChanges();
expect(ngMocks.formatText(fixture)).toEqual('1');

fixture.point.componentInstance.value = 2;
fixture.detectChanges();
expect(ngMocks.formatText(fixture)).toEqual('2');

ngMocks.stubMember(fixture.componentInstance, 'value', 3);
fixture.detectChanges();
expect(ngMocks.formatText(fixture)).toEqual('3');

ngMocks.stubMember(fixture.point.componentInstance, 'value', 4);
fixture.detectChanges();
expect(ngMocks.formatText(fixture)).toEqual('4');
});
});

0 comments on commit 3450e1d

Please sign in to comment.