TypeError: getItems[props.action] is not a function

TypeError: getItems[props.action] is not a function

我是 React 测试的新手,我正在尝试使用此代码验证 React 功能组件是否存在,但出于某种原因它抱怨其他问题 TypeError: getItems [ props.action ]不是函数

任何 help/suggestion 以此验证组件是否存在?

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

您正在为 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 的所有函数,但最后一个函数需要一个 site 参数。但是,当前您也为参数传递了一个空字符串。在 sites 函数中,您可以删除未使用的 sites 参数