【发布时间】:2021-12-14 01:23:13
【问题描述】:
我是反应测试的新手,我正在尝试使用此代码验证反应功能组件是否存在,但由于某种原因它抱怨其他东西 TypeError: getItems [ props.action ] is not a function
对此有任何帮助/建议,以验证组件是否存在?
interface SelectionListProps {
actionArgs?: string | undefined;
action: string;
name?: string;
identifier?: string;
classes: {
button_basic: string;
formControl: string;
selectionCard: string;
};
}
export const SelectionList: React.FC<SelectionListProps> = (
props
) => {
const dispatch = useDispatch();
const getItems = {
analysers: (site: any) => dispatch(getAnalysers(site)),
sites: (site: any) => dispatch(getSites()),
} as any;
const initializeCollapses = () => {
};
useEffect(() => {
getItems[props.action](props.actionArgs).then(() => {
initializeCollapses();
});
}, []);
return (
<List component="div" data-testid="SelectionListt">
</List>
);
};
我的测试代码:
import React from "react";
import { expect } from "@jest/globals";
import { render, screen, cleanup, fireEvent } from "@testing-library/react";
import { SelectionList } from "../SelectionList";
import { Provider } from "react-redux";
import { store } from "../../redux/store";
import "@testing-library/jest-dom/extend-expect";
function handleUpdateClick(event: any, type = "") {}
test("test", () => {
render(
<Provider store={store}>
<SelectionList
classes={{ button_basic: "", formControl: "", selectionCard: "" }}
action={""}
actionArgs={""}
/>
</Provider>
);
const SelectionCardElement = screen.getByTestId("SelectionListt");
expect(SelectionCardElement).toBeInTheDocument();
});
【问题讨论】:
标签: reactjs typescript function unit-testing jestjs