我正在尝试测试,当用户在input
中输入文字时,button
从disabled
变为enabled
。它显然可以在浏览器中工作,但是我无法通过测试。我正在使用redux-form
和react-testing-library
。但是,如果我使用本机input
而非redux-form
的{{1}},则测试通过。
我用最少的代码创建了一个repo,以重复该问题。
答案 0 :(得分:0)
由于您没有在测试中使用来自redux-form的reducer,它的props将不会正确地传递到您的组件。最好提供一个类似于您的应用实际使用的减速器。以下代码有效。
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()
})