我是反應測驗的新手,我正在嘗試使用此代碼驗證反應功能組件是否存在,但出于某種原因,它抱怨其他型別錯誤: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();
});
uj5u.com熱心網友回復:
您正在為action名稱傳遞一個空字串,但getItems[""]不匹配任何函式,因為它的值為getItems:
const getItems = {
analysers: (site: any) => dispatch(getAnalysers(site)),
platforms: (site: any) => dispatch(getPlatforms(site)),
brokers: (site: any) => dispatch(getBrokers(site)),
cameras: (site: any) => dispatch(getCameras(site)),
sites: (site: any) => dispatch(getSites()),
}
也是getItems 的所有功能,但最后一個,需要一個站點引數。但是,目前您還為引數傳遞了一個空字串。在sites函式中,您可以洗掉未使用的站點引數
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/340818.html
上一篇:設定切比雪夫插值
