deno.land / x / rambda@v9.1.1 / source / bind.spec.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { bind } from './bind.js'
function Foo(x){ this.x = x}function add(x){ return this.x + x}function Bar(x, y){ this.x = x this.y = y}Bar.prototype = new Foo()Bar.prototype.getX = function (){ return 'prototype getX'}
test('returns a function', () => { expect(typeof bind(add)(Foo)).toBe('function')})
test('returns a function bound to the specified context object', () => { const f = new Foo(12) function isFoo(){ return this instanceof Foo } const isFooBound = bind(isFoo, f) expect(isFoo()).toBeFalse() expect(isFooBound()).toBeTrue()})
test('works with built-in types', () => { const abc = bind(String.prototype.toLowerCase, 'ABCDEFG') expect(typeof abc).toBe('function') expect(abc()).toBe('abcdefg')})
test('works with user-defined types', () => { const f = new Foo(12) function getX(){ return this.x } const getXFooBound = bind(getX, f) expect(getXFooBound()).toBe(12)})
test('works with plain objects', () => { const pojso = { x : 100 } function incThis(){ return this.x + 1 } const incPojso = bind(incThis, pojso) expect(typeof incPojso).toBe('function') expect(incPojso()).toBe(101)})
test('does not interfere with existing object methods', () => { const b = new Bar('a', 'b') function getX(){ return this.x } const getXBarBound = bind(getX, b) expect(b.getX()).toBe('prototype getX') expect(getXBarBound()).toBe('a')})
test('preserves arity', () => { const f0 = function (){ return 0 } const f1 = function (a){ return a } const f2 = function (a, b){ return a + b } const f3 = function ( a, b, c ){ return a + b + c }
expect(bind(f0, {})).toHaveLength(0) expect(bind(f1, {})).toHaveLength(1) expect(bind(f2, {})).toHaveLength(2) expect(bind(f3, {})).toHaveLength(3)})
rambda

Version Info

Tagged at
2 months ago