debian-mirror-gitlab/app/assets/javascripts/test_utils/simulate_input.js

24 lines
587 B
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
function triggerEvents(input) {
input.dispatchEvent(new Event('keydown'));
input.dispatchEvent(new Event('keypress'));
input.dispatchEvent(new Event('input'));
input.dispatchEvent(new Event('keyup'));
}
export default function simulateInput(target, text) {
const input = document.querySelector(target);
if (!input || !input.matches('textarea, input')) {
return false;
}
if (text.length > 0) {
2021-03-08 18:12:59 +05:30
Array.prototype.forEach.call(text, (char) => {
2018-03-17 18:26:18 +05:30
input.value += char;
triggerEvents(input);
});
} else {
triggerEvents(input);
}
return true;
}