简体   繁体   English

在 redux-form 上使用 react-testing-library 时,表单输入不会改变

[英]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 .我正在尝试测试当用户将文本inputbuttondisabled变为enabledinput中时。 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-formreact-testing-library However, if I use a native input rather than redux-form 's Field , the test passes.但是,如果我使用本机input而不是redux-formField ,则测试通过。

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.

相关问题 如何使用redux-form预先填充反应表单输入字段? - How to prepopulate react form input fields using redux-form? 使用 Jest 和 react-testing-library 测试具有大量输入字段的表单的正确方法是什么? - What is the proper way to test a form with a lot of input fields using Jest and react-testing-library? 如何使用 react-testing-library 测试带有动作重定向的表单 - How to test a form with action redirection using react-testing-library 使用 react-testing-library 在表单中提交时,如何测试已调用 function? - How to test a function has been called when submitted within a form using react-testing-library? 使用带有redux-form的react-datepicker - Using react-datepicker with redux-form 使用 Redux 和 react-testing-library 测试 React 组件 - Test React Component using Redux and react-testing-library 使用 react-testing-library 进行测试时如何模拟 react-hook-form 的元素? - How to mock the elements of react-hook-form when testing with react-testing-library? React redux-form - 测试时出错 - “TypeError: handleSubmit is not a function” - React redux-form - error while testing - "TypeError: handleSubmit is not a function" 使用 redux-form 库在 React 中输入字符后失去对输入字段的关注 - Losing focus on input field after typing a character in React with redux-form library 用redux-form反应Datepicker - React Datepicker with redux-form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM