-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path68-reflect-construct.js
83 lines (70 loc) · 2.9 KB
/
68-reflect-construct.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
// 68: Reflect - construct
// To do: make all tests pass, leave the assert lines unchanged!
describe('`Reflect.construct` is the `new` operator as a function', function() {
describe('the function itself', function() {
it('is static on the `Reflect` object', function() {
const name = 'construct';
assert.equal(name in Reflect, true);
});
it('is of type `function`', function() {
const expectedType = 'function';
assert.equal(typeof Reflect.construct, expectedType)
});
});
describe('the 1st parameter is the constructor to be invoked', function() {
it('fails when given a number as constructor', function() {
let aNumber = Number();
assert.throws(() => { Reflect.construct(aNumber, []) }, TypeError);
});
it('works giving a function', function() {
let aFunction = () => {};
assert.doesNotThrow(() => { Reflect.construct(aFunction, []) });
});
it('works giving a class', function() {
const aClass = class {};
assert.doesNotThrow(() => { Reflect.construct(aClass, []) });
});
});
describe('the 2nd parameter is a list of arguments, that will be passed to the constructor', function() {
const aClass = class {};
it('fails when it`s not an array(-like), e.g. a number', function() {
let aNumber = Number();
assert.throws(() => { Reflect.construct(aClass, aNumber) }, TypeError);
});
it('works with an array-like object (the `length` property look up should not throw)', function() {
let arrayLike = {get length() { }};
assert.doesNotThrow(() => { Reflect.construct(aClass, arrayLike) });
});
it('works with a real array', function() {
let realArray = [];
assert.doesNotThrow(() => { Reflect.construct(aClass, realArray) });
});
});
describe('in use', function() {
it('giving it a class it returns an instance of this class', function() {
class Constructable {}
let instance = Reflect.construct(Constructable, []);
assert.equal(instance instanceof Constructable, true);
});
describe('the list of arguments are passed to the constructor as given', function() {
class Constructable {
constructor(...args) { this.args = args; }
}
it('if none given, nothing is passed', function() {
let instance = Reflect.construct(Constructable, []);
assert.deepEqual(instance.args, []);
});
it('passing an array, all args of any type are passed', function() {
const argumentsList = ['arg1', ['arg2.1', 'arg2.2'], {arg: 3}];
let instance = Reflect.construct(Constructable, argumentsList);
assert.deepEqual(instance.args, argumentsList);
});
});
});
describe('the length property', function() {
it('of `Reflect.construct` is 2', function() {
let expected = 2;
assert.equal(Reflect.construct.length, expected);
});
});
});