suitest-js-api
Version:
Suitest is a test automation and device manipulation tool for living room devices and web browsers.
82 lines (72 loc) • 2.53 kB
JavaScript
/**
* Helper to make a composer for a method, that applies modifications to meta and returns new chain
* @param {symbol} type - type of the composer. It's useful for debugging and unit test. Otherwise it's not identifiable
* @param {Array<string>} names
* @param {Function} applyModifier
* @param {boolean} [configurable] - is field configurable
* @param {boolean} [writable] - is field writable
* @param {boolean} [enumerable] - is field enumerable
* @param {boolean} [registerClone] - should new chain be registered into unused expressions watcher
* @param {boolean} [unregisterParent] - should parent chain be removed from unused expressions watcher
*/
const makeModifierComposer = (type, names, applyModifier, {
configurable = false,
writable = false,
enumerable = true,
registerClone = true,
unregisterParent = true,
} = {}) => {
const composer = (classInstance, data, chain, makeChain) =>
names.reduce((acc, name) => ({
...acc,
[name]: {
value: (...args) => {
const newChain = makeChain(applyModifier(classInstance, data, ...args));
unregisterParent && classInstance.unusedExpressionWatchers.unregisterLeaf(chain);
registerClone && classInstance.unusedExpressionWatchers.registerLeaf(newChain, data.stack);
return newChain;
},
configurable,
writable,
enumerable,
},
}), {});
composer.composerType = type;
return composer;
};
/**
* Helper to make a composer for a method, that simply calls provided callback and returns appropriate values
* @param {symbol} type - type of the composer. It's useful for debugging and unit test. Otherwise it's not identifiable
* @param {Array<string>} names
* @param {Function} callback
* @param {boolean} [configurable] - is field configurable
* @param {boolean} [writable] - is field writable
* @param {boolean} [enumerable] - is field enumerable
* @param {boolean} [unregisterParent]
*/
const makeMethodComposer = (type, names, callback, {
configurable = false,
writable = false,
enumerable = true,
unregisterParent = false,
} = {}) => {
const composer = (classInstance, data, chain) =>
names.reduce((acc, name) => ({
...acc,
[name]: {
value: (...args) => {
unregisterParent && classInstance.unusedExpressionWatchers.unregisterLeaf(chain);
return callback(classInstance, data, ...args);
},
configurable,
writable,
enumerable,
},
}), {});
composer.composerType = type;
return composer;
};
module.exports = {
makeMethodComposer,
makeModifierComposer,
};