酶模拟更改输入不会更改 React Hooks 上的值

Miq*_*des 0 unit-testing reactjs jestjs enzyme react-hooks

我正在尝试用 Jest 和 Enzyme 在 React 上编写测试。但在调用.simulate('change')函数时,该值保持不变NULL,不会更改为预期值miqueias@gmail.com。我的应用程序.jsx:

const App = () => {
    const [username, setUsername] = React.useState(null);
    const [password, setPassword] = React.useState(null);
    const handleChange = (event) => {
        setUsername(event.target.value);
    };
    return (
        <div
            style={{
                height: "100vh",
                display: "flex",
                flexDirection: "column",
                justifyContent: "center",
                alignItems: "center",
            }}
        >
            <input
                type="email"
                id="email"
                value={username}
                onChange={handleChange}
            />
            <input
                type="password"
                id="password"
                value={password}
                onChange={(event) => setPassword(event.target.value)}
            />
            <button>Enviar</button>
        </div>
    );
};
export default App;
Run Code Online (Sandbox Code Playgroud)

我的应用程序.test.js:

import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import {
    configure,
    shallow,
} from 'enzyme';
import App from '../App';
configure({
    adapter: new Adapter()
});
it('<App/> state input', () => {
    const email = shallow(<App/>).find('#email');
    email.simulate('change', {
        target: {
            value: 'miqueias@gmail.com'
        }
    });
    expect(email.prop('value')).toEqual('miqueias@gmail.com');
})
Run Code Online (Sandbox Code Playgroud)

命令返回错误npm test

Expected: "miqueias@gmail.com"
Received: null
Run Code Online (Sandbox Code Playgroud)

小智 6

我刚刚遇到了同样的错误。对链接进行调查有更好的解释,但基本上您必须重新查找才能更新值 \xe2\x80\x8b\xe2\x80\x8b...

\n
cont wrapper = shallow(<App/>);\nlet email = wrapper.find(\'#email\');\n\nemail.simulate(\'change\', {\n    target: {\n        value: \'miqueias@gmail.com\'\n    }\n});\n\nemail = wrapper.find(\'#email\');\nexpect(email.prop(\'value\')).toEqual(\'miqueias@gmail.com\');\n
Run Code Online (Sandbox Code Playgroud)\n

来源:https://github.com/enzymejs/enzyme/blob/master/docs/guides/migration-from-2-to-3.md#element-referential-identity-is-no-longer-preserved

\n