-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
142 lines (93 loc) · 3.32 KB
/
test.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"use strict";
const customizeSymbol = require("./lib");
const {afterEach, beforeEach, describe, it} = require("mocha");
const {expect} = require("chai").use( require("sinon-chai") );
const {instanceMethods, instanceProperties, staticMethods, staticProperties} = require("./lib/props");
const {restore, stub} = require("sinon");
require("symbol.prototype.description/auto");
const instanceKeys = [...instanceMethods, ...instanceProperties];
const propertyString = key => typeof key === "symbol" ? key.description : `"${key}"`;
const removeExtendableKeys = keys => keys.filter(key => unextendableKeys.includes(key));
const removeUnextendableKeys = keys => keys.filter(key => !unextendableKeys.includes(key));
const staticKeys = [...staticMethods, ...staticProperties];
const symbolString = "description";
const unextendableKeys =
[
"constructor",
"length",
"toString",
"valueOf"
];
describe("Default behavior", () =>
{
it("is the same as the global", () =>
{
const IncompleteSymbol = customizeSymbol();
const symbol = IncompleteSymbol(symbolString);
instanceKeys.forEach(key => expect(symbol).to.have.property(key));
staticKeys.forEach(key => expect(IncompleteSymbol).to.have.property(key));
const args = ["arg1", "arg2"];
instanceMethods.forEach(method =>
{
stub(Symbol.prototype, method);
symbol[method](...args);
expect(Symbol.prototype[method]).to.have.been.calledWith(...args);
restore();
});
staticMethods.forEach(method =>
{
stub(Symbol, method);
IncompleteSymbol[method](...args);
expect(Symbol[method]).to.have.been.calledWith(...args);
restore();
});
});
});
describe("Unavailable properties/methods", () =>
{
const originalSymbol = Symbol;
beforeEach(() =>
{
const stubbedSymbol = customizeSymbol(
[
"description",
"for",
"hasInstance",
Symbol.toPrimitive
]);
stub(global, "Symbol").callsFake(stubbedSymbol);
});
afterEach(() => restore());
it("does not attempt to wrap them", () =>
{
const IncompleteSymbol = customizeSymbol();
const symbol = IncompleteSymbol(symbolString);
expect(IncompleteSymbol).to.not.have.property("for");
expect(IncompleteSymbol).to.not.have.property("hasInstance");
expect(symbol).to.not.have.property("description");
expect(symbol).to.not.have.property(originalSymbol.toPrimitive);
})
});
describe("Exclusions", () =>
{
removeUnextendableKeys(instanceKeys).forEach(key => it(propertyString(key), () =>
{
const IncompleteSymbol = customizeSymbol([key]);
const symbol = IncompleteSymbol(symbolString);
expect(symbol).to.not.have.property(key);
}));
removeUnextendableKeys(staticKeys).forEach(key => it(propertyString(key), () =>
{
const IncompleteSymbol = customizeSymbol([key]);
expect(IncompleteSymbol).to.not.have.property(key);
}));
it("all methods/properties", () =>
{
const IncompleteSymbol = customizeSymbol([...instanceKeys, ...staticKeys]);
const symbol = IncompleteSymbol(symbolString);
removeExtendableKeys(instanceKeys).forEach(key => expect(symbol).to.have.property(key));
removeExtendableKeys(staticKeys).forEach(key => expect(IncompleteSymbol).to.have.property(key));
removeUnextendableKeys(instanceKeys).forEach(key => expect(symbol).to.not.have.property(key));
removeUnextendableKeys(staticKeys).forEach(key => expect(IncompleteSymbol).to.not.have.property(key));
});
});