In our unit tests, we sometimes want to be able to use the ByTestId methods of the React Testing Library to find bits of DOM created by a Kendo component. However, setting a data-testid attribute (or really any data- attribute) on a Kendo component does not always result in the attribute appearing anywhere in the final DOM.
To demonstrate, below is a test suite (also included in the zip) where I tried putting a data-testid attribute on the Kendo components Input, TextArea, Checkbox, Slider, SliderLabel, MultiSelect, and DatePicker, then checked if the attribute appeared in the rendered DOM. The tests for Input, TextArea, and Checkbox are successful. The ones for Slider, SliderLabel, MultiSelect, and DatePicker are not.
import { render, screen } from '@testing-library/react';
import { Input, TextArea, Checkbox, Slider, SliderLabel } from '@progress/kendo-react-inputs';
import { MultiSelect } from '@progress/kendo-react-dropdowns';
import { DatePicker } from '@progress/kendo-react-dateinputs';
// SUCCEEDS
describe('Kendo Input', () => {
it('supports data attributes', () => {
render(
<Input data-testid="the-input" />
);
const input = screen.queryByTestId('the-input');
expect(input).toBeTruthy();
});
});
// SUCCEEDS
describe('Kendo TextArea', () => {
it('supports data attributes', () => {
render(
<TextArea data-testid="the-text-area" />
);
const textarea = screen.queryByTestId('the-text-area');
expect(textarea).toBeTruthy();
});
});
// SUCCEEDS
describe('Kendo Checkbox', () => {
it('supports data attributes', () => {
render(
<Checkbox label={"Chad"} data-testid="the-checkbox" />
);
const checkbox = screen.queryByTestId('the-checkbox');
expect(checkbox).toBeTruthy();
});
});
// FAILS
describe('Kendo Slider & SliderLabel', () => {
it('supports data attributes', () => {
render(
<Slider min={1} max={2} data-testid="slider-root">
<SliderLabel position={1} data-testid="slider-label-1">One Fish</SliderLabel>
<SliderLabel position={2} data-testid="slider-label-2">Two Fish</SliderLabel>
</Slider>
);
const sliderRoot = screen.queryByTestId('slider-root');
const sliderLabel1 = screen.queryByTestId('slider-label-1');
const sliderLabel2 = screen.queryByTestId('slider-label-2');
// test them all at once so we can know the full scope of our failure/success
expect({
sliderRoot,
sliderLabel1,
sliderLabel2
}).toMatchObject({
sliderRoot: expect.anything(), // don't be null or undefined
sliderLabel1: expect.anything(), // don't be null or undefined
sliderLabel2: expect.anything() // don't be null or undefined
});
});
});
// FAILS
describe('Kendo MultiSelect', () => {
it('supports data attributes', () => {
render(
<MultiSelect data-testid="the-multi-select" />
);
const multiselect = screen.queryByTestId('the-multi-select');
expect(multiselect).toBeTruthy();
});
});
// FAILS
describe('Kendo DatePicker', () => {
it('supports data attributes', () => {
render(
<DatePicker data-testid="the-date-picker" />
);
const datepicker = screen.queryByTestId('the-date-picker');
expect(datepicker).toBeTruthy();
});
});