Skip to content

Commit

Permalink
Conserve the name of the original class.
Browse files Browse the repository at this point in the history
```js
const B= Autobind(A, true);
console.log(B.name) //--> 'A';

```
  • Loading branch information
abdennour committed Dec 5, 2016
1 parent e57caca commit 2ac13e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "babel-autobind",
"version": "0.1.0",
"version": "0.2.0",
"description": "it binds methods to its class prototype + Compatible with stub/spy used on unit-test frameworks (Sinon.Js, enzyme,...so on) + Can be integrated with 3rd party decorators (like @autobind decorator)",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -30,7 +30,8 @@
"addon",
"unit-test",
"expect",
"autobind-decorator"
"autobind-decorator",
"enzyme"
],
"author": "Abdennour TOUMI <http://abdennoor.com>",
"license": "MIT",
Expand Down
13 changes: 12 additions & 1 deletion src/index.js
@@ -1,4 +1,4 @@
export function Autobind(Mocked) {
export function Autobind(Mocked, sameName) {
const methods = Object.getOwnPropertyNames(Mocked.prototype);
const methodsExcluded = ['constructor', 'render'];
const ruleIncluder = (methodName =>
Expand All @@ -14,5 +14,16 @@ export function Autobind(Mocked) {
});
}
}
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;
};
8 changes: 8 additions & 0 deletions test/Spec.js
Expand Up @@ -66,4 +66,12 @@ describe(`babel-autobind`, () => {
wrapper.instance().handleClick({});
}).toNotThrow();
});

it(`rename the bound class to be the same as the name of the original class`, () => {
let Target = Autobind(ComponentWithoutBinding);
expect(Target.name).toNotEqual('ComponentWithoutBinding');
Target = Autobind(ComponentWithoutBinding, true);
expect(Target.name).toEqual('ComponentWithoutBinding');
});

});
7 changes: 0 additions & 7 deletions test/examples.js

This file was deleted.

0 comments on commit 2ac13e0

Please sign in to comment.