Skip to content

Commit

Permalink
fix: conserve props from original method (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
bertho-zero authored Feb 14, 2020
1 parent df1f7ff commit 9a77e81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/hooks/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,16 @@ export const functionHooks = <F, T = any>(original: F, opts: HookSettings<T>) =>
registerContextUpdater(wrapper, updateContext);
registerMiddleware(wrapper, middleware);

const originalProps = (Object.keys(original) as any)
.concat(Object.getOwnPropertySymbols(original));

for (const prop of originalProps) {
const propDescriptor = Object.getOwnPropertyDescriptor(original, prop);

if (!wrapper.hasOwnProperty(prop)) {
Object.defineProperty(wrapper, prop, propDescriptor);
}
}

return Object.assign(wrapper, { original, collect });
};
18 changes: 18 additions & 0 deletions packages/hooks/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,22 @@ describe('functionHooks', () => {
assert.equal(result, 'Hi Bertho!');
assert.equal(called, 1);
});

it('conserves method properties', async () => {
const TEST = Symbol('test');
const hello = (name: any) => `Hi ${name}`;
(hello as any)[TEST] = true;

const sayHi = hooks(hello, [
async (context, next) => {
await next();
context.result += '!';
}
]);

const result = await sayHi('Bertho');

assert.equal(result, 'Hi Bertho!');
assert.equal((sayHi as any)[TEST], (hello as any)[TEST]);
});
});

0 comments on commit 9a77e81

Please sign in to comment.