简体   繁体   English

带有 React 测试库的测试输入搜索框

[英]Test input Search box with React testing library

I have a SearchBox component:我有一个 SearchBox 组件:

    function SearchBox({ search, setSearch }) {
      return (
        <div>
          <div>
            <input onChange={e => setSearch(e.target.value)} type="text" placeholder="Search..." value={search} />
          </div>
        </div>
      )
    }

I tried the following:我尝试了以下方法:

    describe('Input value', () => {
      it('updates on change', () => {
        const { queryByPlaceholderText } = render(<SearchBox />)
    
        const searchInput = queryByPlaceholderText('Search...')
    
        fireEvent.change(searchInput, { target: { value: 'test' } })
    
        expect(searchInput.value).toBe('test')
      })
    })

But then I get an error:但后来我收到一个错误:

Error: Uncaught [TypeError: setSearch is not a function]错误:未捕获 [TypeError: setSearch is not a function]

How and what can I test on this SearchBox component?我如何以及如何在此 SearchBox 组件上进行测试?

The issue is that setSearch props wasn't provided.问题是没有提供setSearch道具。

setSearch can be provided by jest mock function. setSearch可以由 jest 模拟函数提供。

const setSearch = jest.fn((value) => {})
const { queryByPlaceholderText } = render(<SearchBox setSearch={setSearch}/>)

Full test code snippet:完整的测试代码片段:

import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import SearchBox from './SearchBox'
import '@testing-library/jest-dom/extend-expect'

describe('Input value', () => {
    it('updates on change', () => {
      const setSearch = jest.fn((value) => {})
      
      const { queryByPlaceholderText } = render(<SearchBox setSearch={setSearch}/>)
  
      const searchInput = queryByPlaceholderText('Search...')
  
      fireEvent.change(searchInput, { target: { value: 'test' } })
  
      expect(searchInput.value).toBe('test')
    })
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有 label 和 React 测试库的测试输入 - Test input without label with React Testing Library 如何使用 React 测试库测试输入? - How to test Input with React Testing Library? 如何测试 url 搜索随反应测试库更改? - How can I test that url search changed with react testing library? react-testing-library 输入更改测试总是通过 - react-testing-library input change test always passing 如何使用 React 测试库和 Jest 测试必填输入字段? - How to test a required input field with React Testing Library and Jest? 在 dom-testing-library 或 react-testing-library 中测试输入值的最佳方法 - Best way to test input value in dom-testing-library or react-testing-library 如何使用@testing-library/react 和 react-test-renderer 测试由 Redux 状态控制的输入值? - how to test input value controlled by Redux state using @testing-library/react and react-test-renderer? 如何在 React 测试库中为 Select Box onChange 方法编写测试用例? - How to write test cases for Select Box onChange Method in React Testing Library? 对文本输入使用去抖动的 React 组件执行集成测试(使用 React 测试库) - Perform an integration test on a react component where the text input uses debounce (using react testing library) 使用 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM