How to test if component hast only the allowed props and uses only the allowed classes with Jest? - reactjs

I have a Button component that looks like this:
const Button = ({
children,
type,
btnStyles,
onClick,
}: any) => {
//...
}
export default Button
I wan't to make sure that the button is only used with the allowed props defined above. My test currently looks like this:
it('should only have the allowed props', () => {
const tree = shallow(
<Button
type='button'
buttonStyles='a-custom-button'
onClick={() => {
console.log('Custom Button Clicked')
}}
>
<i></i>
Button
</Button>
)
expect(tree.prop('type')).toBeDefined()
expect(tree.prop('buttonStyles')).toBeDefined()
})
I also want to make sure that only allowed class names can be used for buttonStyles. For example the button can only contain the classes .a-custom-button and .a-custom-button-styles.
How can I do this with Jest?

#Clarity is correct. You're using TypeScript so you could use a union of valid class names:
type ButtonProps = {
btnStyles: 'a-custom-button' | 'a-custom-button-styles';
}
const Button = ({
btnStyles,
}: ButtonProps) => {
console.log(btnStyles);
return null;
}
Button({ btnStyles: 'a-custom-button' }) // works
Button({ btnStyles: 'a-custom-button-styles' }) // also works
Button({ btnStyles: 'invalid-class-name' }) // doesn't work - invalid prop value in TypeScript
Playground:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAQgrsYB7AdgBQE5LAZygXigG8AoKKAI2BQGVQAbCHALigHIBDAWgGM4dkAWy4UEyFGygAfdtz4Ckw0YlRcBIRjjYBuEgF8SJHqgGwxqAlAAUpcuSq0GTADT7W8Feiy4AlAQB8xGTkxig4SIwAdPRIAOZWDnQaTD66dhgQwHAYKFAocPT0ugYkHuI2lNRJmqycvPxCIuYSUHp+dh0A9J1QAO5IGADWOKXNFYlOLLL1CkrNapOSbVDdUBz04X0Dw6Oe41WTtQCWKABu60cAJrz0HDg4XCgcghBL7XarJ+f0V1Bg3lBvnBoAAVcAQGg8DBHMDAKAQDBYDAkIA

Related

Observe (get sized) control (listen to events) over a nested component in the react and typescript application via the forwardRef function

