A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/test/lib/react-trigger-change.js

162 lines
5.1 KiB

// Trigger React's synthetic change events on input, textarea and select elements
// https://github.com/vitalyq/react-trigger-change
6 years ago
/** ****************IMPORTANT NOTE******************/
/* This file is a modification of the */
/* 'react-trigger-change' library linked above. */
/* That library breaks when 'onFocus' events are */
/* added to components under test because it */
/* dispatches focus events to ensure changes are */
/* triggered in some versions of IE. */
/* This modification removes the accomodations */
/* 'react-trigger-change' makes for IE to ensure */
/* our tests can pass in chrome and firefox. */
6 years ago
/** ************************************************/
6 years ago
'use strict'
// Constants and functions are declared inside the closure.
// In this way, reactTriggerChange can be passed directly to executeScript in Selenium.
6 years ago
module.exports = function reactTriggerChange (node) {
const supportedInputTypes = {
color: true,
date: true,
datetime: true,
'datetime-local': true,
email: true,
month: true,
number: true,
password: true,
range: true,
search: true,
tel: true,
text: true,
time: true,
url: true,
6 years ago
week: true,
}
const nodeName = node.nodeName.toLowerCase()
const type = node.type
let event
let descriptor
let initialValue
let initialChecked
let initialCheckedRadio
// Do not try to delete non-configurable properties.
// Value and checked properties on DOM elements are non-configurable in PhantomJS.
6 years ago
function deletePropertySafe (elem, prop) {
const desc = Object.getOwnPropertyDescriptor(elem, prop)
if (desc && desc.configurable) {
6 years ago
delete elem[prop]
}
}
6 years ago
function getCheckedRadio (radio) {
const name = radio.name
let radios
let i
if (name) {
6 years ago
radios = document.querySelectorAll('input[type="radio"][name="' + name + '"]')
for (i = 0; i < radios.length; i += 1) {
if (radios[i].checked) {
6 years ago
return radios[i] !== radio ? radios[i] : null
}
}
}
6 years ago
return null
}
6 years ago
function preventChecking (e) {
e.preventDefault()
if (!initialChecked) {
6 years ago
e.target.checked = false
}
if (initialCheckedRadio) {
6 years ago
initialCheckedRadio.checked = true
}
}
if (nodeName === 'select' ||
(nodeName === 'input' && type === 'file')) {
// IE9-IE11, non-IE
// Dispatch change.
6 years ago
event = document.createEvent('HTMLEvents')
event.initEvent('change', true, false)
node.dispatchEvent(event)
} else if ((nodeName === 'input' && supportedInputTypes[type]) ||
nodeName === 'textarea') {
// React 16
// Cache artificial value property descriptor.
// Property doesn't exist in React <16, descriptor is undefined.
6 years ago
descriptor = Object.getOwnPropertyDescriptor(node, 'value')
// Update inputValueTracking cached value.
// Remove artificial value property.
// Restore initial value to trigger event with it.
6 years ago
initialValue = node.value
node.value = initialValue + '#'
deletePropertySafe(node, 'value')
node.value = initialValue
// React 0.14: IE10-IE11, non-IE
// React 15: non-IE
// React 16: IE10-IE11, non-IE
6 years ago
event = document.createEvent('HTMLEvents')
event.initEvent('input', true, false)
node.dispatchEvent(event)
// React 16
// Restore artificial value property descriptor.
if (descriptor) {
6 years ago
Object.defineProperty(node, 'value', descriptor)
}
} else if (nodeName === 'input' && type === 'checkbox') {
// Invert inputValueTracking cached value.
6 years ago
node.checked = !node.checked
// Dispatch click.
// Click event inverts checked value.
6 years ago
event = document.createEvent('MouseEvents')
event.initEvent('click', true, true)
node.dispatchEvent(event)
} else if (nodeName === 'input' && type === 'radio') {
// Cache initial checked value.
6 years ago
initialChecked = node.checked
// Find and cache initially checked radio in the group.
6 years ago
initialCheckedRadio = getCheckedRadio(node)
// React 16
// Cache property descriptor.
// Invert inputValueTracking cached value.
// Remove artificial checked property.
// Restore initial value, otherwise preventDefault will eventually revert the value.
6 years ago
descriptor = Object.getOwnPropertyDescriptor(node, 'checked')
node.checked = !initialChecked
deletePropertySafe(node, 'checked')
node.checked = initialChecked
// Prevent toggling during event capturing phase.
// Set checked value to false if initialChecked is false,
// otherwise next listeners will see true.
// Restore initially checked radio in the group.
6 years ago
node.addEventListener('click', preventChecking, true)
// Dispatch click.
// Click event inverts checked value.
6 years ago
event = document.createEvent('MouseEvents')
event.initEvent('click', true, true)
node.dispatchEvent(event)
// Remove listener to stop further change prevention.
6 years ago
node.removeEventListener('click', preventChecking, true)
// React 16
// Restore artificial checked property descriptor.
if (descriptor) {
6 years ago
Object.defineProperty(node, 'checked', descriptor)
}
}
6 years ago
}