-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
101 lines (93 loc) · 4.06 KB
/
solution.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
// 69: Reflect - defineProperty (solution)
// 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 = 'defineProperty';
assert.equal(name in Reflect, true);
});
it('is of type `function`', function() {
const expectedType = 'function';
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 = new class {}
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, 'prop', {});
assert.equal('prop' in obj, true);
});
it('a number gets converted into a string', function() {
let obj = {};
Reflect.defineProperty(obj, 1, {});
assert.equal('1' in obj, true);
});
it('`undefined` also gets converted into a string (watch out!)', function() {
let obj = {};
let undef = void 0;
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, sym, {});
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', {value: 'property value'});
assert.equal(obj.prop, 'property value');
});
it('can be of any type (even itself)', function() {
let obj = {};
Reflect.defineProperty(obj, 'prop', {value: obj});
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(instance, 'prop', {value: void 0});
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, 'prop', []);
assert.equal(wasPropertyDefined, true);
});
});
// it doesn't work, see -> https://github.com/tddbin/katas/issues/25
describe('returns false', function() {
xit('when no property name is given (since no property has been added)', function() {
let instance = new class {};
const wasPropertyDefined = Reflect.defineProperty(instance, null, {});
console.log(wasPropertyDefined);
assert.equal(wasPropertyDefined, false);
});
xit('when no 3rd parameter, the descriptor is given', function() {
let instance = new class {};
const wasPropertyDefined = Reflect.defineProperty(instance, 'prop');
assert.equal(wasPropertyDefined, false);
});
});
});
});