如何用一个玩笑写一个功能的测试用例?

时间:2020-06-12 06:05:58

标签: reactjs typescript jestjs enzyme

我已经创建了一个函数,我想为该函数编写一个测试用例。我正在该函数中返回HTML内容。

我的功能代码是-

export function executeScenario(handleExecuteScenarioClick: ((event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void) | undefined, classes: any , buttonTitle:string) {
  return <Button id="executescenario" variant="contained" onClick={handleExecuteScenarioClick} type="button" className={classes.button} color="primary">
    {buttonTitle}
  </Button>;
}

我正在使用酶和玩笑,我想测试该功能并返回一个真实的按钮。 如何编写此功能的测试用例?

2 个答案:

答案 0 :(得分:1)

您可以使用React测试库并编写如下测试:

pd.DataFrame(np.reshape(df.to_numpy(),(-1,2)))

       0    1
0   Rahul   e34
1   Pradeep e44
2   Azhar   t54
3   Pavan   e24
4   Venkat  r45
5   Akash   e14
6   Vipul   r15
7   Fairo   e45
8   Akshay  e44
9   Vivek   e99
10  Kumar   e55
11  Asad    t14

这是希望在页面上找到带有提到的import { render, fireEvent, wait } from '@testing-library/react' const { getByTestId } = render(<executeScenario {...props} />) expect(getByTestId("executescenario")).toBeInTheDocument() 的元素。要获取完整的文档和您可以执行的其他操作,请在此处查看文档:{​​{3}}

答案 1 :(得分:1)

如果您正在使用Enzyme和Jest,则可以通过浅层渲染该组件来测试以上内容,然后检查是否使用find方法来渲染Button

const wrapper = shallow(<executeScenario buttonTitle='sample' classes='primary' />); // include the required props
const button = wrapper.find(Button);
expect(button).toHaveLength(1);