- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个函数,可以成功测试 componentDidMount 何时触发。但由于某种原因,使用相同的逻辑来测试其相邻方法是否已被触发是行不通的。不知道为什么?谁能告诉我我的误解是什么?
//Account.js
...
componentDidMount() {
this.checkData();
}
checkData = () => {
console.log('i am a check data method that needs testing');
}
...
//开 Jest
// this works
it('should call the CDM function', () => {
const instance = mountedComponent.instance();
jest.spyOn(instance, 'componentDidMount');
instance.componentDidMount();
expect(instance.componentDidMount).toHaveBeenCalledTimes(1);
})
// Attempt 1 - this fails "Cannot spy the checkData property because it is not a function; undefined given instead"
it('should call the `checkData` function', () => {
const instance = mountedComponent.instance();
jest.spyOn(instance, 'checkData');
instance.componentDidMount();
expect(instance.checkData).toBeCalledTimes(1);
})
// Attempt 2 - also fails "Received number of calls: 0"
it('should call the `checkData` function', () => {
const instance = mountedComponent.instance();
instance.checkData = jest.fn();
instance.componentDidMount();
expect(instance.checkData).toBeCalledTimes(1);
})
为什么实例中会存在 CDM 而不是 checkData
?>
最佳答案
因此,最好的方法是检查结果,而不是专门检查函数调用。
checkData
实际在做什么(您尚未显示)。它是否调用了另一个文件中的某些内容?
如果是这样,请模拟其他文件中的函数以返回一些数据,并验证在安装组件时是否调用了模拟函数。
例如:
// component file
import { someMethod } from 'someModule';
export class MyComponent extends React.Component {
async checkData() {
await someMethod();
}
componentDidMount() {
this.checkData();
}
render() {
}
}
// in your spec file
import { someMethod } from 'someModule';
jest.mock('someModule');
someMethod.mockImplementation(() => {
// do whatever you want here
});
// do your all your normal setup, probably something like this
let mountedComponent;
beforeEach(() => {
mountedComponent = mount(...);
});
// clear the mock after each mount
afterEach(() => someMethod.mockClear());
it('should do things',() => {
expect(someMethod).toHaveBeenCalled();
});
关于javascript - 为什么 Jest/Enzyme 跟踪/聆听 componentDidMount 或任何 React 无法正常运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60969691/
标签自动换行?
我正在尝试侦听变量更改以执行一些代码。因此,该变量是一个名为reset的 bool(boolean) 值。动画结束或按下(来自另一个小部件的)按钮后,我想执行一些操作(例如重置动画 Controlle
我想实现的是: 呈现模板后绑定(bind)到表单提交以添加微调器 在呈现模板之前移除微调器(如果存在) 我想如果有一种方法可以绑定(bind) Handlebars 的渲染,就很容易实现。我还没有找到
我正在为我的计算机科学老师制作一个 Java 应用程序,该应用程序允许他通过从 Google Drive 文件夹加载大量 PDF 并使用 Java Swing 将它们显示到 JScrollPane 上
Java Lambda 客户端应用程序是否可以“监听”Lambda 的结果?例如,客户端应用程序不会调用 lambda,而是说 lambda 是从 SNS 主题触发的。我希望客户端应用程序能够获取 S
我有一个 iOS 市场应用程序,我在其中监听该市场上出售的每件商品的变化。更准确地说,我会倾听该项目上的 child 是否发生变化或被删除。 问题是我也想听听何时添加 child 。在 Firebas
我正在使用 Juggernaut、Redis、SQLite 和 Rails 3.1 构建一个简单的实时聊天 我想在另一个用户断开连接时(例如他关闭了窗口)向每个用户写入一条新消息,这是为了监听 Jug
我编写了一个函数,可以成功测试 componentDidMount 何时触发。但由于某种原因,使用相同的逻辑来测试其相邻方法是否已被触发是行不通的。不知道为什么?谁能告诉我我的误解是什么? //Acc
有谁知道如何监听 IBM Cloud Object Storage 中特定存储桶(数据库)的更改?我已阅读文档并检查了所有相关链接,但找不到任何 atm。我想要一个函数来响应对象存储中所做的更改,例如
我是一名优秀的程序员,十分优秀!