我正在转移到 react-testing-library,并且不知道如何触发此事件并获得更改的结果。
我尝试使用 fireEvent
函数来触发更改,然后尝试了 rerender
函数,但我似乎无法让它工作。
应用程序.js
import React, { useState } from "react";
import logo from "./logo.svg";
import "./App.css";
const options = {
DoTheThing: 'DoTheThing',
DoOtherThing: 'DoOtherThing',
};
function App() {
const [action, setAction] = useState(options.DoTheThing);
return (
<div className="App">
<header className="App-header">
<form>
<fieldset>
<label>
<input
type="radio"
name="radio1"
value={options.DoTheThing}
checked={action === options.DoTheThing}
onChange={event => setAction(event.target.value)}
/>
First
</label>
<label>
<input
type="radio"
name="radio1"
value={options.DoOtherThing}
checked={action === options.DoOtherThing}
onChange={event => setAction(event.target.value)}
/>
Second
</label>
</fieldset>
</form>
</header>
</div>
);
}
export default App;
应用程序.test.js
import React from 'react';
import { render, cleanup, fireEvent } from 'react-testing-library';
import App from './App';
afterEach(cleanup);
it('should change the value ', () => {
const {getByLabelText, rerender } = render(<App/>);
const second = getByLabelText(/Second/);
fireEvent.change(second);
rerender(<App/>);
expect(document.forms[0].elements.radio1.value).toEqual("DoOtherThing");
});
原文由 jktravis 发布,翻译遵循 CC BY-SA 4.0 许可协议
更新
正如人们指出的那样,我原来的解决方案是错误的。
现在我建议你使用
userEvent
来更好地模拟用户交互。首先,您不必调用
rerender
。仅当您希望组件接收不同的道具时才使用rerender
。见 链接。每当您调用
fireEvent
时,组件都会像在普通应用程序中一样呈现。触发
change
事件是正确的,但您必须传递带有事件数据的第二个参数。这个例子有效: