1

我正在尝试测试登录表单验证错误:

<Form className='form' onSubmit={onSubmit} data-testid='form'>
<FormGroup className='form-group'>
      <input
        type='email'
        placeholder='Email Address'
        name='email'
        value={email}
        onChange={onChange}
        required
      />
    </FormGroup>
    <FormGroup className='form-group'>
      <input
        type='password'
        placeholder='Password'
        name='password'
        value={password}
        onChange={onChange}
        minLength='6'
      />
    </FormGroup>
    <input type='submit' value='Login' />
  </Form>

但问题是我无法在 component = render() 中获取错误文本

我的测试如下:

it('validate user inputs, and provides error messages', async () => {
    const { getByTestId, getByText, getByPlaceholderText } = component
    
    fireEvent.change(screen.queryByPlaceholderText(/Email Address/i), {
        target: {value: ""},
    });

    fireEvent.change(screen.queryByPlaceholderText(/Password/i), {
        target: {value: ""},
    });

    fireEvent.submit(getByTestId("form"));

    expect(getByText("Please fill out this field.")).toBeInTheDocument();


})

我正在开玩笑地使用反应测试库

4

0 回答 0