繁体   English   中英

使用 Typescript 在 React 中使用 Enzyme 测试输入道具时 onChange 的正确类型

[英]Correct type for onChange in testing input props with Enzyme in React with Typescript

我正在尝试测试反应形式的道具。 我正在使用 Typescript 在 React 中工作。 我有如下特定的密码输入:

<input
                type="password"
                className="my-2"
                onChange={handlePasswordChange}
                value={password}
            />

具有 onChange 功能:

 const handlePasswordChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        const passwordValue = event.target.value.trim();
        setPassword(passwordValue);
    };

我正在尝试测试如下道具:

import React, {ChangeEventHandler} from 'react'
import {shallow, ShallowWrapper} from "enzyme";
import Login from './';

describe('<Logn /> with props test', () => {
    const loginConfig = {
        email: "kmichalski314@gmail.com",
        password: "testvalue",
        dispatch: (param: string) => {
            console.log("test")
        }
    }
    let container: ShallowWrapper;
    beforeEach(() => {
        container = shallow(<Login {...loginConfig}/>);
    })
    it('should have proper props for password field', () =>{
        console.log(container.find('input[type="password"]').props());
        expect(container.find('input[type="password"]').props()).toEqual({
            type: "password",
            className: "my-2",
            value: "testvalue",
            onChange: ChangeEventHandler<HTMLInputElement>;
        });
    })
})

我的问题是如何在 toEqual 断言中正确键入 onChange 函数? Jest 无法识别onChange:ChangeEventHandler

我现在有一个语法错误: 在此处输入图片说明

看起来你应该使用逗号而不是分号

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM