This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
/
autobind.spec.js
293 lines (230 loc) · 6.74 KB
/
autobind.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { expect } from 'chai';
import autobind from '../../lib/autobind';
const root = (typeof window !== 'undefined') ? window : global;
describe('@autobind', function () {
let Foo;
let Bar;
let Car;
let barCount;
beforeEach(function () {
Foo = class Foo {
@autobind
getFoo() {
return this;
}
getFooAgain() {
return this;
}
@autobind
onlyOnFoo() {
return this;
}
}
barCount = 0;
Bar = class Bar extends Foo {
@autobind()
getFoo() {
const foo = super.getFoo();
barCount++;
return foo;
}
getSuperMethod_getFoo() {
return super.getFoo;
}
getSuperMethod_onlyOnFoo() {
return super.onlyOnFoo;
}
@autobind
onlyOnBar() {
return this;
}
}
Car = class Car extends Foo {
@autobind()
getCarFromFoo() {
return super.onlyOnFoo();
}
}
});
afterEach(function () {
Foo = null;
Bar = null;
barCount = null;
});
it('returns a bound instance for a method', function () {
const foo = new Foo();
const { getFoo } = foo;
getFoo().should.equal(foo);
});
it('sets the correct prototype descriptor options', function () {
const desc = Object.getOwnPropertyDescriptor(Foo.prototype, 'getFoo');
desc.configurable.should.equal(true);
desc.enumerable.should.equal(false);
});
it('sets the correct instance descriptor options when bound', function () {
const foo = new Foo();
const { getFoo } = foo;
const desc = Object.getOwnPropertyDescriptor(foo, 'getFoo');
desc.configurable.should.equal(true);
desc.enumerable.should.equal(false);
desc.writable.should.equal(true);
desc.value.should.equal(getFoo);
});
it('sets the correct instance descriptor options when reassigned outside', function () {
const noop = function () {};
const foo = new Foo();
const ret = foo.getFoo = noop;
const desc = Object.getOwnPropertyDescriptor(foo, 'getFoo');
ret.should.equal(noop);
desc.configurable.should.equal(true);
desc.enumerable.should.equal(true);
desc.writable.should.equal(true);
desc.value.should.equal(noop);
});
it('works with multiple instances of the same class', function () {
const foo1 = new Foo();
const foo2 = new Foo();
const getFoo1 = foo1.getFoo;
const getFoo2 = foo2.getFoo;
getFoo1().should.equal(foo1);
getFoo2().should.equal(foo2);
});
it('returns the same bound function every time', function () {
const foo = new Foo();
const bar = new Bar();
foo.getFoo.should.equal(foo.getFoo);
bar.getFoo.should.equal(bar.getFoo);
bar.getSuperMethod_getFoo().should.equal(bar.getSuperMethod_getFoo());
bar.getFooAgain().should.equal(bar.getFooAgain());
});
it('works with inheritance, super.method() being autobound as well', function () {
const bar = new Bar();
const car = new Car();
const getFooFromBar = bar.getFoo;
const getCarFromFoo = car.getCarFromFoo;
// Calling both forms more than once to catch
// bugs that only appear after first invocation
getFooFromBar().should.equal(bar);
getFooFromBar().should.equal(bar);
getCarFromFoo().should.equal(car);
getCarFromFoo().should.equal(car);
bar.getFoo().should.equal(bar);
bar.getFoo().should.equal(bar);
bar.getFooAgain().should.equal(bar);
const getSuperMethod_getFoo = bar.getSuperMethod_getFoo();
getSuperMethod_getFoo().should.equal(bar);
const onlyOnFoo = bar.getSuperMethod_onlyOnFoo();
onlyOnFoo().should.equal(bar);
barCount.should.equal(4);
});
it('throws when it needs WeakMap but it is not available', function () {
const WeakMap = root.WeakMap;
delete root.WeakMap;
const bar = new Bar();
(function () {
bar.getFoo();
}).should.throw(`Using @autobind on getFoo() requires WeakMap support due to its use of super.getFoo()
See https://github.com/jayphelps/core-decorators.js/issues/20`);
barCount.should.equal(0);
root.WeakMap = WeakMap;
});
it('does not override descriptor when accessed on the prototype', function () {
Bar.prototype.getFoo;
Bar.prototype.onlyOnBar;
const bar = new Bar();
const getFoo2 = bar.getFoo;
const onlyOnBar = bar.onlyOnBar;
getFoo2().should.equal(bar);
onlyOnBar().should.equal(bar);
barCount.should.equal(1);
// check Foo after Bar since it was inherited by Bar and might accidentally
// be bound to the instance of Foo above!
Foo.prototype.getFoo;
Foo.prototype.onlyOnFoo;
const foo = new Foo();
const getFoo1 = foo.getFoo;
const onlyOnFoo = foo.onlyOnFoo;
getFoo1().should.equal(foo);
onlyOnFoo().should.equal(foo);
});
it('can be used to autobind the entire class at once', function () {
// do not @autobind, which means start() should return `undefined` if
// it is detached from the instance
class Vehicle {
start() {
return this;
}
}
@autobind
class Car extends Vehicle {
constructor() {
super();
this.name = 'amazing';
}
get color() {
return 'red';
}
drive() {
return this;
}
stop() {
return this;
}
render() {
return this;
}
}
const originalRender = Car.prototype.render;
Car.prototype.render = function () {
return originalRender.apply(this, arguments);
};
Car.prototype.stop;
Car.prototype.color;
const car = new Car();
car.render().should.equal(car);
const { drive, stop, name, color, start } = car;
expect(drive()).to.equal(car);
drive().should.equal(car);
stop().should.equal(car);
name.should.equal('amazing');
color.should.equal('red');
// We didn't @autobind Vehicle
expect(start()).to.be.undefined;
});
it('correctly binds with multiple class prototype levels', function () {
@autobind
class A {
method() {
return this.test || 'WRONG ONE';
}
}
@autobind
class B extends A {}
@autobind
class C extends B {
test = 'hello';
method() {
return super.method();
}
}
const c = new C();
const { method } = c;
method().should.equal('hello');
const method2 = A.prototype.method;
method2.call({ test: 'first' }).should.equal('first');
const method3 = B.prototype.method;
method3.call({ test: 'second' }).should.equal('second');
});
it('correctly binds class with symbol properties', function () {
const parkHash = Symbol('park');
@autobind
class Car {
[parkHash]() {
return this;
}
}
const car = new Car();
const park = car[parkHash];
park().should.equal(car);
})
});