-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
44 lines (37 loc) · 1.58 KB
/
test.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var expect = require('chai').expect
, rebind = require('./')
describe('rebind', function() {
it("source function always has source as context", function() {
var target = {}, source = {method: function() { that = this }}, that
rebind(target, source, "method")
expect((target.method(), that)).to.be.eql(source)
expect((target.method.call({}), that)).to.be.eql(source)
})
it("source function receives target function's arguments", function() {
var target = {}, source = {method: function() { those = Array.prototype.slice.call(arguments) }}, those
rebind(target, source, "method")
expect((target.method(), those)).to.be.eql([])
expect((target.method(1), those)).to.be.eql([1])
expect((target.method(null), those)).to.be.eql([null])
expect((target.method(source, source, 1), those)).to.be.eql([source, source, 1])
})
it("target function returns target if source function returns source", function() {
var target = {}, source = {method: function(value) { return value ? source : 42 }}
rebind(target, source, "method")
expect(target.method(true)).to.be.eql(target)
expect(target.method(false)).to.be.eql(42)
})
it("can bind multiple methods", function() {
var target = {}, source = {
foo: function() { return 1 }
, bar: function() { return 2 }
}
rebind(target, source, "foo", "bar")
expect(target.foo()).to.be.eql(1)
expect(target.bar()).to.be.eql(2)
})
it("returns the target object", function() {
var target = {}, source = { foo: String }
expect(rebind(target, source, "foo")).to.be.eql(target)
})
})