This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdd-day14.js
109 lines (99 loc) · 3.9 KB
/
tdd-day14.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
// Day - 14. Task 1
// http://tddbin.com/#?kata=es6/language/symbol/basics
// 34: symbol - basics
// A symbol is a unique and immutable data type and may be used as an identifier for object properties
// read more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
// To do: make all tests pass, leave the assert lines unchanged!
// Follow the hints of the failure messages!
describe('Symbol', function() {
it('`Symbol` lives in the global scope', function(){
const expected = Symbol;
assert.equal(Symbol, expected);
});
it('every `Symbol()` is unique', function(){
const sym1 = Symbol();
const sym2 = Symbol();
assert.notEqual(sym1, sym2);
});
it('every `Symbol()` is unique, also with the same parameter', function(){
var sym1 = Symbol('foo');
var sym2 = Symbol('foo');
assert.notEqual(sym1, sym2);
});
it('`typeof Symbol()` returns "symbol"', function(){
const theType = typeof (Symbol());
assert.equal(theType, 'symbol');
});
it('`new Symbol()` throws an exception, to prevent creation of Symbol wrapper objects', function(){
function fn() {
new Symbol();
}
assert.throws(fn);
});
});
// Day - 14. Task 2
// http://tddbin.com/#?kata=es6/language/symbol/for
// 35: Symbol.for - retrieves or creates a runtime-wide symbol
// To do: make all tests pass, leave the assert lines unchanged!
// Follow the hints of the failure messages!
describe('`Symbol.for` for registering Symbols globally', function() {
it('creates a new symbol (check via `typeof`)', function() {
const symbolType = typeof(Symbol());
assert.equal(symbolType, 'symbol');
});
it('stores the symbol in a runtime-wide registry and retrieves it from there', function() {
const sym = Symbol.for('new symbol');
const sym1 = Symbol.for('new symbol');
assert.equal(sym, sym1);
});
it('is different to `Symbol()` which creates a symbol every time and does not store it', function() {
var globalSymbol = Symbol.for('new symbol');
var localSymbol = Symbol('new symbol');
assert.notEqual(globalSymbol, localSymbol);
});
describe('`.toString()` on a Symbol', function() {
it('also contains the key given to `Symbol.for()`', function() {
const description = Symbol('new symbol').toString();
assert.equal(description, 'Symbol(new symbol)');
});
describe('NOTE: the description of two different symbols', function() {
it('might be the same', function() {
const symbol1AsString = Symbol('new symbol').toString();
const symbol2AsString = Symbol.for('new symbol').toString();
assert.equal(symbol1AsString, symbol2AsString);
});
it('but the symbols are not the same!', function() {
const symbol1 = Symbol('new symbol');
const symbol2 = Symbol('new symbol');
assert.notEqual(symbol1, symbol2);
});
});
});
});
// Day - 14. Task 3
// http://tddbin.com/#?kata=es6/language/symbol/keyFor
// 36: Symbol.keyFor - retrieves a shared symbol key from the global symbol registry
// To do: make all tests pass, leave the assert lines unchanged!
// Follow the hints of the failure messages!
describe('`Symbol.keyFor()` gets the symbol key for a given symbol', function() {
it('pass the symbol to `keyFor()` and you get its key', function() {
const key = Symbol.keyFor(Symbol.for('foo'));
assert.equal(key, 'foo');
});
it('local symbols are not in the runtime-wide registry', function() {
// Hint: `Symbol()` creates a local symbol!
const localSymbol = Symbol('foo');
const key = Symbol.keyFor(localSymbol);
assert.equal(key, void 0);
});
it('predefined symbols are not in the runtime-wide registry either', function() {
const key = Symbol.keyFor(Symbol());
assert.equal(key, void 0);
});
it('for non-Symbols throws an error', function() {
function fn() {
Symbol.keyFor(Symbol.for);
}
assert.throws(fn);
});
});