[英]Form input not changing when using react-testing-library on redux-form
I am trying to test that when a user inputs text into an input
that a button
changes from disabled
to enabled
.我正在尝试测试当用户将文本
input
到button
从disabled
变为enabled
的input
中时。 It clearly works in the browser, but I can not get the test to pass.它在浏览器中显然有效,但我无法通过测试。 I am using
redux-form
and react-testing-library
.我正在使用
redux-form
和react-testing-library
。 However, if I use a native input
rather than redux-form
's Field
, the test passes.但是,如果我使用本机
input
而不是redux-form
的Field
,则测试通过。
I created a repo with minimal code to duplicate the problem.我用最少的代码创建了一个repo来复制问题。
Since you are not using the reducer from redux-form in your tests, its props will not be passed down to your component correctly.由于您没有在测试中使用 redux-form 中的 reducer,因此它的 props 不会正确传递给您的组件。 It's best if you provide a reducer which resembles what your app is actually using.
最好提供一个类似于您的应用程序实际使用的减速器。 The following code works.
以下代码有效。
import React from "react"
import { Provider } from "react-redux"
import { combineReducers, createStore } from "redux"
import { reducer as reduxFormReducer } from "redux-form"
import { fireEvent, render } from "@testing-library/react"
import SimpleForm from "./SimpleForm"
// Create a reducer that includes the form reducer from redux-form
const reducer = combineReducers({
form: reduxFormReducer,
})
const store = createStore(reducer)
it("test that button is not disabled when input is entered", () => {
const { getByLabelText, getByText } = render(
<Provider store={store}>
<SimpleForm />
</Provider>,
)
const input = getByLabelText("Name")
const submit = getByText("Submit")
fireEvent.change(input, { target: { value: "test input" } })
expect(submit).not.toBeDisabled()
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.