I want to test connected React component. I am using Jest, React Testing Library and redux-mock-store. When I'm trying to get some elements, I get an error:
TestingLibraryElementError: Unable to find an element with the text: Test. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
package.json
"redux-mock-store": "^1.5.4",
"typescript": "^3.9.7",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.0.4",
"@types/jest": "^26.0.13",
"@types/redux-mock-store": "^1.0.2",
"jest": "^26.4.2",
"ts-jest": "^26.3.0",
"react-redux": "5.0.7",
"react-router-dom": "^5.1.2",
"redux": "3.7.2",
Header.tsx
import React, { FunctionComponent, useEffect } from "react";
import { Box, Grid, Link } from "@material-ui/core";
import { AppState } from "../../redux/reducers/IndexReducer";
import { AppDispatch } from "../../redux/actions/Actions";
import { connect } from "react-redux";
import { getSettingsUi } from "../../redux/actions/SidebarActions";
import { SettingsUi } from "../../redux/reducers/SidebarMenuReducer";
interface Props {
settingsUi: SettingsUi;
}
export interface Events {
initUiSettings(): void;
}
const Header: FunctionComponent<Props & Events> = props => {
const { settingsUi } = props as Props;
const { initUiSettings } = props as Events;
useEffect(() => {
initUiSettings();
}, []);
return (
<Grid item className="header">
<Grid container alignItems="center" justify="space-between">
<Grid item>
<Link href="#">
<Box fontWeight={700} fontSize={18}>
{settingsUi && settingsUi.title}
</Box>
</Link>
<Box>{settingsUi && settingsUi.subtitle}</Box>
</Grid>
</Grid>
</Grid>
);
};
const mapStateToProps = (state: AppState): Props => ({
settingsUi: state.sidebar.settingsUi!,
});
const mapDispatchToProps = (dispatch: AppDispatch): Events => ({
initUiSettings: () => dispatch(getSettingsUi()),
});
export default React.memo(connect(mapStateToProps, mapDispatchToProps)(Header));
Header.test.tsx
import React from "react";
import { render } from "@testing-library/react";
import { Provider } from "react-redux";
import Header from "../../components/header/Header";
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const store = mockStore({
sidebar: { toggle: false },
settingsUi: { title: "Test" },
});
describe("<Header/>", () => {
it("test title", () => {
const { getByText } = render(
<Provider store={store}>
<Header />
</Provider>,
);
expect(getByText("Test")).toBeInTheDocument();
});
});
In your mapStateToProps
function the settingsUi
is a property of the sidebar
property.
const mapStateToProps = (state: AppState): Props => ({
settingsUi: state.sidebar.settingsUi!,
});
Either the mapStateToProps
is using the wrong selector or your mock store is wrong.
const mapStateToProps = (state: AppState): Props => ({
settingsUi: state.settingsUi!,
});
or
const store = mockStore({
sidebar: { toggle: false, settingsUi: { title: "Test" } }
});