-
Notifications
You must be signed in to change notification settings - Fork 54
/
bindSpec.js
executable file
·204 lines (154 loc) · 5.95 KB
/
bindSpec.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
describe('bind', function () {
var alice = {
name: 'Al'
};
describe('fundamentals', function () {
var greet = function () {
return 'hi';
};
it('returns a function', function () {
var boundGreet = bind(greet, alice);
expect(boundGreet).to.be.a('function');
});
it('does not return the input function', function () {
var boundGreet = bind(greet, alice);
expect(boundGreet).not.to.equal(greet);
});
/* Warning: Advanced testing syntax
*
* This test is disabled because it relies on function spying, which is a powerful but complex
* testing tool. If you're ready to tackle this syntax, your code will benefit from more
* thorough testing.
*
* Uncomment the following test block if you would like to enable the test.
*/
/*
it('returns a function that proxies the original function', function () {
// setup
var original = greet;
greet = sinon.spy(greet);
// test
var boundGreet = bind(greet, alice);
expect(greet.callCount).to.equal(0);
var sentence = boundGreet();
expect(greet.callCount).to.equal(1);
// cleanup
greet = original;
});
*/
});
describe('operating on a simple function', function () {
var personalGreet = function () {
return 'this is ' + this.name;
};
it('runs the target function in the context of the target object, when invoked as a free function', function () {
var boundPersonalGreet = bind(personalGreet, alice);
var sentence = boundPersonalGreet();
expect(sentence).to.equal('this is Al');
});
it('runs the target function in the context of the target object, even when invoked as a method of some other object', function () {
var bob = {
name: 'Bobby'
};
bob.boundPersonalGreet = bind(personalGreet, alice);
var sentence = bob.boundPersonalGreet();
expect(sentence).to.equal('this is Al');
});
});
describe('operating on a function that takes an argument', function () {
var friendlyGreet = function (salutation) {
return salutation + ', ' + 'this is ' + this.name;
};
var boundFriendlyGreet = bind(friendlyGreet, alice);
it('passes explicit arguments along from the proxy function to the target function', function () {
var sentence = boundFriendlyGreet('howdy');
expect(sentence).to.equal('howdy, this is Al');
});
});
describe('operating on a function that takes dynamic arguments', function () {
var lawyerGreet = function (firstPartner, secondPartner /*, [...nthParner]*/ ) {
var names = Array.prototype.slice.call(arguments);
names[names.length - 1] = '& ' + names[names.length - 1];
return names.join(', ') + ', this is ' + this.name;
};
var boundLawyerGreet = bind(lawyerGreet, alice);
/* Warning: Advanced testing syntax
*
* Uncomment the following test block if you would like to enable the test.
*/
/*
it('calls the target function with the same number of arguments as were passed to the proxy function', function(){
// setup
var original = boundLawyerGreet;
boundLawyerGreet = sinon.spy(boundLawyerGreet);
// test
var sentence = boundLawyerGreet('Shelton', 'Maxwell', 'Eddies', 'Park');
expect(sentence).to.equal('Shelton, Maxwell, Eddies, & Park, this is Al');
expect(boundLawyerGreet.lastCall.args.length).to.equal(4);
// cleanup
boundLawyerGreet = original;
});
it('passes an arbitrarily large number of arguments along from the proxy function to the target function', function(){
// setup
var original = boundLawyerGreet;
boundLawyerGreet = sinon.spy(boundLawyerGreet);
// test
var names = ['Winston', 'Miller'];
while(0.05 < Math.random()){
var randomName = 'Nowell Hendrix Larson Gaffney Laub Reynolds Green Parker Wilmer Snell'.split(' ')[Math.floor(Math.random() * 10)];
names.push(randomName);
}
var sentence = boundLawyerGreet.apply({}, names);
expect(boundLawyerGreet.lastCall.args).to.eql(names);
// cleanup
boundLawyerGreet = original;
});
*/
});
describe('available as a method of any function', function () {
var personalGreet = function () {
return 'this is ' + this.name;
};
var boundPersonalGreet = personalGreet.bind(alice);
it('returns a function', function () {
expect(boundPersonalGreet).to.be.a('function');
});
it('does not return the input function', function () {
expect(boundPersonalGreet).not.to.equal(personalGreet);
});
/* Warning: Advanced testing syntax
*
* This test is disabled because it relies on function spying, which is a powerful but complex
* testing tool. If you're ready to tackle this syntax, your code will benefit from more
* thorough testing.
*
* Uncomment the following test block if you would like to enable the test.
*/
/*
it('returns a function that proxies the original function', function(){
// setup
var original = personalGreet;
personalGreet = sinon.spy(personalGreet);
// test
var boundPersonalGreet = bind(personalGreet, alice);
expect(personalGreet.callCount).to.equal(0);
var sentence = boundPersonalGreet();
expect(personalGreet.callCount).to.equal(1);
// cleanup
personalGreet = original;
});
*/
it('runs the target function in the context of the target object, when invoked as a free function', function () {
var sentence = boundPersonalGreet();
expect(sentence).to.equal('this is Al');
});
it('runs the target function in the context of the target object, even when invoked as a method of some other object', function () {
var bob = {
name: 'Bobby'
};
bob.boundPersonalGreet = boundPersonalGreet;
var sentence = bob.boundPersonalGreet();
expect(sentence).to.equal('this is Al');
});
});
});