react-unit-test-generator
Version:
react-unit-test-generator is a helper for writing unit tests for React apps. Its main goal is to automatically generate a suite of the most common test cases for a given component.
36 lines (33 loc) • 1.18 kB
JavaScript
function formatInnerAttributes(formattedProps, key) {
if (typeof formattedProps[key] === 'function') {
return `${key}: jest.fn()`;
}
if (typeof formattedProps[key] === 'string' && formattedProps[key].startsWith('mock')) {
return `${key}: ${formattedProps[key]}`;
}
return `${key}: ${JSON.stringify(formattedProps[key])}`;
}
export function formatTemplateProps(props) {
let formattedProps = {};
Object.assign(formattedProps, props);
return Object.keys(formattedProps)
.map(key => {
if (typeof formattedProps[key] === 'function') {
return `
${key}={jest.fn()}`;
}
if (typeof formattedProps[key] === 'string' && formattedProps[key].startsWith('mock')) {
return `
${key}={${formattedProps[key]}}`;
}
if (typeof formattedProps[key] === 'object' && !Array.isArray(formattedProps[key])) {
return `
${key}={{${Object.keys(formattedProps[key])
.map(k => formatInnerAttributes(formattedProps[key], k))
.join(',')}}}`;
}
return `
${key}={${JSON.stringify(formattedProps[key])}}`;
})
.join('');
}