Skip to content

Commit

Permalink
fixes developit#12 -- bug in "bind" decorator on super method call fr…
Browse files Browse the repository at this point in the history
…om subclass method
  • Loading branch information
vsevolodkulaga committed Sep 3, 2017
1 parent d6d3695 commit 8c45b58
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"modules": "umd",
"loose": "all",
"loose": false,
"compact": true,
"comments": false,
"stage": 0
Expand Down
12 changes: 7 additions & 5 deletions src/decko.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ let fns = {
configurable: true,
get() {
let value = fn.bind(this);
Object.defineProperty(this, key, {
value,
configurable: true,
writable: true
});
if (target.constructor === this.constructor) {
Object.defineProperty(this, key, {
value,
configurable: true,
writable: true
});
}
return value;
}
};
Expand Down
34 changes: 34 additions & 0 deletions tests/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,38 @@ describe('bind()', () => {

next();
});

it('should bind when used as a decorator for a class method that invokes a decorated super method', next => {
let aValue = 'a',
bValue = 'b';

class A {
@bind
f() {
return {value: aValue, this: this};
}
}

class B extends A {
@bind
f() {
let superResult = super.f();
return {value: bValue, superValue: superResult.value, this: this};
}
}

let b = new B(),
result;


// call twice to take into consideration effect of super method call
for (let i = 0; i < 2; i++) {
result = b.f();
expect(result.this).to.equal(b);
expect(result.value).to.equal(bValue);
expect(result.superValue).to.equal(aValue);
}

next();
});
});

0 comments on commit 8c45b58

Please sign in to comment.