-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.test.js
More file actions
46 lines (40 loc) · 938 Bytes
/
bind.test.js
File metadata and controls
46 lines (40 loc) · 938 Bytes
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
import { bind } from './bind'
describe('Test bind', () => {
it('bind object', () => {
const obj = {
name: 1,
getName() {
return this.name
}
}
expect(obj.getName()).toBe(1)
const obj2 = {
name: 2
}
expect(bind(obj.getName, obj2)()).toBe(2)
})
it('bind with args', () => {
const obj = {
name: 'foo',
greet(greeting) {
return greeting + this.name
}
}
expect(obj.greet('hi,')).toBe('hi,foo')
const obj2 = {
name: 'bar'
}
expect(bind(obj.greet, obj2)('hi,')).toBe('hi,bar')
expect(bind(obj.greet, obj2, 'Hi,')()).toBe('Hi,bar')
})
it('bind with constructor', () => {
function Foo() {
this.name = 'inner name'
}
Foo.prototype.greet = function () {
return this.name
}
const BoundFoo = bind(Foo, { name: 'bound name' })
expect(new BoundFoo().greet()).toBe('inner name')
})
})