DEV Community

Discussion on: Testing a simple component with React Testing Library

Collapse
haaxor1689 profile image
Maroš Beťko • Edited on

I'm planning on getting into writing tests for a while now. Thanks for showing multiple options of this testing library. It looks pretty handy, I would liko to see it with typescript though. What I didn't like, was the data attribute related to tests in the component itself.

One thing though. I feel like the examples use way too much explicit types. Some examples like isInvalidWord: boolean or onAddClicked: MouseEventHandler<HTMLButtonElement> feel absolutely unnecessary. Also the useRef can be used much simpler.

const inputEl: RefObject<HTMLInputElement> | null = useRef(null);
// vs
const inputEl = useRef<HTMLInputElement | null>(null);
Enter fullscreen mode Exit fullscreen mode
Collapse
mbarzeev profile image
Matti Bar-Zeev Author

I hear ya.
I'm fairly new to TypeScript but not at all new to typed languages. I don't see declaring types as an optional choice. Either you're writing in a typed language or not. If you're using a typed language you should type anything which can be typed.
Nice simplification on the useRef() though ;)

Collapse
haaxor1689 profile image
Maroš Beťko • Edited on

Typescript should be a tool that helps you and not something that hinders you by wasting your time. Even languages like C++ now provide conveniences like auto type. By omitting some explicit types to let them be implicitly deduced or simplifying them you don't give up any type safety but save your time and make development faster.