@muban/muban
Version:
Writing components for server-rendered HTML
51 lines (50 loc) • 1.85 kB
JavaScript
import getPropTypeInfo from '../../test-utils/propTypes';
function getMixedPropTypeInfo(name, targetElement, type, formData, sourceName = '') {
const formTarget = getPropTypeInfo({
name,
type,
source: {
name: sourceName,
target: targetElement,
type: 'form',
formData,
},
});
const inputTarget = getPropTypeInfo({
name,
type,
source: {
name: sourceName,
target: targetElement.querySelector(`[name='${formTarget.source.name}']`) ||
targetElement,
type: 'form',
formData,
},
});
return {
asForm: formTarget,
asInput: inputTarget,
};
}
/**
* For a given form return an object containg two PropTypeInfo, one where the
* target is the form itself and one where the target is the form's child input
*
* Useful for testing value extraction for a form and it's child inputs
* @param {string} name prop info name
* @param {Element} targetElement target element
* @param {string} sourceName to be used as source.name
* @param {boolean } formData to be used as source.formData
* @returns {FullPropTypeInfo}
*/
export function getFullPropTypeInfo(name, targetElement, sourceName, formData) {
const target = targetElement;
return {
number: getMixedPropTypeInfo(name, target, Number, formData, sourceName),
string: getMixedPropTypeInfo(name, target, String, formData, sourceName),
boolean: getMixedPropTypeInfo(name, target, Boolean, formData, sourceName),
date: getMixedPropTypeInfo(name, target, Date, formData, sourceName),
array: getMixedPropTypeInfo(name, target, Array, formData, sourceName),
object: getMixedPropTypeInfo(name, target, Object, formData, sourceName),
};
}