|
| 1 | +import {wrapAsync} from '../wrap-async' |
| 2 | +import { |
| 3 | + fireEvent, |
| 4 | + getMouseEventOptions, |
| 5 | + getPreviouslyFocusedElement, |
| 6 | +} from './utils' |
| 7 | + |
| 8 | +async function clickLabel(label, init) { |
| 9 | + await fireEvent.mouseOver(label, getMouseEventOptions('mouseover', init)) |
| 10 | + await fireEvent.mouseMove(label, getMouseEventOptions('mousemove', init)) |
| 11 | + await fireEvent.mouseDown(label, getMouseEventOptions('mousedown', init)) |
| 12 | + await fireEvent.mouseUp(label, getMouseEventOptions('mouseup', init)) |
| 13 | + await fireEvent.click(label, getMouseEventOptions('click', init)) |
| 14 | + |
| 15 | + // clicking the label will trigger a click of the label.control |
| 16 | + // however, it will not focus the label.control so we have to do it |
| 17 | + // ourselves. |
| 18 | + if (label.control) label.control.focus() |
| 19 | +} |
| 20 | + |
| 21 | +async function clickBooleanElement(element, init) { |
| 22 | + if (element.disabled) return |
| 23 | + |
| 24 | + await fireEvent.mouseOver(element, getMouseEventOptions('mouseover', init)) |
| 25 | + await fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init)) |
| 26 | + await fireEvent.mouseDown(element, getMouseEventOptions('mousedown', init)) |
| 27 | + await fireEvent.focus(element) |
| 28 | + await fireEvent.mouseUp(element, getMouseEventOptions('mouseup', init)) |
| 29 | + await fireEvent.click(element, getMouseEventOptions('click', init)) |
| 30 | +} |
| 31 | + |
| 32 | +async function clickElement(element, previousElement, init) { |
| 33 | + await fireEvent.mouseOver(element, getMouseEventOptions('mouseover', init)) |
| 34 | + await fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init)) |
| 35 | + const continueDefaultHandling = await fireEvent.mouseDown( |
| 36 | + element, |
| 37 | + getMouseEventOptions('mousedown', init), |
| 38 | + ) |
| 39 | + const shouldFocus = element.ownerDocument.activeElement !== element |
| 40 | + if (continueDefaultHandling) { |
| 41 | + if (previousElement) previousElement.blur() |
| 42 | + if (shouldFocus) element.focus() |
| 43 | + } |
| 44 | + await fireEvent.mouseUp(element, getMouseEventOptions('mouseup', init)) |
| 45 | + await fireEvent.click(element, getMouseEventOptions('click', init, 1)) |
| 46 | + const parentLabel = element.closest('label') |
| 47 | + if (parentLabel?.control) parentLabel?.control.focus?.() |
| 48 | +} |
| 49 | + |
| 50 | +async function dblClickElement(element, previousElement, init) { |
| 51 | + await fireEvent.mouseOver(element, getMouseEventOptions('mouseover', init)) |
| 52 | + await fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init)) |
| 53 | + const continueDefaultHandling = await fireEvent.mouseDown( |
| 54 | + element, |
| 55 | + getMouseEventOptions('mousedown', init), |
| 56 | + ) |
| 57 | + const shouldFocus = element.ownerDocument.activeElement !== element |
| 58 | + if (continueDefaultHandling) { |
| 59 | + if (previousElement) previousElement.blur() |
| 60 | + if (shouldFocus) element.focus() |
| 61 | + } |
| 62 | + await fireEvent.mouseUp(element, getMouseEventOptions('mouseup', init)) |
| 63 | + await fireEvent.click(element, getMouseEventOptions('click', init, 1)) |
| 64 | + const parentLabel = element.closest('label') |
| 65 | + if (parentLabel?.control) parentLabel?.control.focus?.() |
| 66 | + |
| 67 | + await fireEvent.mouseDown(element, getMouseEventOptions('mousedown', init, 1)) |
| 68 | + await fireEvent.mouseUp(element, getMouseEventOptions('mouseup', init, 1)) |
| 69 | + await fireEvent.click(element, getMouseEventOptions('click', init, 2)) |
| 70 | + await fireEvent.dblClick(element, getMouseEventOptions('dblclick', init, 2)) |
| 71 | +} |
| 72 | + |
| 73 | +async function dblClickCheckbox(checkbox, init) { |
| 74 | + await fireEvent.mouseOver(checkbox, getMouseEventOptions('mouseover', init)) |
| 75 | + await fireEvent.mouseMove(checkbox, getMouseEventOptions('mousemove', init)) |
| 76 | + await fireEvent.mouseDown(checkbox, getMouseEventOptions('mousedown', init)) |
| 77 | + await fireEvent.focus(checkbox) |
| 78 | + await fireEvent.mouseUp(checkbox, getMouseEventOptions('mouseup', init)) |
| 79 | + await fireEvent.click(checkbox, getMouseEventOptions('click', init, 1)) |
| 80 | + await fireEvent.mouseDown( |
| 81 | + checkbox, |
| 82 | + getMouseEventOptions('mousedown', init, 1), |
| 83 | + ) |
| 84 | + await fireEvent.mouseUp(checkbox, getMouseEventOptions('mouseup', init, 1)) |
| 85 | + await fireEvent.click(checkbox, getMouseEventOptions('click', init, 2)) |
| 86 | +} |
| 87 | + |
| 88 | +async function click(element, init) { |
| 89 | + const previouslyFocusedElement = getPreviouslyFocusedElement(element) |
| 90 | + if (previouslyFocusedElement) { |
| 91 | + await fireEvent.mouseMove( |
| 92 | + previouslyFocusedElement, |
| 93 | + getMouseEventOptions('mousemove', init), |
| 94 | + ) |
| 95 | + await fireEvent.mouseLeave( |
| 96 | + previouslyFocusedElement, |
| 97 | + getMouseEventOptions('mouseleave', init), |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + switch (element.tagName) { |
| 102 | + case 'LABEL': |
| 103 | + await clickLabel(element, init) |
| 104 | + break |
| 105 | + case 'INPUT': |
| 106 | + if (element.type === 'checkbox' || element.type === 'radio') { |
| 107 | + await clickBooleanElement(element, init) |
| 108 | + break |
| 109 | + } |
| 110 | + // eslint-disable-next-line no-fallthrough |
| 111 | + default: |
| 112 | + await clickElement(element, previouslyFocusedElement, init) |
| 113 | + } |
| 114 | +} |
| 115 | +click = wrapAsync(click) |
| 116 | + |
| 117 | +async function dblClick(element, init) { |
| 118 | + const previouslyFocusedElement = getPreviouslyFocusedElement(element) |
| 119 | + if (previouslyFocusedElement) { |
| 120 | + await fireEvent.mouseMove( |
| 121 | + previouslyFocusedElement, |
| 122 | + getMouseEventOptions('mousemove', init), |
| 123 | + ) |
| 124 | + await fireEvent.mouseLeave( |
| 125 | + previouslyFocusedElement, |
| 126 | + getMouseEventOptions('mouseleave', init), |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + switch (element.tagName) { |
| 131 | + case 'INPUT': |
| 132 | + if (element.type === 'checkbox') { |
| 133 | + await dblClickCheckbox(element, previouslyFocusedElement, init) |
| 134 | + break |
| 135 | + } |
| 136 | + // eslint-disable-next-line no-fallthrough |
| 137 | + default: |
| 138 | + await dblClickElement(element, previouslyFocusedElement, init) |
| 139 | + } |
| 140 | +} |
| 141 | +dblClick = wrapAsync(dblClick) |
| 142 | + |
| 143 | +export { |
| 144 | + click, |
| 145 | + dblClick, |
| 146 | + clickLabel, |
| 147 | + clickBooleanElement, |
| 148 | + clickElement, |
| 149 | + dblClickElement, |
| 150 | + dblClickCheckbox, |
| 151 | +} |
0 commit comments