Skip to content

Commit

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

const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
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 @@ -25,15 +34,23 @@ const getDefaultConfig = randomizer => {
const fillTextElement = element => {
const character = randomizer.character();
const newValue = element.value + character;
triggerInputOnChange(element, newValue);
triggerSimulatedOnChange(element, newValue);

return character;
};

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

return character;
};

const fillNumberElement = element => {
const number = randomizer.character({ pool: '0123456789' });
const newValue = element.value + number;
triggerInputOnChange(element, newValue);
triggerSimulatedOnChange(element, newValue);

return number;
};
Expand Down Expand Up @@ -69,13 +86,13 @@ const getDefaultConfig = randomizer => {

const fillEmail = element => {
const email = randomizer.email();
triggerInputOnChange(element, email);
triggerSimulatedOnChange(element, email);

return email;
};

const defaultMapElements = {
textarea: fillTextElement,
textarea: fillTextAreaElement,
'input[type="text"]': fillTextElement,
'input[type="password"]': fillTextElement,
'input[type="number"]': fillNumberElement,
Expand Down

0 comments on commit a877e46

Please sign in to comment.