Skip to content

Commit

Permalink
fix: simulated on change usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Haller committed Apr 8, 2020
1 parent a877e46 commit 24ea017
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/species/formFiller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ const getDefaultConfig = randomizer => {
/**
* Hacky function to trigger react, angular & vue.js onChange on input
*/
const triggerSimulatedOnChange = (element, newValue, elementType = 'input') => {
const triggerSimulatedOnChange = (element, newValue, prototype) => {
const lastValue = element.value;
element.value = newValue;

const htmlPrototypes = {
textarea: window.HTMLTextAreaElement.prototype,
input: window.HTMLInputElement.prototype,
};

const prototype = htmlPrototypes[elementType];
if (!prototype) {
return;
}
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
nativeInputValueSetter.call(element, newValue);
const event = new Event('input', { bubbles: true });
Expand All @@ -34,15 +25,15 @@ const getDefaultConfig = randomizer => {
const fillTextElement = element => {
const character = randomizer.character();
const newValue = element.value + character;
triggerSimulatedOnChange(element, newValue);
triggerSimulatedOnChange(element, newValue, window.HTMLInputElement.prototype);

return character;
};

const fillTextAreaElement = element => {
const character = randomizer.character();
const newValue = element.value + character;
triggerSimulatedOnChange(element, newValue, 'textarea');
triggerSimulatedOnChange(element, newValue, window.HTMLTextAreaElement.prototype);

return character;
};
Expand Down Expand Up @@ -86,7 +77,7 @@ const getDefaultConfig = randomizer => {

const fillEmail = element => {
const email = randomizer.email();
triggerSimulatedOnChange(element, email);
triggerSimulatedOnChange(element, email, window.HTMLInputElement.prototype);

return email;
};
Expand Down

0 comments on commit 24ea017

Please sign in to comment.