I have a functional component called MyDivBlock
const MyDivBlock: FC<BoxProps> = ({ }) => {
{getting data...}
return (
<>
<div className='divBlock'>
{data.map((todo: { id: string; title: string }) =>
<div key={todo.id}>{todo.id} {todo.title} </div>)}
</div>
</>
);
};
I use it in such a way that MyDivBlock is nested as a child of
const App: NextPage = () => {
return (
<div>
<Box >
<MyDivBlock key="key0" areaText="DIV1" another="another"/>
</Box>
</div>
)
}
Note that MyDivBlock is nested in Box and MyDivBlock has no ref attribute. This is important because I need to write Box code with no additional requirements for my nested children. And anyone who will use my Box should not think about constraints and ref attributes.
Then I need to get the dimensions of MyDivBlock in the code of Box component, and later attach some event listeners to it, such as scrolling. These dimensions and listeners will be used in the Box component. I wanted to use Ref to control it. That is, the Box will later observe changes in the dimensions and events of MyDivBlock by creating a ref-reference to them
I know that this kind of parent-child relationship architecture is implemented through forwardRef
And here is the Box code:
import React, { forwardRef, useImperativeHandle, useRef } from 'react';
export interface BoxProps extends React.ComponentProps<any> {
children?: Element[];
className: string;
}
export const Box: React.FC<BoxProps> = ({ children, ...rest }: BoxProps): JSX.Element => {
const childRef = useRef<HTMLDivElement>();
const ChildWithForwardRef = forwardRef<HTMLDivElement>((props, _ref) => {
const methods = {
show() {
if (childRef.current) {
console.log("childRef.current is present...");
React.Children.forEach(children, function (item) {
console.log(item)})
console.log("offsetWidth = " + childRef.current.offsetWidth);
} else {
console.log("childRef.current is UNDEFINED");
}
},
};
useImperativeHandle(_ref, () => (methods));
return <div ref={childRef}> {children} </div>
});
ChildWithForwardRef.displayName = 'ChildWithForwardRef';
return (
<div
className={'BoxArea'}>
<button name="ChildComp" onClick={() => childRef.current.show()}>get Width</button>
<ChildWithForwardRef ref={childRef} />
</div>
);
}
export default Box;
The result of pressing the button:
childRef.current is present...
[...]
$$typeof: Symbol(react.element) key: "key0" props: {areaText: 'DIV1', another: 'another'}
[...] Object
offsetWidth = undefined
As you can see from the output, the component is visible through the created ref. I can even make several nested ones and get the same for all of them.
But the problem is that I don't have access to the offsetWidth and other properties.
The other challenge is how can I add the addEventListener?
Because it works in pure Javascript with their objects like Element, Document, Window or any other object that supports events, and I have ReactChildren objects.
Plus I'm using NextJS and TypeScript.
Didn't dive too deep into the problem, but this may be because you are passing the same childRef to both div inside ChildWithForwardRef and to ChildWithForwardRef itself. The latter overwrites the former, so you have the method .show from useImperativeHandle available but not offsetWidth. A quick fix is to rewrite ChildWithForwardRef to use its own ref:
const ChildWithForwardRef = forwardRef<HTMLDivElement>((props, _ref) => {
const ref = useRef<HTMLDivElement>()
const methods = {
show() {
if (ref.current) {
console.log("ref.current is present...");
React.Children.forEach(children, (item) => console.log(item))
console.log("offsetWidth = " + ref.current.offsetWidth);
} else {
console.log("ref.current is UNDEFINED");
}
},
};
useImperativeHandle(_ref, () => (methods));
// Here ref instead of childRef
return <div ref={ref}> {children} </div>
});
But really I don't quite get why you would need ChildWithForwardRef at all. The code is basically equivalent to this simpler version:
const Box: React.FC<BoxProps> = ({ children, ...rest }: BoxProps): JSX.Element => {
const childRef = useRef<HTMLDivElement>();
const showWidth = () => {
if(childRef.current) {
console.log("childRef.current is present...");
React.Children.forEach(children, item => console.log(item))
console.log("offsetWidth = " + childRef.current.offsetWidth);
} else {
console.log("childRef.current is UNDEFINED");
}
}
return (
<div className={'BoxArea'}>
<button name="ChildComp" onClick={showWidth}>get Width</button>
<div ref={childRef}>{children}</div>
</div>
);
}
You can't solve this completely with React. I solved it by wrapping the child component, making it take the form of the parent.

How can I fix this Unit Test?

