javascript - Reactjs 功能组件中的 Jest SpyOn
问题描述
我正在尝试 SpyOn 一个 reactJS 功能组件内的功能。我已经看到了一些答案,并且开始了解我们将函数作为道具传递给我们的组件的过程。但这种方法对我不起作用。所以我尝试使用
const spy = jest.spyOn(App.prototype, "sampleFunctionName");
As Expected 它会抛出一个错误,说sampleFunctionName
是未定义的,因为 App.prototype 不包含它。所以为了克服这个我添加sampleFunctionName
到 App.prototype asApp.prototype.sampleFunctionName = jest.fn()
并尝试将它作为
wrapper = shallow(<App {...props} />);
wrapper.find("#btn-id").simulate("click")
expect(spy).toHaveBeenCalled()
现在的问题是 spy 被调用的次数总是为零。所以我想知道我尝试过的是否正确或任何其他方法来监视无类组件中的函数
下面是一个示例来演示上述要求:
function App(props){
function save(){
//contains the code for api calls and store changes
}
return(
<Button id = "btn-id" onClick = {save}>Name</Button>
)
}
测试文件会是这样的:
import React from "react";
import { shallow } from "enzyme";
import {App} from './App'
describe('desc',() =>{
...
const spy = jest.spyOn(App.prototype, "save");
//using the above spy says "save" is undefined so added this line before using spy
// App.prototype.save = jest.fn()
wrapper = shallow(<App {...props} />);
wrapper.find("#btn-id").simulate("click")
expect(spy).toHaveBeenCalled()
})
解决方案
您不能模拟或监视save
函数,它是App
构造函数中的私有函数,而不是 App 实例的方法,这意味着save
函数在 App 的原型中不存在。您应该模拟或监视函数内部的save
函数,即为您的案例进行 api 调用的函数/方法。这是单元测试解决方案:
例如
index.js
:
import React from 'react';
export function App(props) {
function save() {
// contains the code for api calls and store changes
console.log('You should spy or mock the function which make the api calls');
}
return (
<button id="btn-id" onClick={save}>
Name
</button>
);
}
index.test.js
:
import React from 'react';
import { shallow } from 'enzyme';
import { App } from './';
describe('61174416', () => {
it('should pass', () => {
const logSpy = jest.spyOn(console, 'log');
const props = {};
const wrapper = shallow(<App {...props} />);
wrapper.find('#btn-id').simulate('click');
expect(logSpy).toHaveBeenCalled();
});
});
覆盖率 100% 的单元测试结果:
PASS stackoverflow/61174416/index.test.tsx (8.332s)
61174416
✓ should pass (19ms)
console.log node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866
You should spy or mock the function which make the api calls
-----------|---------|----------|---------|---------|-------------------
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: 9.395s
将 替换为console.log
进行 api 调用的函数或方法。
源代码:https ://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/61174416
推荐阅读
- cron - 当我添加日志选项时,Cron 作业不起作用
- android - 尝试在 Windows 上为 Android 构建 Chromium 时,gclient runhooks 抛出错误
- java - 不兼容的类型:javafx.scene.media.Media 无法转换为 javax.print.attribute.standard.Media
- python - 降维(异构数据)
- node.js - Meteor Mongodb 深度搜索
- vue.js - `nuxt.js` 替代 vue3?
- javascript - 如何使用 TypeScript 在 useRef 挂钩中键入函数
- c# - 在 .net 核心的自定义模型绑定器中验证嵌套的 Json 对象
- angular - 在 Liferay 中调整 Angular 应用程序时出错
- python - 使用双缓冲时的滚动条动画