gpt4 book ai didi

reactjs - 未从 react-testing-library 中的 .toHaveBeenCalledTimes() 获得预期结果

转载 作者:行者123 更新时间:2023-12-04 14:17:01 28 4
gpt4 key购买 nike

无论如何,尝试测试一个函数是否在其触发后被调用。 fireEvent正在工作,因为我收到了 console.log从那个函数。但是.toHaveBeenCalledTimes(1)返回 0。我错过了什么?

如果我有 handleLoginSubmit在父级中运行并将其作为 Prop 传递给子级,在测试中一切都通过了。但是如果它在同一个组件中,它就会失败。如果有任何意义,请使用 typescript 。

这是经过测试的

import React, { FC } from 'react';

type Event = React.FormEvent<HTMLFormElement>;

interface Login {
handleLoginSubmit?: (event: Event) => React.ReactNode;
}

const Login: FC<Login> = () => {
const handleLoginSubmit = (_event: Event) => {
console.log('Firing' ); // This is logged
};

return (
<form data-testid='form' onSubmit={(event) => handleLoginSubmit(event)}>
<input data-testid='email'/>
<input data-testid='password'/>
<button data-testid='login-button'>login</button>
</form>
);
};

export default Login;

我的提交测试
  it('should handle ClickEvents', () => {

const handleLoginSubmit = jest.fn();

const { getByTestId } = render(<Login/>);

expect(getByTestId('login-button')).toBeTruthy();

fireEvent.submit(getByTestId('form'));

expect(handleLoginSubmit).toHaveBeenCalledTimes(1);

});

错误信息
 ● Login page › should handle ClickEvents

expect(jest.fn()).toHaveBeenCalledTimes(expected)

Expected number of calls: 1
Received number of calls: 0

32 | fireEvent.submit(getByTestId('form'));
33 |
> 34 | expect(handleLoginSubmit).toHaveBeenCalledTimes(1);
| ^
35 |
36 | });
37 | });

at Object.it (__tests__/components/Login.test.tsx:34:31)

最佳答案

你不能断言 handleLoginSubmit函数是直接调用的。因为它是在 Login 的私有(private)范围内定义的证监会。您无法模拟或监视此功能,因为您无法访问它。所以,你需要间接测试它。由于您使用的是 console.log在这个函数中,我们可以窥探 console.log .如果它被调用,那意味着 handleLoginSubmit函数被调用。
例如。index.tsx :

import React, { FC } from "react";

type Event = React.FormEvent<HTMLFormElement>;

interface Login {
handleLoginSubmit?: (event: Event) => React.ReactNode;
}

const Login: FC<Login> = () => {
const handleLoginSubmit = (_event: Event) => {
console.log("Firing");
};

return (
<form data-testid="form" onSubmit={event => handleLoginSubmit(event)}>
<input data-testid="email" />
<input data-testid="password" />
<button data-testid="login-button">login</button>
</form>
);
};

export default Login;
index.spec.tsx :
import { render, fireEvent } from "@testing-library/react";
import Login from "./";
import React from "react";

it("should handle ClickEvents", () => {
const logSpy = jest.spyOn(console, "log");
const { getByTestId } = render(<Login />);
expect(getByTestId("login-button")).toBeTruthy();
fireEvent.submit(getByTestId("form"));
expect(logSpy).toHaveBeenCalledTimes(1);
});
100% 覆盖率的单元测试结果:
 PASS  src/stackoverflow/59162138/index.spec.tsx
✓ should handle ClickEvents (42ms)

console.log node_modules/jest-mock/build/index.js:860
Firing

-----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.tsx | 100 | 100 | 100 | 100 | |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.987s, estimated 9s
源代码: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59162138

关于reactjs - 未从 react-testing-library 中的 .toHaveBeenCalledTimes() 获得预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59162138/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com