I'm fairly new to unit testing my .tsx files and I am currently having trouble testing this (sorry if the format is off)
//this is Banner.tsx
import React, {useCallback} from "react";
type Properties = {
close: () => void;
text: string;
const Banner: React.FC<Properties> = ({close, text}) => {
const onClick = useCallback(() => {
close();},
[close, text]);
return (
<div className = "BannerBox">
<div className = "banner">
<span className = "popup"> onClick={onClick}[x]
</span>
{text}
</div>
</div>
);
};
export default Banner;
//this is App.tsx
import Banner from "./Components/Banner";
function App(): JSX.Element {
const [isOpen, setIsOpen]=useState(false);
const toggleBanner = () => {
SetIsOpen(!isOpen);
};
return (
<div>
<input type = "button"
value = "popup"
onClick={toggleBanner}/>
<p>hi</p>
{isOpen && <Banner text = {"hello"} close={() => isOpen(false)}/>}
</div>
export default App;
this is what i have so far
//Banner.test.tsx
test("Check that all type Properties are being used", () => {
render(<Banner />);
})
it gives this error -> "type {} is missing the following properties from type Banner: close and text"
"type {} is missing the following properties from type Banner: close and text"
Read this error message carefully.
Banner is a functional component. That means it's a function that that takes it's props as an object. And it's typed to receive two props, close and text. These props are required.
But you are providing no props in your test. Since the props argument is always an object, and you have no props, then the props argument is an empty object.
So now that error tells you that your function expects an object, but the one you provided is missing the close and text props.
You need to satisfy the required props of your component. Whether you are in a test or not, the contract of those types must must be fulfilled.
That means you want something like this:
//Banner.test.tsx
test("Check that all type Properties are being used", () => {
render(<Banner text="Hello, World!" close={() => null} />);
})
In additional there several syntax errors in your components. And your code will be much easier to understand if you use proper indenting to inform you of the structure of your code.

How to pass parameters in react functions with Typescript

Here is the structure of my folder:
src--|
|--ComponentsFolder1--Button.tsx
|--ComponentsFolder2--ParentDiv.tsx
My problem is as follows:
Button.tsx is a react component which calls a fetch function located in react ParentDiv.tsx with a few parameters:
type getNewVerseProps = {
getNewVerseFunc: (event: React.MouseEvent<HTMLDivElement>) => string;
};
const Button = ({ getNewVerseFunc }: getNewVerseProps) => {
return (
<div>
<div
onClick={getNewVerseFunc.bind(name, id)}>
</div>
</div>
Now, as you can see, I want to call the function on my ParentDiv.tsx file with the specified parameters in Button.tsx:
const getNewVerseFunc = async (name: string, id: string) => {
const requ = await fetch(
`https://api.scripture.api.bible/${name}/${id}`,
{
method: "GET",
headers: {
"api-key": bibleApi,
},
}
};
<Button getNewVerseFunc={getNewVerseFunc}/>
My problem is that the params in the function are not being passed down in the Button component when i call the getNewVerseFunc function in the ParentDiv component
First of all you've used bind wrongly. First argument is a context, which is in this case ignored, because you have an arrow function, which is instantly bound; but it is impossible to rebind bound function.
Common practice is to have button with onClick callback, like:
import { FunctionComponent } from 'react';
type Props {
onClick: MouseEventHandler;
}
const Button: FunctionComponent<Props> = ({ onClick, children }) => (
<div onClick={getNewVerseFunc.bind(name, id)}>
{children}
</div>
);
And for simplicity avoid using bind, call or apply if possible; it's good that you are aware of these functions, but using them usually reduces readability.
Then in parent you can do the following:
import { FunctionComponent } from 'react';
const ParentDiv: FunctionComponent = () => {
const name = 'Some name';
const id = 'Some id';
return (
<Button onClick={() => getNewVerseFunc(name, id)}>
Click me
</Button>
);
};
If your Button component is wrapped with memo, then it makes sense to use useCallback hook for onClick handler, but that's already micro optimisation.

Trying to apply a class to a styled-component for an onclick event

In using standard JSX and CSS, I can add a className attribute with some logic to add a class name based on a boolean value, but when using styled-components, this doesn't appear to be as easy. This is what I have at the moment:
Menu.tsx
interface IMenuProps {
showMenu: boolean;
menuToggle: () => void;
}
const Menu: React.FC<IMenuProps> = ({ showMenu, menuToggle }) => {
return (
<MenuWrapper onClick={menuToggle} {showMenu ? "showMenu" : ""}>
...
At the moment, there's a red line under the showMenu within the ternary statement.
'...' expected.
Hopefully you can see what I'm trying to do here.
You forgot to add className.
const Menu: React.FC<IMenuProps> = ({ showMenu, menuToggle }) => {
return (
<MenuWrapper onClick={menuToggle} className={showMenu ? "showMenu" : ""}>
BTW:
If there isn't any logic inside your component you can write it as
const Example: FC = () => (
<div>
<h1>Example</h1>
</div>
);
Have you written types for your styled MenuWrapper?
If not, try something like this:
const MenuWrapper = styled.div<{showMenu?:boolean}>

Method within React functional component

Hello I had to add a method inside a component, that was stateless functional component. Now I am wondering if it can stay like this or should it be a class component now. My component:
const Pagination = ({ changePage }) => {
function changePageNumber(event) {
changePage(event.currentTarget.dataset.num);
}
return (
<button
className={css.button}
data-num={num}
onClick={changePageNumber}
>
{num}
</button>
);
};
Can it be like this?
Yes, you can write methods like this. Also you can use arrow functions, like:
const Pagination = ({ changePage }) => {
const changePageNumber = event => {
changePage(event.currentTarget.dataset.num);
}
return (
<button
className={css.button}
data-num={num}
onClick={changePageNumber}
>
{num}
</button>
);
};
Bonus: It's not necessary to name component that exported default, just:
export default ({ changePage }) => {
...
}
And in another file:
import AnyName from './Pagination'
You can change it to be like
const changePageNumber = (event) = () => {
changePage(event.currentTarget.dataset.num);
}
const Pagination = ({ changePage }) => {
return (
<button
className={css.button}
data-num={num}
onClick={changePageNumber}
>
{num}
</button>
);
};
changePageNumber is a function not a method. It is perfectly fine to stay there. It is there as a utility/helper function for that Pagination component. Such functions can be deployed to improve the readability of the code. Current state of your code perfectly fine.
Also you don't need to turn it into Class components, we use them when we think we need to store state, not to store methods.

Resources