-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest.js
218 lines (169 loc) · 6.54 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import test from 'ava';
import mimicFunction from './index.js';
const foo = function (bar) {
return bar;
};
foo.unicorn = '🦄';
const symbol = Symbol('🦄');
foo[symbol] = '✨';
const parent = function () {};
parent.inheritedProp = true;
Object.setPrototypeOf(foo, parent);
test('should return the wrapped function', t => {
const wrapper = function () {};
const returnValue = mimicFunction(wrapper, foo);
t.is(returnValue, wrapper);
});
test('should copy `name`', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
t.is(wrapper.name, foo.name);
});
test('should copy other properties', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
t.is(wrapper.unicorn, foo.unicorn);
});
test('should copy symbol properties', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
t.is(wrapper[symbol], foo[symbol]);
});
test('should not copy `length`', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
t.is(wrapper.length, 0);
});
test('should keep descriptors', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
const {length: fooLength, toString: fooToString, ...fooProperties} = Object.getOwnPropertyDescriptors(foo);
const {length: wrapperLength, toString: wrapperToString, ...wrapperProperties} = Object.getOwnPropertyDescriptors(wrapper);
t.deepEqual(fooProperties, wrapperProperties);
t.not(fooLength, wrapperLength);
t.not(fooToString, wrapperToString);
});
test('should copy inherited properties', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
t.is(wrapper.inheritedProp, foo.inheritedProp);
});
test('should not delete extra configurable properties', t => {
const wrapper = function () {};
wrapper.extra = true;
mimicFunction(wrapper, foo);
t.true(wrapper.extra);
});
test('should not copy prototypes', t => {
const wrapper = function () {};
const prototype = {};
wrapper.prototype = prototype;
mimicFunction(wrapper, foo);
t.is(wrapper.prototype, prototype);
});
test('should allow classes to be copied', t => {
class wrapperClass {}
class fooClass {}
mimicFunction(wrapperClass, fooClass);
t.is(wrapperClass.name, fooClass.name);
t.not(wrapperClass.prototype, fooClass.prototype);
});
test('should patch toString()', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
t.is(wrapper.toString(), `/* Wrapped with wrapper() */\n${foo.toString()}`);
});
test('should patch toString() with arrow functions', t => {
const wrapper = function () {};
const arrowFunction = value => value;
mimicFunction(wrapper, arrowFunction);
t.is(wrapper.toString(), `/* Wrapped with wrapper() */\n${arrowFunction.toString()}`);
});
test('should patch toString() with bound functions', t => {
const wrapper = function () {};
const boundFunction = (() => {}).bind();
mimicFunction(wrapper, boundFunction);
t.is(wrapper.toString(), `/* Wrapped with wrapper() */\n${boundFunction.toString()}`);
});
test('should patch toString() with new Function()', t => {
const wrapper = function () {};
// eslint-disable-next-line no-new-func
const newFunction = new Function('');
mimicFunction(wrapper, newFunction);
t.is(wrapper.toString(), `/* Wrapped with wrapper() */\n${newFunction.toString()}`);
});
test('should patch toString() several times', t => {
const wrapper = function () {};
const wrapperTwo = function () {};
mimicFunction(wrapper, foo);
mimicFunction(wrapperTwo, wrapper);
t.is(wrapperTwo.toString(), `/* Wrapped with wrapperTwo() */\n/* Wrapped with wrapper() */\n${foo.toString()}`);
});
test('should keep toString() non-enumerable', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
const {enumerable} = Object.getOwnPropertyDescriptor(wrapper, 'toString');
t.false(enumerable);
});
test('should print original function with Function.prototype.toString.call()', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
t.is(Function.prototype.toString.call(wrapper), 'function () {}');
});
test('should work with String()', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
t.is(String(wrapper), `/* Wrapped with wrapper() */\n${foo.toString()}`);
});
test('should not modify toString.name', t => {
const wrapper = function () {};
mimicFunction(wrapper, foo);
t.is(wrapper.toString.name, 'toString');
});
test('should work when toString() was patched by original function', t => {
const wrapper = function () {};
const bar = function () {};
bar.toString = () => 'bar.toString()';
mimicFunction(wrapper, bar);
t.is(wrapper.toString(), `/* Wrapped with wrapper() */\n${bar.toString()}`);
});
// eslint-disable-next-line max-params
const configurableTest = (t, shouldThrow, ignoreNonConfigurable, toDescriptor, fromDescriptor) => {
const wrapper = function () {};
Object.defineProperty(wrapper, 'conf', {
value: true,
configurable: false,
writable: true,
enumerable: true,
...toDescriptor,
});
const bar = function () {};
Object.defineProperty(bar, 'conf', {
value: true,
configurable: false,
writable: true,
enumerable: true,
...fromDescriptor,
});
if (shouldThrow) {
t.throws(() => {
mimicFunction(wrapper, bar, {ignoreNonConfigurable});
});
} else {
t.notThrows(() => {
mimicFunction(wrapper, bar, {ignoreNonConfigurable});
});
}
};
configurableTest.title = title => `should handle non-configurable properties: ${title}`;
test('not throw with no changes', configurableTest, false, false, {}, {});
test('not throw with writable value change', configurableTest, false, false, {}, {value: false});
test('throw with non-writable value change', configurableTest, true, false, {writable: false}, {value: false, writable: false});
test('not throw with non-writable value change and ignoreNonConfigurable', configurableTest, false, true, {writable: false}, {value: false, writable: false});
test('throw with configurable change', configurableTest, true, false, {}, {configurable: true});
test('not throw with configurable change and ignoreNonConfigurable', configurableTest, false, true, {}, {configurable: true});
test('throw with writable change', configurableTest, true, false, {writable: false}, {writable: true});
test('not throw with writable change and ignoreNonConfigurable', configurableTest, false, true, {writable: false}, {writable: true});
test('throw with enumerable change', configurableTest, true, false, {}, {enumerable: false});
test('not throw with enumerable change and ignoreNonConfigurable', configurableTest, false, true, {}, {enumerable: false});
test('default ignoreNonConfigurable to false', configurableTest, true, undefined, {}, {enumerable: false});