Пытаюсь написать свой первый тест с помощью react-testing-library, но похоже, что он не захватит правильный элемент material-ui.
https://codesandbox.io/s/lx5nl1839z
Я получаю сообщение об ошибке, в котором говорится, что событие никогда не запускалось.
Ожидается, что фиктивная функция будет вызываться один раз, но она вызывалась ноль раз.
Когда я меняю button
на обычную кнопку вместо Button из материала ui, тогда он работает.
Вот мой тест
test('calls with user email and password', () => {
const login = jest.fn();
const error = null as any;
const { getByLabelText, getByText } = render(
<LoginForm login={login} error={error} />
);
const email = getByLabelText('Email');
const password = getByLabelText('Password');
(email as any).value = '[email protected]';
(password as any).value = 'password';
fireEvent.click(getByText('Login'));
expect(login).toHaveBeenCalledTimes(1);
expect(login).toHaveBeenCalledWith({
email: '[email protected]',
password: 'password',
});
});
Вот моя составляющая:
export const LoginForm: FunctionComponent<IProps> = ({ login, error }) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
return (
<Paper style={{ width: '400px', padding: '30px', margin: '0 auto' }}>
<form
onSubmit={e => {
e.preventDefault();
return login({ email, password });
}}
>
<TextField
id="email"
label="Email"
onChange={e => setEmail(e.target.value)}
fullWidth
variant="outlined"
error={Boolean(getEmailError(email))}
helperText={getEmailError(email)}
/>
<TextField
id="password"
label="Password"
type="password"
style={{ marginTop: '10px' }}
onChange={e => setPassword(e.target.value)}
fullWidth
variant="outlined"
/>
<LoginError error={error} />
<Button
variant="contained"
color="primary"
type="submit"
fullWidth
// disabled={isDisabled(email, password)}
style={{
margin: '20px 0px',
}}
>
Login
</Button>
</form>
<Divider />
<Typography style={{ margin: '10px auto' }}>Forgot password?</Typography>
</Paper>
);
};
getByLabelText
? Не могли бы вы очистить свой код, чтобы его можно было просто вставить в codeandbox и проверить, что отрисовывается? - person epsilon   schedule 06.12.2018