diff --git a/test/utils/createComponent.tsx b/test/utils/createComponent.tsx new file mode 100644 index 0000000..d37e9e0 --- /dev/null +++ b/test/utils/createComponent.tsx @@ -0,0 +1,126 @@ +import * as React from "react"; + +/** + * Crate single function component with optional name + * @param name Optional name + */ +function createFunctionComponent(name?: string): React.FunctionComponent { + const UnnamedFunctionComponent: React.FunctionComponent = function(props) { + if (props.children !== undefined) { + return {props.children}; + } else { + return null + } + }; + + if (name !== undefined) { + Object.defineProperty(UnnamedFunctionComponent, "name", { value: name }); + UnnamedFunctionComponent.displayName = name; + } + + return UnnamedFunctionComponent; +} + +/** + * Crate single class component with optional name + * @param name Optional name + */ +function createClassComponent(name?: string): React.ComponentClass { + const UnnamedClassComponent: React.ComponentClass = class extends React.Component { + render() { + if (this.props.children !== undefined) { + return this.props.children; + } else { + return null + } + } + }; + + if (name !== undefined) { + Object.defineProperty(UnnamedClassComponent, "name", { value: name }); + UnnamedClassComponent.displayName = name; + } + + return UnnamedClassComponent; +} + +/** + * Create n number of functionalComponents + * given either count or array of names + */ +function createFunctionComponents( + count: number +): Array; +function createFunctionComponents( + names: Array +): Array; +function createFunctionComponents( + countOrNames: number | Array = 1 +): Array { + if (typeof countOrNames === "number") { + const returnArr: Array = []; + for (let index = 0; index < countOrNames; index++) { + const newComp = createFunctionComponent(); + returnArr.push(newComp); + } + + return returnArr; + } else if (Array.isArray(countOrNames)) { + const returnArr: Array = []; + for (let index = 0; index < countOrNames.length; index++) { + const compName = countOrNames[index]; + + const newComp = createFunctionComponent(compName); + + returnArr.push(newComp); + } + + // Handle other invalid inputs + return returnArr; + } + + throw new TypeError("Input is not valid"); +} + +/** + * Create n number of classComponents + * given either count or array of names + */ +function createClassComponents(count: number): Array; +function createClassComponents( + names: Array +): Array; +function createClassComponents( + countOrNames: number | Array = 1 +): Array { + if (typeof countOrNames === "number") { + const returnArr: Array = []; + for (let index = 0; index < countOrNames; index++) { + const newComp = createClassComponent(); + returnArr.push(newComp); + } + + return returnArr; + } else if (Array.isArray(countOrNames)) { + const returnArr: Array = []; + for (let index = 0; index < countOrNames.length; index++) { + const compName = countOrNames[index]; + + const newComp = createClassComponent(compName); + + returnArr.push(newComp); + } + + return returnArr; + } + + // Handle other invalid inputs + throw new TypeError("Input is not valid"); +} + +export { + createFunctionComponent, + createFunctionComponents, + createClassComponent, + createClassComponents +};