Skip to content

Commit

Permalink
feat(aroundmethod): added original method to meta for arround hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mjancarik committed Jul 23, 2020
1 parent c9868ec commit f83eae1
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
67 changes: 67 additions & 0 deletions src/__tests__/__snapshots__/aopForMethodsSpec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`for class class method should call pattern.aroundMethod 1`] = `
Array [
Object {
"args": Array [
Object {},
1,
],
"context": A {
"variable": "method",
},
"object": Object {
"_privateMethod": [Function],
"method": [Function],
"superMethod": [Function],
},
"original": [Function],
"property": "method",
"target": [Function],
},
]
`;

exports[`for class class method should call pattern.aroundMethod for super.superMethod 1`] = `
Array [
Object {
"args": Array [],
"context": C {
"map": Map {},
"variable": "super method",
},
"object": Object {
"_privateMethod": [Function],
"method": [Function],
"method2": [Function],
"superMethod": [Function],
},
"original": [Function],
"property": "superMethod",
"target": [Function],
},
]
`;

exports[`for class class method should call pattern.arroundMethod for extended class with multiple aspect 1`] = `
Array [
Object {
"args": Array [
Object {},
1,
],
"context": C {
"map": Map {},
"variable": "method",
},
"object": Object {
"_privateMethod": [Function],
"method": [Function],
"method2": [Function],
"superMethod": [Function],
},
"original": [Function],
"property": "method2",
"target": [Function],
},
]
`;

exports[`for class class method should call pattern.beforeMethod and pattern.afterMethod 1`] = `
Array [
Object {
Expand Down
42 changes: 41 additions & 1 deletion src/__tests__/aopForMethodsSpec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import aopForMethods from '../aopForMethods';
import { hookName } from '../hook';
import { hookName, createHook } from '../hook';
import createClasses from './createClasses';
import createPattern from './createPattern';

describe('for class', () => {
let pattern = null;
let pattern2 = null;
let arroundPattern = null;

describe('class method', () => {
let beforeMethod = null;
let afterMethod = null;
let aroundMethod = null;

beforeEach(() => {
beforeMethod = jest.fn();
afterMethod = jest.fn();
aroundMethod = jest.fn();

pattern = createPattern(undefined, {
[hookName.beforeMethod]: beforeMethod,
Expand All @@ -23,6 +26,18 @@ describe('for class', () => {
[hookName.beforeMethod]: beforeMethod,
[hookName.afterMethod]: afterMethod,
});
arroundPattern = createHook(hookName.aroundMethod, /.*/, aroundMethod);
});

it('should call pattern.aroundMethod', () => {
let { A } = createClasses();

aopForMethods(A, arroundPattern);
new A('method').method({}, 1);

expect(aroundMethod.mock.calls.length).toEqual(1);
expect(aroundMethod.mock.calls[0][0].original).not.toEqual(aroundMethod);
expect(aroundMethod.mock.calls[0]).toMatchSnapshot();
});

it('should call pattern.beforeMethod and pattern.afterMethod', () => {
Expand Down Expand Up @@ -51,6 +66,18 @@ describe('for class', () => {
expect(afterMethod.mock.calls[0]).toMatchSnapshot();
});

it('should call pattern.arroundMethod for extended class with multiple aspect', () => {
let { A, C } = createClasses();

aopForMethods(A, arroundPattern);
aopForMethods(C, arroundPattern);
new C('method').method2({}, 1);

expect(aroundMethod.mock.calls.length).toEqual(1);
expect(aroundMethod.mock.calls[0][0].original).not.toEqual(aroundMethod);
expect(aroundMethod.mock.calls[0]).toMatchSnapshot();
});

it('should call pattern.beforeMethod and pattern.afterMethod for extended class', () => {
let { B } = createClasses();

Expand Down Expand Up @@ -118,6 +145,19 @@ describe('for class', () => {
expect(afterMethod.mock.calls.length).toEqual(1);
expect(afterMethod.mock.calls[0]).toMatchSnapshot();
});

it('should call pattern.aroundMethod for super.superMethod', () => {
let { C } = createClasses();

aopForMethods(C, arroundPattern);
const c = new C('super method');

expect(c.superMethod()).toEqual(undefined);

expect(aroundMethod.mock.calls.length).toEqual(1);
expect(aroundMethod.mock.calls[0][0].original).not.toEqual(aroundMethod);
expect(aroundMethod.mock.calls[0]).toMatchSnapshot();
});
});

describe('es5 problems', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/trap/createCallTrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export default function createCallTrap({
property,
context: context || self,
args: rest,
original:
typeof object[property] === 'function' ? object[property] : method,
});
} else {
if (typeof object[property] === 'function') {
Expand Down

0 comments on commit f83eae1

Please sign in to comment.