-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
29 lines (28 loc) · 885 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
export function Autobind(Mocked, sameName) {
const methods = Object.getOwnPropertyNames(Mocked.prototype);
const methodsExcluded = ['constructor', 'render'];
const ruleIncluder = (methodName =>
!methodsExcluded.includes(methodName) &&
(typeof Object.getOwnPropertyDescriptor(Mocked.prototype, methodName).set === 'undefined')
);
class Mocker extends Mocked {
constructor() {
super(...arguments);
methods.filter(ruleIncluder).forEach((methodName) => {
this[methodName] = this[methodName].bind(this);
});
}
}
if(sameName) {
const rename = (() => {
Object.defineProperty(Mocker, 'name', {
writable: true
});
Mocker.name = (Mocked.name && Mocked.name.length) ? Mocked.name : 'Component';
Object.defineProperty(Mocker, 'name', {
writable: false
});
})();
}
return Mocker;
};