-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathbind.js
More file actions
71 lines (54 loc) · 1.24 KB
/
bind.js
File metadata and controls
71 lines (54 loc) · 1.24 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { bind } from '..';
import { expect } from 'chai';
/*global describe,it*/
describe('bind()', () => {
it('should bind when used as a simple decorator', next => {
let c = {
@bind
foo() {
return this;
}
};
expect(c.foo()).to.equal(c);
let p = c.foo;
expect(p()).to.equal(c);
let a = {};
expect(c.foo.call(a)).to.equal(c);
next();
});
it('should bind when used as a function', next => {
let ctx = {},
c = bind(function(){ return this; }, ctx);
expect(c()).to.equal(ctx);
let a = {};
expect(c.call(a)).to.equal(ctx);
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();
});
});