我有一个连接的组件,该组件从redux存储中获取数据,因此基本上,该组件呈现数据,没有要分派的动作。我正在使用antd渲染表,以便进行设置 垂直高度我正在计算高度并传递给参考。另外,我正在为此示例应用程序使用样式化的组件。
我如何使用笑话和酶进行测试。大多数文章和资源都显示了调度按钮操作并获取数据。
请参阅以下组件
table.js
import React, { useState, useEffect, useRef, useLayoutEffect } from "react";
import { Table, Alert, Icon } from "antd";
import { connect } from "react-redux";
import constants from "../constants";
import helpers from "../helpers";
import Style from "./style";
import hooks from "../hooks";
import Icon from "../DisabledIcon";
const {
column_headings: { top_view, front_view },
unique_keys: { TOP_VIEW },
} = constants;
const { transformRowData } = helpers;
const { TableContainer } = Style;
const { useWindowSize } = hooks;
const Table = props => {
const {
isTableDataLoading,
tableData,
selectedView,
headerHeight,
isTableError,
tableErrorMessage,
} = props;
const [formattedTableData, setFormattedTableData] = useState([]);
const [columnHeadings, setColumnHeadings] = useState([]);
const tableRef = useRef(null);
const size = useWindowSize();
const [tableHeight, setTableHeight] = useState(118);
useEffect(() => {
if (tableData && tableRef.current) {
let tableHead = document.querySelector(
`.${tableRef.current.classList[0]} table > thead`
);
const reducedPixel = tableHead.offsetHeight + headerHeight;
setTableHeight(reducedPixel);
}
}, [size, tableData, headerHeight]);
useEffect(() => {
if (tableData) {
let columnTitlesList = selectedView === TOP_VIEW ? top_view : front_view;
columnTitlesList = columnTitlesList.map(colTitle => {
if (colTitle.key === "disabled") {
return {
...colTitle,
render: (text, data) => {
return <Icon component={text && Icon} />;
},
};
}
return colTitle;
});
setColumnHeadings(columnTitlesList);
const transformedTableData = transformRowData(tableData.list, columnTitlesList);
setFormattedTableData(transformedTableData);
}
}, [tableData]);
return (
<>
{isTableError ? (
<Alert
message={<p style={{ textAlign: "center" }}>{tableErrorMessage}</p>}
type="error"
/>
) : (
<TableContainer ref={tableRef}>
<Table
columns={columnHeadings}
rowClassName={record => !record.isEnabled && "disabled-row"}
dataSource={formattedTableData}
pagination={false}
scroll={{ y: `calc(100vh - ${tableHeight}px)` }}
loading={isTableDataLoading}
/>
</TableContainer>
)}
</>
);
};
const mapStateToProps = ({ tableState }) => {
return {
isTableDataLoading: tableState.isLoading,
isTableError: tableState.isError,
tableErrorMessage: tableState.errorMessage,
tableData: tableState.payload,
selectedView: tableState.view,
};
};
export default connect(mapStateToProps, null)(Table);
并且我尝试编写基本测试,因为我使用的是redux thunk,所以我也应该在configureStore中通过中间件。
table.test.js
import React from "react";
import { shallow } from "enzyme";
import configureStore from "redux-mock-store";
import constants from "../constants";
import Table from "../Table";
import { Provider } from "react-redux";
const mockStore = configureStore([]);
const {
unique_keys: { TOP_VIEW },
} = constants;
describe("Table", () => {
let store;
let component;
beforeEach(() => {
store = mockStore({
isLoading: false,
isError: false,
errorMessage: null,
payload: null,
selectedView: TOP_VIEW,
});
component = shallow(
<Provider store={store}>
<Table />
</Provider>
);
});
it("should render the component", () => {
expect(component).toMatchSnapshot();
});
});
我们将不胜感激:)
答案 0 :(得分:0)
通常通过导出未连接版本的组件并为其编写测试来测试连接的组件。这使您可以控制什么数据以及何时提供给组件以及模拟操作。而且您不需要嘲笑商店。有关示例,请参见Redux docs。
但是,请注意,使用shallow rendering时,带有钩子的组件很难测试。