Google Recaptcha v2 的 React 单元测试

vbo*_*tio 5 javascript reactjs jestjs enzyme

我实现了一个 React-Recaptcha 组件,我试图向它添加一些单元测试。

除其他外,我试图测试我的组件的状态是否已更改(这应该是选中该框后收到的令牌)。

<ReCAPTCHA sitekey={RECAPTCHA_SITE_KEY} onChange={value => this.setState({ gToken: value, recaptchaState: true })} />
Run Code Online (Sandbox Code Playgroud)

我的测试有这样的东西

describe('RefundaModal Component', () => {
  const modalcomponent = shallow(<RefundModalDialog />);
  const captcha = shallow(<ReCAPTCHA sitekey={RECAPTCHA_SITE_KEY} onChange={() => {}} />);
  test('render RefundModalDialog component', () => {
    expect(modalcomponent.exists()).toBe(true);
  });
  test('user clicked on recaptcha', async () =>
    modalcomponent.setState({ gToken: null, recaptchaState: false });
    captcha.props().onChange();
    expect(modalcomponent.state().recaptchaState).toEqual(true);
  });
});
Run Code Online (Sandbox Code Playgroud)

我也尝试了一种不同的方法,captcha.simulate('change');但是当我想要接收 true 时,我一直收到 false。

实施此类测试的正确方法是什么?

谢谢