-
Notifications
You must be signed in to change notification settings - Fork 0
/
kata.js
76 lines (65 loc) · 2.21 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
// 67: object-literal - setter
// To do: make all tests pass, leave the assert lines unchanged!
describe('An object literal can also contain setters', () => {
describe('defining: a setter', function() {
it('by prefixing the property with `set` (and make it a function)', function() {
let theX = null;
const obj = {
x(newX) { theX = newX; }
};
obj.x = 'the new X';
assert.equal(theX, 'the new X');
});
it('must have exactly one parameter', function() {
let setterCalledWith = void 0;
const obj = {
x() { // <<<<=== it's not a setter yet!
if (arguments.length === 1) {
setterCalledWith = arguments[0];
}
}
};
assert.equal(obj.x = 'new value', setterCalledWith);
});
it('can be a computed property (an expression enclosed in `[]`)', function() {
const publicPropertyName = 'x';
const privatePropertyName = '_' + publicPropertyName;
const obj = {
[privatePropertyName]: null
// write the complete setter to make the assert below pass :)
};
obj.x = 'axe';
assert.equal(obj._x, 'axe');
});
});
describe('working with/on the setter', function() {
it('you can use `delete` to remove the property (including it`s setter)', function() {
let setterCalled = false;
const obj = {
set x(param) { setterCalled = true; }
};
// delete the property x here, to make the test pass
obj.x = true;
assert.equal(setterCalled, false);
});
});
// TODO
// The following dont seem to work in the current transpiler version
// but should be correct, as stated here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set
// It might be corrected later, new knowledge welcome.
// it('must not overlap with a pure property', function() {
// const obj = {
// x: 1,
// set x(val) { return 'ax'; }
// };
// assert.equal(obj.x, 'ax');
// });
// it('multiple `set` for the same property are not allowed', function() {
// const obj = {
// x: 1,
// set x(v) { return 'ax'; },
// set x(v) { return 'ax1'; }
// };
// assert.equal(obj.x, 'ax');
// });
});