【问题标题】:Testing connected components with jest and react-testing-library使用 jest 和 react-testing-library 测试连接的组件
【发布时间】:2020-10-09 03:26:48
【问题描述】:

我想测试连接的 React 组件。我正在使用 Jest、React 测试库和 redux-mock-store。当我尝试获取一些元素时,出现错误:

TestingLibraryElementError:无法找到包含以下文本的元素:Test。这可能是因为文本被多个元素分解。在这种情况下,你可以为你的文本匹配器提供一个函数,让你的匹配器更加灵活。

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();
    });
});

【问题讨论】:

  • 您正在从 state.sidebar.settingsUi 检索标题,但您的 mockStore 正在为 sidebarsettingsUi 使用单独的属性。

标签: reactjs react-redux jestjs react-testing-library redux-mock-store


【解决方案1】:

在您的 mapStateToProps 函数中,settingsUisidebar 属性的一个属性。

const mapStateToProps = (state: AppState): Props => ({
    settingsUi: state.sidebar.settingsUi!,
});

mapStateToProps 使用了错误的选择器,或者您的模拟存储错误。

const mapStateToProps = (state: AppState): Props => ({
    settingsUi: state.settingsUi!,
});

const store = mockStore({
    sidebar: { toggle: false, settingsUi: { title: "Test" } }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-11
    • 2020-03-22
    • 2020-05-14
    • 2021-02-28
    • 2021-05-25
    • 2019-06-14
    • 2020-01-20
    • 1970-01-01
    相关资源
    最近更新 更多