简体   繁体   English

笑话和测试库控制的输入

[英]jest and testing-library controlled input

I'm trying to apply a unit test in my custom input component我正在尝试在我的自定义输入组件中应用单元测试
If I pass the value prop in the <Input /> in the render function, the test fails.如果我在渲染function 的<Input />中传递value属性,则测试失败。
Does anyone know the reason?有谁知道原因?
In controlled components like this input, what we can test to have valid unit tests?在像这个输入这样的受控组件中,我们可以测试什么来获得有效的单元测试?
Thanks谢谢

Input.tsx输入.tsx

import React, {ChangeEvent, FunctionComponent} from 'react';
import styles from './Input.module.css'

export interface InputProps {
    type?: string
    disabled?: boolean
    value?: string
    onChange?: (value: string) => void
}

const Input: FunctionComponent<InputProps> = ({
    type= 'text',
    disabled = false,
    value,
    onChange,
}) => {
    
    const handleOnChange = (e: ChangeEvent<HTMLInputElement>) => {
        const value = e.currentTarget.value
        if (onChange) {
            onChange(value)
        }
    }
    
    return (
        <div className={styles.inputWrapper}>
            <input
                className={styles.input}
                type={type}
                disabled={disabled}
                value={value}
                onChange={handleOnChange}
            />
        </div>
    );
}

export default Input;

Input.test.tsx输入.test.tsx

// PASS WITHOUT ANY ERRORS
it('should has the new value', async () => {
    const user = userEvent.setup()
    const onChangeMock = jest.fn();
    const { getByRole } = render(<Input onChange={onChangeMock} />)
    const input = getByRole('textbox')
    expect(input).toHaveValue('')
    await user.type(input, 'new input value')
    expect(input).toHaveValue('new input value')
})
 
// FAILS
it('should has the new value', async () => {
    const user = userEvent.setup()
    const onChangeMock = jest.fn();
    const { getByRole } = render(<Input value="" onChange={onChangeMock} />)
    const input = getByRole('textbox')
    expect(input).toHaveValue('')
    await user.type(input, 'new input value')
    expect(input).toHaveValue('new input value')
})

You can use rerender function that is returned from render .您可以使用从render返回的重新rerender function 。

With this, you send new value into your component, input has a new value and then you can assert for that value in the input.有了这个,您将新值发送到您的组件中,输入有一个新值,然后您可以在输入中为该值断言。

In your case, your test needs to have also checking if onChange method is called when you do userEvent.type() or if that does not work you can use fireEvent.change(input...) .在您的情况下,您的测试还需要检查在您执行userEvent.type()时是否调用了onChange方法,或者如果这不起作用,您可以使用fireEvent.change(input...)

it('should has the new value', async () => {
  const user = userEvent.setup();
  const onChangeMock = jest.fn();
  const { getByRole, rerender } = render(<Input value="" onChange={onChangeMock} />);

  const input = getByRole('textbox');
  expect(input).toHaveValue('');

  rerender(<Input value="new input value" onChange={onChangeMock} />);

  expect(input).toHaveValue('new input value');
});

暂无
暂无

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

相关问题 如何使用@testing-library/react 和 react-test-renderer 测试由 Redux 状态控制的输入值? - how to test input value controlled by Redux state using @testing-library/react and react-test-renderer? useFakeTimers 在玩笑/测试库中不起作用 - useFakeTimers not working in jest/testing-library @testing-library/jest-dom 未加载 - @testing-library/jest-dom not loading 使用 testing-library 和 jest 的 React 组件抛出的测试错误 - Testing error thrown by a React component using testing-library and jest 如何在 jest 和 @testing-library/react 中测试 catch 部分 - How to test catch part in jest and @testing-library/react 无法使用 Jest 和 Testing-Library 测试 React 组件渲染,因为 Jest 没有可用的文档 - Trouble testing React component render with Jest and Testing-Library because there's no Document available to Jest @testing-library/react 的默认值不会发生输入更改 - Input changes do not occur with default value with @testing-library/react 使用 Jest 和 @testing-library/react-hooks 在 TypeScript 中单元测试自定义 React Hook - Unit Testing Custom React Hooks in TypeScript using Jest and @testing-library/react-hooks Jest + @testing-library/react 在 [dom-accessibility-api] 库中的 a.mjs 文件上抛出错误 - Jest + @testing-library/react throws error on a .mjs file from [dom-accessibility-api] library 自定义 React 组件库 - 开玩笑“找不到模块反应”- 测试库,汇总 - Custom React Component Library - jest 'cannot find module react'- testing-library, rollup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM