-
Notifications
You must be signed in to change notification settings - Fork 0
/
kata.js
99 lines (91 loc) · 3.84 KB
/
kata.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
// 69: Reflect - defineProperty
// To do: make all tests pass, leave the assert lines unchanged!
describe('`Reflect.defineProperty()` is like `Object.defineProperty()` but returns a Boolean.', function() {
describe('the function itself', function() {
it('is static on the `Reflect` object', function() {
const name = 'what`s the functions name again? :)';
assert.equal(name in Reflect, true);
});
it('is of type `function`', function() {
const expectedType = '';
assert.equal(typeof Reflect.defineProperty, expectedType)
});
});
describe('the 1st parameter is the object on which to define a property', function() {
it('fails if it is not an object', function() {
let noObj = {};
assert.throws(() => { Reflect.defineProperty(noObj, 'property', {value: 'value'}); }, TypeError);
});
it('accepts an object', function() {
let obj = '';
assert.doesNotThrow(() => { Reflect.defineProperty(obj, 'property', {value: 'value'}); });
});
it('accepts an instance (of a class)', function() {
let instance;
assert.doesNotThrow(() => { Reflect.defineProperty(instance, 'property', {value: 'value'}); });
});
});
describe('2nd parameter is the name of the property to be defined on the object (normally a string)', function() {
it('works with a `normal` string', function() {
let obj = {};
Reflect.defineProperty(obj, '', {});
assert.equal('prop' in obj, true);
});
it('a number gets converted into a string', function() {
let obj = {};
Reflect.defineProperty(obj, 2, {});
assert.equal('1' in obj, true);
});
it('`undefined` also gets converted into a string (watch out!)', function() {
let obj = {};
let undef = 1;
Reflect.defineProperty(obj, undef, {});
assert.equal('undefined' in obj, true);
});
it('it can be a symbol', function() {
let obj = {};
const sym = Symbol.for('prop');
Reflect.defineProperty(obj, 'prop', {});
assert.equal(sym in obj, true);
});
});
describe('the `value` is part of the 3rd parameter, given as a property in an object `{value: ...}`', function() {
// The entire complexity of the 3rd parameter might be covered in a later kata.
it('contains the initial value of the property, as an object in the property `value`', function() {
let obj = {};
Reflect.defineProperty(obj, 'prop');
assert.equal(obj.prop, 'property value');
});
it('can be of any type (even itself)', function() {
let obj = {};
Reflect.defineProperty(obj, 'prop');
assert.equal(obj.prop, obj);
});
});
describe('the return value of the function indicates wether the property was defined successfully', function() {
describe('returns true', function() {
it('when the property was created (which requires the 3rd parameter too!!!)', function() {
let instance = new class {};
const wasPropertyDefined = Reflect.defineProperty();
assert.equal(wasPropertyDefined, true);
});
it('no matter what the value of the property is (just the 3rd param has to exist as `{}`)', function() {
let instance = new class {};
const wasPropertyDefined = Reflect.defineProperty(instance);
assert.equal(wasPropertyDefined, true);
});
});
describe('returns false', function() {
it('when no property name is given (since no property has been added)', function() {
let instance = new class {};
const wasPropertyDefined = Reflect.defineProperty;
assert.equal(wasPropertyDefined, false);
});
it('when no 3rd parameter, the descriptor is given', function() {
let instance = new class {};
const wasPropertyDefined = Reflect.defineProperty(instance, 'prop', {value: 1});
assert.equal(wasPropertyDefined, false);
});
});
});
});