I'm using ReactJS and Material UI and Typescript. I want to test my menu - should show after I click on button with label Location. New menu should contain Location 1 item.
describe('<LocationsMenu />', () => {
let component: RenderResult;
beforeEach(() => {
component = render(<LocationsMenu />);
});
it('should show and hide on click on top item trigger', async () => {
const button = component.getByText('Locations').parentElement;
await act(async () => {
fireEvent.click(button);
});
expect(component.getByText('Location 1')).toBeDefined();
});
});
This works - test passes.
Visual Studio Code shows me error in fireEvent.click(button) line: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element | Node | Document | Window'.. How can I avoid it? I know I can do type casting like:
fireEvent.click(button as Element);
or
const button = component.getByText('Locations').parentElement as Element;
But maybe there is a better solution.