Skip to content

Commit

Permalink
test(zone.js): add tests for inherited method patchingThis change add…
Browse files Browse the repository at this point in the history
…s tests for when patching a child class with and without an overriden method.
  • Loading branch information
edusperoni committed Nov 25, 2020
1 parent d811768 commit 8bbab0d
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions packages/zone.js/test/common/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,126 @@ describe('utils', function() {
expect(desc!.writable).toBeTruthy();
expect(!desc!.get).toBeTruthy();
});

it('should patch target if it overrides a patched method', () => {
let args: any[]|undefined;
let childArgs: any[]|undefined;
let self: any;
let childSelf: any;
class Type {
method(..._args: any[]) {
args = _args;
self = this;
return 'OK';
}
}
class ChildType extends Type {
method(..._args: any[]) {
childArgs = _args;
childSelf = this;
return 'ChildOK';
}
}

const method = Type.prototype.method;
const childMethod = ChildType.prototype.method;
let delegateMethod: Function;
let delegateSymbol: string;
let childDelegateMethod: Function;
let childDelegateSymbol: string;

const typeInstance = new Type();
const childTypeInstance = new ChildType();
expect(patchMethod(
Type.prototype, 'method',
(delegate: Function, symbol: string, name: string) => {
expect(name).toEqual('method');
delegateMethod = delegate;
delegateSymbol = symbol;
return function(self, args) {
return delegate.apply(self, ['patch', args[0]]);
};
}))
.toBe(delegateMethod!);

expect(patchMethod(
ChildType.prototype, 'method',
(delegate: Function, symbol: string, name: string) => {
expect(name).toEqual('method');
childDelegateMethod = delegate;
childDelegateSymbol = symbol;
return function(self, args) {
return delegate.apply(self, ['child patch', args[0]]);
};
}))
.toBe(childDelegateMethod!);

expect(typeInstance.method('a0')).toEqual('OK');
expect(childTypeInstance.method('a0')).toEqual('ChildOK');
expect(args).toEqual(['patch', 'a0']);
expect(childArgs).toEqual(['child patch', 'a0']);
expect(self).toBe(typeInstance);
expect(childSelf).toBe(childTypeInstance);
expect(delegateMethod!).toBe(method);
expect(childDelegateMethod!).toBe(childMethod);
expect(delegateSymbol!).toEqual(zoneSymbol('method'));
expect(childDelegateSymbol!).toEqual(zoneSymbol('method'));
expect((Type.prototype as any)[delegateSymbol!]).toBe(method);
expect((ChildType.prototype as any)[delegateSymbol!]).toBe(childMethod);
});

it('should not patch target if does not override a patched method', () => {
let args: any[]|undefined;
let self: any;
class Type {
method(..._args: any[]) {
args = _args;
self = this;
return 'OK';
}
}
class ChildType extends Type {}
const method = Type.prototype.method;
let delegateMethod: Function;
let delegateSymbol: string;
let childPatched = false;

const typeInstance = new Type();
const childTypeInstance = new ChildType();
expect(patchMethod(
Type.prototype, 'method',
(delegate: Function, symbol: string, name: string) => {
expect(name).toEqual('method');
delegateMethod = delegate;
delegateSymbol = symbol;
return function(self, args) {
return delegate.apply(self, ['patch', args[0]]);
};
}))
.toBe(delegateMethod!);

expect(patchMethod(
ChildType.prototype, 'method',
(delegate: Function, symbol: string, name: string) => {
childPatched = true;
return function(self, args) {
return delegate.apply(self, ['child patch', args[0]]);
};
}))
.toBe(delegateMethod!);

expect(childPatched).toBe(false);
expect(typeInstance.method('a0')).toEqual('OK');
expect(args).toEqual(['patch', 'a0']);
expect(self).toBe(typeInstance);
expect(delegateMethod!).toBe(method);
expect(delegateSymbol!).toEqual(zoneSymbol('method'));
expect((Type.prototype as any)[delegateSymbol!]).toBe(method);
expect(childTypeInstance.method('a0')).toEqual('OK');
expect(args).toEqual(['patch', 'a0']);
expect(self).toBe(childTypeInstance);
expect((ChildType.prototype as any)[delegateSymbol!]).toBe(method);
});
});

describe('patchPrototype', () => {
Expand Down

0 comments on commit 8bbab0d

Please sign in to comment.