domHook.js
3.13 KB
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
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/* eslint-disable no-param-reassign */
var NO_EXIST = {
__NOT_EXIST: true
};
export function spyElementPrototypes(elementClass, properties) {
var propNames = Object.keys(properties);
var originDescriptors = {};
propNames.forEach(function (propName) {
var originDescriptor = Object.getOwnPropertyDescriptor(elementClass.prototype, propName);
originDescriptors[propName] = originDescriptor || NO_EXIST;
var spyProp = properties[propName];
if (typeof spyProp === 'function') {
// If is a function
elementClass.prototype[propName] = function spyFunc() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return spyProp.call.apply(spyProp, [this, originDescriptor].concat(args));
};
} else {
// Otherwise tread as a property
Object.defineProperty(elementClass.prototype, propName, _objectSpread(_objectSpread({}, spyProp), {}, {
set: function set(value) {
if (spyProp.set) {
return spyProp.set.call(this, originDescriptor, value);
}
return originDescriptor.set(value);
},
get: function get() {
if (spyProp.get) {
return spyProp.get.call(this, originDescriptor);
}
return originDescriptor.get();
},
configurable: true
}));
}
});
return {
mockRestore: function mockRestore() {
propNames.forEach(function (propName) {
var originDescriptor = originDescriptors[propName];
if (originDescriptor === NO_EXIST) {
delete elementClass.prototype[propName];
} else if (typeof originDescriptor === 'function') {
elementClass.prototype[propName] = originDescriptor;
} else {
Object.defineProperty(elementClass.prototype, propName, originDescriptor);
}
});
}
};
}
export function spyElementPrototype(Element, propName, property) {
return spyElementPrototypes(Element, _defineProperty({}, propName, property));
}
/* eslint-enable */