I have a component that I need to pass the state into like this, so the component can change the state in its parent. I am just using React Testing Library without Enzyme but with Jest.
const [open, setOpen] = useState<boolean>(false);
<Toggle value={open} setValue={setOpen} onText='Open' offtext='Closed' />
And the component itself
import React, { Dispatch, SetStateAction } from 'react';
interface PropsShape {
value: boolean;
setValue: Dispatch<SetStateAction<boolean>>;
onText: string;
offtext: string;
}
function Toggle({ value, setValue, onText, offtext }: PropsShape) {
return (
<div className='Toggle'>
<div
className='Toggle--slider'
onClick={() => setValue(!value)}
data-testid='click'
>
<div className={value ? 'on' : 'off'}></div>
</div>
<div className='Toggle--label'>{value ? onText : offtext}</div>
</div>
);
}
export { Toggle };
And here's the kind of test I'm trying to do
import { useState } from 'react';
import { render, fireEvent, screen } from '@testing-library/react';
import { Toggle } from '../components/toggle';
test('The user can see the closed text after the toggle has been clicked', async () => {
const [open, setOpen] = useState<boolean>(false); // CANT DO THIS
render(
<Toggle value={open} setValue={setOpen} onText='Open' offtext='Closed' />
);
fireEvent.click(screen.getByTestId('click'));
expect(await screen.queryByText('Closed')).toBeVisible();
});
How do I set value and setValue in the test so they can be changed when the toggle is clicked? I have tried adding const [open, setOpen] = useState<boolean>(false); but hooks are not allowed?