2018-03-19 16:06:09 +01:00
|
|
|
// Trigger React's synthetic change events on input, textarea and select elements
|
|
|
|
// https://github.com/vitalyq/react-trigger-change
|
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
/** ****************IMPORTANT NOTE******************/
|
2018-03-19 16:06:09 +01:00
|
|
|
/* 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. */
|
2018-07-03 00:49:33 +02:00
|
|
|
/** ************************************************/
|
2018-03-19 16:06:09 +01:00
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
'use strict'
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// Constants and functions are declared inside the closure.
|
|
|
|
// In this way, reactTriggerChange can be passed directly to executeScript in Selenium.
|
2018-07-03 00:49:33 +02:00
|
|
|
module.exports = function reactTriggerChange (node) {
|
2018-03-19 16:06:09 +01:00
|
|
|
var 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,
|
2018-07-03 00:49:33 +02:00
|
|
|
week: true,
|
|
|
|
}
|
|
|
|
var nodeName = node.nodeName.toLowerCase()
|
|
|
|
var type = node.type
|
|
|
|
var event
|
|
|
|
var descriptor
|
|
|
|
var initialValue
|
|
|
|
var initialChecked
|
|
|
|
var initialCheckedRadio
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// Do not try to delete non-configurable properties.
|
|
|
|
// Value and checked properties on DOM elements are non-configurable in PhantomJS.
|
2018-07-03 00:49:33 +02:00
|
|
|
function deletePropertySafe (elem, prop) {
|
|
|
|
var desc = Object.getOwnPropertyDescriptor(elem, prop)
|
2018-03-19 16:06:09 +01:00
|
|
|
if (desc && desc.configurable) {
|
2018-07-03 00:49:33 +02:00
|
|
|
delete elem[prop]
|
2018-03-19 16:06:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
function getCheckedRadio (radio) {
|
|
|
|
var name = radio.name
|
|
|
|
var radios
|
|
|
|
var i
|
2018-03-19 16:06:09 +01:00
|
|
|
if (name) {
|
2018-07-03 00:49:33 +02:00
|
|
|
radios = document.querySelectorAll('input[type="radio"][name="' + name + '"]')
|
2018-03-19 16:06:09 +01:00
|
|
|
for (i = 0; i < radios.length; i += 1) {
|
|
|
|
if (radios[i].checked) {
|
2018-07-03 00:49:33 +02:00
|
|
|
return radios[i] !== radio ? radios[i] : null
|
2018-03-19 16:06:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 00:49:33 +02:00
|
|
|
return null
|
2018-03-19 16:06:09 +01:00
|
|
|
}
|
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
function preventChecking (e) {
|
|
|
|
e.preventDefault()
|
2018-03-19 16:06:09 +01:00
|
|
|
if (!initialChecked) {
|
2018-07-03 00:49:33 +02:00
|
|
|
e.target.checked = false
|
2018-03-19 16:06:09 +01:00
|
|
|
}
|
|
|
|
if (initialCheckedRadio) {
|
2018-07-03 00:49:33 +02:00
|
|
|
initialCheckedRadio.checked = true
|
2018-03-19 16:06:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nodeName === 'select' ||
|
|
|
|
(nodeName === 'input' && type === 'file')) {
|
|
|
|
// IE9-IE11, non-IE
|
|
|
|
// Dispatch change.
|
2018-07-03 00:49:33 +02:00
|
|
|
event = document.createEvent('HTMLEvents')
|
|
|
|
event.initEvent('change', true, false)
|
|
|
|
node.dispatchEvent(event)
|
2018-03-19 16:06:09 +01:00
|
|
|
} 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.
|
2018-07-03 00:49:33 +02:00
|
|
|
descriptor = Object.getOwnPropertyDescriptor(node, 'value')
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// Update inputValueTracking cached value.
|
|
|
|
// Remove artificial value property.
|
|
|
|
// Restore initial value to trigger event with it.
|
2018-07-03 00:49:33 +02:00
|
|
|
initialValue = node.value
|
|
|
|
node.value = initialValue + '#'
|
|
|
|
deletePropertySafe(node, 'value')
|
|
|
|
node.value = initialValue
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// React 0.14: IE10-IE11, non-IE
|
|
|
|
// React 15: non-IE
|
|
|
|
// React 16: IE10-IE11, non-IE
|
2018-07-03 00:49:33 +02:00
|
|
|
event = document.createEvent('HTMLEvents')
|
|
|
|
event.initEvent('input', true, false)
|
|
|
|
node.dispatchEvent(event)
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// React 16
|
|
|
|
// Restore artificial value property descriptor.
|
|
|
|
if (descriptor) {
|
2018-07-03 00:49:33 +02:00
|
|
|
Object.defineProperty(node, 'value', descriptor)
|
2018-03-19 16:06:09 +01:00
|
|
|
}
|
|
|
|
} else if (nodeName === 'input' && type === 'checkbox') {
|
|
|
|
// Invert inputValueTracking cached value.
|
2018-07-03 00:49:33 +02:00
|
|
|
node.checked = !node.checked
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// Dispatch click.
|
|
|
|
// Click event inverts checked value.
|
2018-07-03 00:49:33 +02:00
|
|
|
event = document.createEvent('MouseEvents')
|
|
|
|
event.initEvent('click', true, true)
|
|
|
|
node.dispatchEvent(event)
|
2018-03-19 16:06:09 +01:00
|
|
|
} else if (nodeName === 'input' && type === 'radio') {
|
|
|
|
// Cache initial checked value.
|
2018-07-03 00:49:33 +02:00
|
|
|
initialChecked = node.checked
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// Find and cache initially checked radio in the group.
|
2018-07-03 00:49:33 +02:00
|
|
|
initialCheckedRadio = getCheckedRadio(node)
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// React 16
|
|
|
|
// Cache property descriptor.
|
|
|
|
// Invert inputValueTracking cached value.
|
|
|
|
// Remove artificial checked property.
|
|
|
|
// Restore initial value, otherwise preventDefault will eventually revert the value.
|
2018-07-03 00:49:33 +02:00
|
|
|
descriptor = Object.getOwnPropertyDescriptor(node, 'checked')
|
|
|
|
node.checked = !initialChecked
|
|
|
|
deletePropertySafe(node, 'checked')
|
|
|
|
node.checked = initialChecked
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// 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.
|
2018-07-03 00:49:33 +02:00
|
|
|
node.addEventListener('click', preventChecking, true)
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// Dispatch click.
|
|
|
|
// Click event inverts checked value.
|
2018-07-03 00:49:33 +02:00
|
|
|
event = document.createEvent('MouseEvents')
|
|
|
|
event.initEvent('click', true, true)
|
|
|
|
node.dispatchEvent(event)
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// Remove listener to stop further change prevention.
|
2018-07-03 00:49:33 +02:00
|
|
|
node.removeEventListener('click', preventChecking, true)
|
2018-03-19 16:06:09 +01:00
|
|
|
|
|
|
|
// React 16
|
|
|
|
// Restore artificial checked property descriptor.
|
|
|
|
if (descriptor) {
|
2018-07-03 00:49:33 +02:00
|
|
|
Object.defineProperty(node, 'checked', descriptor)
|
2018-03-19 16:06:09 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 00:49:33 +02:00
|
|
|
}
|