rx-player
Version:
Canal+ HTML5 Video Player
37 lines (36 loc) • 1.51 kB
JavaScript
/**
* Util useful to create mocked versions of classes with each of their methods
* and properties replaced, while being properly checked by TypeScript.
* @param {Object} methods - Each of this class' methods (key is the method
* name, value is the implementation).
* @param {Object} properties - Each of this class' property (key is the
* property name, value is the implementation).
* @returns {*} - The mocked class, which should respect its public API.
*/
export function makeMockedClass(methods, properties) {
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class Dummy {
constructor(opts = {}) {
// Apply default property values
for (const key in properties) {
if (Object.prototype.hasOwnProperty.call(properties, key)) {
// @ts-expect-error dynamic assignment
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this[key] = properties[key];
}
}
// Override with any provided opts
for (const key in opts) {
if (Object.prototype.hasOwnProperty.call(opts, key)) {
// @ts-expect-error dynamic assignment
this[key] = opts[key];
}
}
}
}
for (const [name, fn] of Object.entries(methods)) {
// @ts-expect-error dynamic assignment
Dummy.prototype[name] = fn;
}
return Dummy;
}