- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
模拟按钮单击似乎是一个非常简单/标准的操作。但是,我无法让它在 reacyt Jest.js 测试中工作。
下面是我尝试过的,错误消息是什么以及我想测试的代码
我想测试的代码
class Home extends React.Component {
constructor(props) {
super(props)
this.state = {
showAssessment: false
};
}
assessment = (boolean) => {
this.setState({
showAssessment: boolean
})
}
render() {
if (this.state.showAssessment === true) {
return <App
assessment = {
this.assessment
}
/>
} else {
return ( < div className='container-fluid' style={landingPage}>
<
Component1 / >
<
button className='btn btn-dark' style={matchToolButton} onClick = {
() => this.assessment(true)
} > Match Tool < /button> < /
div > )
}
}
}
我编写的测试:
import React from 'react';
import { shallow, mount, render } from 'enzyme';
import Home from './Home';
test ('Home Button', () => {
it ('It calls assessment function when clicked', () => {
const wrapper = mount(<Home/>); // rednoing the home componenet/ testing
wrapper.instance().assessment = jest.fn(); //if this isn't working try wrapper.instance().assessment
wrapper.find('button').simulate('click');
expect(wrapper.assessment).toHaveBeenCalled();// see comment on line 14 keep the same
});
});
我收到的错误消息
FAIL src/Home.test.js
● Test suite failed to run
/Users/mbyousaf/Desktop/Bucket/dsp_poc2_uwe/front-end/src/Home.test.js: Unexpected token (12:30)
10 |
11 |
> 12 | const wrapper = mount(<Home/>); // rednoing the home componenet/ testing
| ^
13 | wrapper.instance().assessment = jest.fn(); //if this isn't working try wrapper.instance().assessment
14 |
15 | wrapper.find('button').simulate('click');
最佳答案
首先你的测试中有 test(... it(... 。它无法运行。它应该是描述(... it(...
其次,React 测试库使您想要实现的目标变得非常容易(取代 enzyme ):
import { render, fireEvent } from "@testing-library/react";
describe('Home Button', () => {
it ('It calls assessment function when clicked', () => {
const { getByText } = render(<Home/>);
fireEvent.click(getByText("Match Tool"));
// test now whatever should be differnt after the button is clicked
...
关于javascript - 如何在 Enzyme React 中模拟按钮单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553857/
我的页面上有多个按钮: Home Action 1 Action 2 如何通过其文本选择“操作 2”按钮,以便单击它?我知道可能还有其他方法来选择该按钮,但我专门寻找通过文
我目前正在学习使用 Enzyme 进行单元测试。 似乎有一种奇怪的行为: enzyme 似乎随机地不识别某些成分。让我给你举个例子: 这是我的SafeContainer组件: import React
我在使用 Jest/Enzyme 编写的测试用例时遇到了一些问题。我有一个 React/Redux 组件,正在尝试编写一个基本测试,但出现以下错误: Invariant Violation: Reac
在我的项目中使用 enzyme 3 时出现以下错误(之前使用 enzyme 2 并且一切正常): Enzyme Internal Error: configured enzyme adapter di
我正在致力于使用摩卡、 enzyme 创建 react 组件的单元测试。下面是一个示例组件。 Foo.js class Foo extends React.Component { custom
我已经设置了 jest 和 enzyme,但无法运行。它提示 EnzymeAdapter 基类。 我试过了 configure({}) with import { configure } from '
使用 React+Enzyme+Jest 您好,我使用 .innerText 属性来获取特定元素的值,请参阅我的代码的第 5 行: _modifyProfileField (event) {
我正在使用 airbnb 的 enzyme 库进行 react 测试,我只想检索当前节点的文本,不包括来自任何子节点的任何文本。 const component = hellothere 如果我做
我一直在查看很多使用 Enzyme 和 Jest 测试 React 和 Redux 应用程序的示例,但很少有人提到代码清理。当使用 shallow 或 mount 时,您是否不需要明确调用 unmou
从文档来看,这样的事情应该是可能的: const wrapper = mount(); expect(wrapper.find('.foo')).to.have.length(1); 但就我而言,这会
有关enzyme 测试库中adapter 用途的任何文档。 import { configure } from 'enzyme'; import Adapter from 'enzyme-adapte
我需要访问内部组件的状态,使其在单击事件时处于事件状态,我的问题是 Enzyme 在使用 mount 时不允许这样做,这只能通过 来实现如 here 中提到的 enzyme 的浅渲染,也如上所述,我尝
我正在尝试使用 Enzyme 来测试组件的方法。我知道执行此操作的典型方法是使用 Enzyme 的 instance() 方法。 问题是,这只适用于root组件,我的组件需要包装在两个上下文提供程序中
我正在尝试测试当通过简单的 bool 值更新状态时,在父组件中其子组件是否正确呈现。在父组件下方: class Parent extends Component { ... render()
我的渲染函数中有一个 FileInput } className= 'fileInput' /> 我需要编写一个文件上传测试,当我模拟更改函数时,它调用函数file
我通过 create-react-app 创建了一个新的 React 应用程序,我想为我在应用程序中创建的名为“MessageBox”的组件编写单元测试。这是我编写的单元测试: import Mess
昨天我将React项目升级到了v16.0,但是我发现Enzyme有一些问题 Error: Enzyme Internal Error: Enzyme expects an adap
我有一个具有多种渲染方法的组件。我怎样才能测试它们呢?使用 enzyme 和 Jest 进行 react 。 一些伪代码作为我的组件结构的示例,因为该组件非常大。 class MyComponent
我正在使用 Jest 和 Enzyme 来测试我的 react 组件。其中一项测试如下所示: import DropDown from './DropdDown'; import SelectButt
我正在尝试测试轮子事件。我在加载事件中添加滚轮监听器。 this.domElement = document.getElementById('myImg'); if (this.domElement)
我是一名优秀的程序员,十分优秀!