Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
1
Indexable
Never
function getMockProps(props) {
	const listOfValue = Object.entries(props)
		.map((item) => {
			if (typeof item[1] !== 'function') {
				return `${item[0]}:{}`
			}
		})
		.filter((item) => item !== undefined)
	const listOfFunction = Object.entries(props)
		.map((item) => {
			if (typeof item[1] === 'function') {
				return `${item[0]}:()=>{}`
			}
		})
		.filter((item) => item !== undefined)
	const mockProps = [...listOfFunction, ...listOfValue]
	console.log(mockProps)
	return 0
}

function getFunction(props, component) {
	//test function in class
	// const listOfValue = Object.entries(props)
	// 	.map((item) => {
	// 		if (typeof item[1] !== 'function') {
	// 			return `${item[0]}:${item[1]}`
	// 		}
	// 	})
	// 	.filter((item) => item !== undefined)
	// const listOfFunction = Object.entries(props)
	// 	.map((item) => {
	// 		if (typeof item[1] === 'function') {
	// 			return `${item[0]}:()=>{}`
	// 		}
	// 	})
	// 	.filter((item) => item !== undefined)
	// const mockProps = [...listOfFunction, ...listOfValue]
	const instance = new component(props)
	const listOfFunctionClass = Object.getOwnPropertyNames(
		component.prototype
	).filter((item) => item !== 'constructor')
	const listOfArrowFunction = Object.entries(instance)
		.map((item, index) => {
			if (typeof item[1] === 'function') {
				return item[0]
			}
		})
		.filter((item) => item !== undefined)
	const listOfAllFunction = listOfFunctionClass.concat(listOfArrowFunction)
	const listOfFunctionTest = listOfAllFunction.map((item, index) => {
		const props = () => {
			if (item === 'UNSAFE_componentWillReceiveProps') {
				return 'props'
			} else if (item === 'componentDidUpdate') {
				return 'props,props'
			}
			return null
		}
		return `test('${item}', ()=>{ expect(instance.${item}(${
			props() === null ? '\'\',\'\',\'\',\'\'' : props()
		})).toMatchSnapshot()})`
	})
	console.log('list of function test', listOfFunctionTest)
	return 0
}

export { getFunction, getMockProps }