admin管理员组文章数量:1426609
I have a material-ui textfield on which I want to simulate the user types in and hit enter. How do I do it in Jest? I went through related answers and none of them are working for me.
Tried the following:
const input = getByTestId('emailId').querySelector('input')
fireEvent.change(input, {
target: { value: 'abc' }
}); //Up to this point works
None of the following are working:
fireEvent.submit(getByTestId('emailId').querySelector('input'));
input.dispatchEvent(new KeyboardEvent('keydown',{'key':'Enter'}));
getByTestId('emailId').querySelector('input').simulate('keydown', {key: 'Enter'})
The code under Test is:
<CustomTextField
data-testid="emailId"
textLabel={t('email', 'Email')}
autoplete="email"
value={userEmail}
onChange={onEmailchange}
handleKeyHandler={submitOnEnter}
autoFocus
/>
CustomTextField is another ponent that returns a textfield:
<TextField
size="small"
data-testid="emailId"
id={textFieldId || 'defaultTextFieldId'}
label={textLabel}
variant="outlined"
fullWidth
onChange={handleOnchange}
onBlur={onBlur}
value={value}
inputRef={txtField}
autoComplete={autoplete}
autoFocus={autoFocus}
type={textFieldType}
onKeyPress={handleKeyHandler}
InputLabelProps={{
classes: {
root: classes.customLabelStyle,
focused: classes.cssFocused
}
}}
InputProps={{
classes: {
root: classes.cssOutlinedInput,
focused: classes.cssFocused,
notchedOutline: classes.notchedOutline
},
endAdornment: showSearchIcon ? inputAdornment : null
}}
{...textFieldProps}
/>
I have a material-ui textfield on which I want to simulate the user types in and hit enter. How do I do it in Jest? I went through related answers and none of them are working for me.
Tried the following:
const input = getByTestId('emailId').querySelector('input')
fireEvent.change(input, {
target: { value: 'abc' }
}); //Up to this point works
None of the following are working:
fireEvent.submit(getByTestId('emailId').querySelector('input'));
input.dispatchEvent(new KeyboardEvent('keydown',{'key':'Enter'}));
getByTestId('emailId').querySelector('input').simulate('keydown', {key: 'Enter'})
The code under Test is:
<CustomTextField
data-testid="emailId"
textLabel={t('email', 'Email')}
autoplete="email"
value={userEmail}
onChange={onEmailchange}
handleKeyHandler={submitOnEnter}
autoFocus
/>
CustomTextField is another ponent that returns a textfield:
<TextField
size="small"
data-testid="emailId"
id={textFieldId || 'defaultTextFieldId'}
label={textLabel}
variant="outlined"
fullWidth
onChange={handleOnchange}
onBlur={onBlur}
value={value}
inputRef={txtField}
autoComplete={autoplete}
autoFocus={autoFocus}
type={textFieldType}
onKeyPress={handleKeyHandler}
InputLabelProps={{
classes: {
root: classes.customLabelStyle,
focused: classes.cssFocused
}
}}
InputProps={{
classes: {
root: classes.cssOutlinedInput,
focused: classes.cssFocused,
notchedOutline: classes.notchedOutline
},
endAdornment: showSearchIcon ? inputAdornment : null
}}
{...textFieldProps}
/>
Share
Improve this question
edited May 12, 2021 at 5:40
ABGR
asked May 11, 2021 at 13:26
ABGRABGR
5,2754 gold badges34 silver badges55 bronze badges
3
- 1 To be sure: do you mean 'simulate' or 'stimulate'? – Mustard Shaper Commented May 11, 2021 at 13:29
- Show the code under test – Lin Du Commented May 12, 2021 at 4:20
- @slideshowp2 Done – ABGR Commented May 12, 2021 at 5:40
2 Answers
Reset to default 3Well what worked for me is this:
fireEvent.keyPress(input, { key: 'Enter', charCode: 13 });
keyPress
is now deprecated, so using keyDown
worked for me
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', keyCode: 13 });
本文标签: javascriptHow do I simulate text getting entered and enter key pressed in JestStack Overflow
版权声明:本文标题:javascript - How do I simulate text getting entered and enter key pressed in Jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745481717a2660202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论