- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对我的 ReactJS 组件进行单元测试。基本上它是一个由输入和按钮组成的简单组件。单击按钮时,它会触发一个名为“onSave”的事件,并且仅当 state.textValue 不为空时才会调用“this.props.addTodo”:
import React, {Component, PropTypes} from 'react';
export default class Invoer extends Component {
static propTypes = {
saveTodo: PropTypes.func.isRequired
}
constructor(props) {
super();
this.props = props;
this.state = {textValue: ''};
}
onChange = (event) => {
if (event.target.value !== '') {
this.setState({textValue: event.target.value});
console.log(event.target.value);
}
}
onSave = () => {
if (this.state.textValue!== '') {
this.props.saveTodo(this.state.textValue);
}
}
render() {
return (
<header>
<input type="text" value={this.state.textValue} onChange={this.onChange}/>
<button onClick={this.onSave}>save</button>
</header>
);
}
}
我为 onSave 事件编写了一个单元测试。唯一的问题是我无法弄清楚如何“模拟” this.state.textValue 以便设置第 26 行上的状态,请参见上面:
import React from 'react';
import Invoer from "../components/Invoer";
import {createRenderer} from 'react-test-renderer/shallow';
it('should call this.props.saveTodo event when clicking on button on non empty value', () => {
const props = {
saveTodo: jest.fn()
}
const renderer = createRenderer();
renderer.render(<Invoer {...props} />)
const output = renderer.getRenderOutput()
//tried to mock the state.textValue, results in error:
//output.props.children[0].value = 'hoera';
output.props.children[1].props.onClick();
expect(props.saveTodo).toBeCalled()
});
运行此测试时,我收到此错误:
should call this.props.saveTodo event when clicking on button on non empty value
expect(jest.fn()).toBeCalled()
Expected mock function to have been called.
这当然是预料之中的。我怎样才能“模拟”这个 this.state.textValue ?或者这是完全错误的方法?
最佳答案
对于遇到这个问题的其他人(我假设OP现在已经解决了这个问题)-如果使用enzyme您可以直接在组件上调用 setState:
// assuming props and mockState are set in a beforeEach()
it('adds state to the component', () => {
const myComponent = shallow(<MyComponent {...props} />);
myComponent.setState({ ...mockState });
const instance = myComponent.instance();
expect(instance.state).toEqual(mockState);
// you can then directly call instance methods - e.g. mocking
// previous props/state to test changes are handled as expected
instance.componentDidUpdate(prevProps, prevMockState);
});
请注意,根据您的要求,您可能需要使用 mount()
而不是 shallow()
。
关于reactjs - 如何在 ReactJS 组件单元测试中获取 'mock' 状态值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46315640/
我有一个帮助程序类,它提供有关登录用户的信息。我想将它作为静态方法并在多个地方使用它(不是 react 组件) 如何访问类模型中的 redux 存储值? 最佳答案 如果您可以在应用程序中管理它,那么使
我正在构建一个简单的 rgba 选择器,它允许用户通过向上箭头和向下箭头键切换各个值。下面是我的代码片段。 class App extends Component { constructor(){
想象一下这段代码: class App extends React.Component { state = { name: "george", counter: 1 };
我正在使用 PHP SDK getLoginUrl() 函数,它可以完美地让用户登录。一旦用户被重定向回我的页面,URL 可以有两种形式,请参见以下链接第 3 小节:http://developers
我正在尝试对我的 ReactJS 组件进行单元测试。基本上它是一个由输入和按钮组成的简单组件。单击按钮时,它会触发一个名为“onSave”的事件,并且仅当 state.textValue 不为空时才会
我试图通过将成员变量保存在 ViewState 中(只有几个小变量)来保留它们,但是我读了一下,我发现在 PreRender 阶段将它们保存到 ViewState 中比 PageLoad 更好? 我可
我正在尝试根据用户的输入更改 div 背景颜色。我想将状态键设置为色调,值为用户输入。然后我想获取该状态并将其注入(inject)另一个名为颜色值的状态键。我希望颜色值是在 chromajs 中使用
研究应该是对数据库的简单调用。选择与当前用户对应的记录,然后根据检索到的数据执行一些代码。但是,当它尝试查询数据库时,它返回“数据转换失败。[ OLE DB 状态值(如果已知)= 2 ]”。有什么想法
我正在运行以下代码 /*Fetchinch Last CustID from custMaster*/ int ID = 0; try { con.Open(); da = new
我是一名优秀的程序员,十分优秀!