I'm trying to test a Slider component created with Material-UI, but I cannot get my tests to pass.
I would like test the the value changes using the fireEvent
with @testing-library/react
. I've been following this post to properly query the DOM, I cannot get the correct DOM nodes.
<Slider />
component
// @format
// @flow
import * as React from "react";
import styled from "styled-components";
import { Slider as MaterialUISlider } from "@material-ui/core";
import { withStyles, makeStyles } from "@material-ui/core/styles";
import { priceRange } from "../../../domain/Search/PriceRange/priceRange";
const Wrapper = styled.div`
width: 93%;
display: inline-block;
margin-left: 0.5em;
margin-right: 0.5em;
margin-bottom: 0.5em;
`;
// ommited code pertaining props and styles for simplicity
function Slider(props: SliderProps) {
const initialState = [1, 100];
const [value, setValue] = React.useState(initialState);
function onHandleChangeCommitted(e, latestValue) {
e.preventDefault();
const { onUpdate } = props;
const newPriceRange = priceRange(latestValue);
onUpdate(newPriceRange);
}
function onHandleChange(e, newValue) {
e.preventDefault();
setValue(newValue);
}
return (
<Wrapper
aria-label="range-slider"
>
<SliderWithStyles
aria-labelledby="range-slider"
defaultValue={initialState}
// getAriaLabel={index =>
// index === 0 ? "Minimum Price" : "Maximum Price"
// }
getAriaValueText={valueText}
onChange={onHandleChange}
onChangeCommitted={onHandleChangeCommitted}
valueLabelDisplay="auto"
value={value}
/>
</Wrapper>
);
}
export default Slider;
Slider.test.js
// @flow
import React from "react";
import { cleanup,
render,
getAllByAltText,
fireEvent,
waitForElement } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import Slider from "../Slider";
afterEach(cleanup);
describe("<Slider /> specs", () => {
// [NOTE]: Works, but maybe a better way to do it ?
xdescribe("<Slider /> component aria-label", () => {
it("renders without crashing", () => {
const { container } = render(<Slider />);
expect(container.firstChild).toBeInTheDocument();
});
});
// [ASK]: How to test the event handlers with fireEvent.
describe("<Slider /> props", () => {
it("display a initial min value of '1'", () => {
const renderResult = render(<Slider />);
// TODO
});
it("display a initial max value of '100'", () => {
const renderResult = render(<Slider />);
// TODO
});
xit("display to values via the onHandleChangeCommitted event when dragging stop", () => {
const renderResult = render(<Slider />);
console.log(renderResult)
// fireEvent.change(renderResult.getByText("1"))
// expect(onChange).toHaveBeenCalled(0);
});
// [NOTE]: Does not work, returns undefined
xit("display to values via the onHandleChange event when dragging stop", () => {
const renderResult = render(<Slider />);
console.log(renderResult.container);
const spanNodeWithAriaAttribute = renderResult.container.firstChild.getElementsByTagName("span")[0].getAttribute('aria-label')
expect(spanNodeWithAriaAttribute).toBe(/range-slider/)
});
});
// [ASK]: Works, but a snapshot is an overkill a better way of doing this ?
xdescribe("<Slider /> snapshot", () => {
it("renders without crashing", () => {
const { container } = render(<Slider />);
expect(container.firstChild).toMatchSnapshot();
});
});
});
fireEvent.change(getByTestId('slider'), { target: { value: 25 } });
Just this change. Thanks for this idea bdw. Much cleaner than above solutions – Opening