mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-02 15:59:38 +08:00
55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
|
export function spyElementPrototypes(Element, properties) {
|
||
|
const propNames = Object.keys(properties);
|
||
|
const originDescriptors = {};
|
||
|
|
||
|
propNames.forEach(propName => {
|
||
|
const originDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, propName);
|
||
|
originDescriptors[propName] = originDescriptor;
|
||
|
|
||
|
const spyProp = properties[propName];
|
||
|
|
||
|
if (typeof spyProp === 'function') {
|
||
|
// If is a function
|
||
|
Element.prototype[propName] = function spyFunc(...args) {
|
||
|
return spyProp.call(this, originDescriptor, ...args);
|
||
|
};
|
||
|
} else {
|
||
|
// Otherwise tread as a property
|
||
|
Object.defineProperty(Element.prototype, propName, {
|
||
|
...spyProp,
|
||
|
set(value) {
|
||
|
if (spyProp.set) {
|
||
|
return spyProp.set.call(this, originDescriptor, value);
|
||
|
}
|
||
|
return originDescriptor.set(value);
|
||
|
},
|
||
|
get() {
|
||
|
if (spyProp.get) {
|
||
|
return spyProp.get.call(this, originDescriptor);
|
||
|
}
|
||
|
return originDescriptor.get();
|
||
|
},
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return {
|
||
|
mockRestore() {
|
||
|
propNames.forEach(propName => {
|
||
|
const originDescriptor = originDescriptors[propName];
|
||
|
if (typeof originDescriptor === 'function') {
|
||
|
Element.prototype[propName] = originDescriptor;
|
||
|
} else {
|
||
|
Object.defineProperty(Element.prototype, propName, originDescriptor);
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export function spyElementPrototype(Element, propName, property) {
|
||
|
return spyElementPrototypes(Element, {
|
||
|
[propName]: property,
|
||
|
});
|
||
|
}
|