click.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.click = click;
  6. exports.dblClick = dblClick;
  7. var _dom = require("@testing-library/dom");
  8. var _utils = require("./utils");
  9. var _hover = require("./hover");
  10. var _blur = require("./blur");
  11. var _focus = require("./focus");
  12. function getPreviouslyFocusedElement(element) {
  13. const focusedElement = element.ownerDocument.activeElement;
  14. const wasAnotherElementFocused = focusedElement && focusedElement !== element.ownerDocument.body && focusedElement !== element;
  15. return wasAnotherElementFocused ? focusedElement : null;
  16. }
  17. function clickLabel(label, init, {
  18. clickCount
  19. }) {
  20. if ((0, _utils.isLabelWithInternallyDisabledControl)(label)) return;
  21. _dom.fireEvent.pointerDown(label, init);
  22. _dom.fireEvent.mouseDown(label, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
  23. _dom.fireEvent.pointerUp(label, init);
  24. _dom.fireEvent.mouseUp(label, (0, _utils.getMouseEventOptions)('mouseup', init, clickCount));
  25. _dom.fireEvent.click(label, (0, _utils.getMouseEventOptions)('click', init, clickCount)); // clicking the label will trigger a click of the label.control
  26. // however, it will not focus the label.control so we have to do it
  27. // ourselves.
  28. if (label.control) (0, _focus.focus)(label.control);
  29. }
  30. function clickBooleanElement(element, init, clickCount) {
  31. _dom.fireEvent.pointerDown(element, init);
  32. if (!element.disabled) {
  33. _dom.fireEvent.mouseDown(element, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
  34. }
  35. (0, _focus.focus)(element, init);
  36. _dom.fireEvent.pointerUp(element, init);
  37. if (!element.disabled) {
  38. _dom.fireEvent.mouseUp(element, (0, _utils.getMouseEventOptions)('mouseup', init, clickCount));
  39. _dom.fireEvent.click(element, (0, _utils.getMouseEventOptions)('click', init, clickCount));
  40. }
  41. }
  42. function clickElement(element, init, {
  43. clickCount
  44. }) {
  45. const previousElement = getPreviouslyFocusedElement(element);
  46. _dom.fireEvent.pointerDown(element, init);
  47. if (!element.disabled) {
  48. const continueDefaultHandling = _dom.fireEvent.mouseDown(element, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
  49. if (continueDefaultHandling) {
  50. const closestFocusable = findClosest(element, _utils.isFocusable);
  51. if (previousElement && !closestFocusable) {
  52. (0, _blur.blur)(previousElement, init);
  53. } else if (closestFocusable) {
  54. (0, _focus.focus)(closestFocusable, init);
  55. }
  56. }
  57. }
  58. _dom.fireEvent.pointerUp(element, init);
  59. if (!element.disabled) {
  60. _dom.fireEvent.mouseUp(element, (0, _utils.getMouseEventOptions)('mouseup', init, clickCount));
  61. _dom.fireEvent.click(element, (0, _utils.getMouseEventOptions)('click', init, clickCount));
  62. const parentLabel = element.closest('label');
  63. if (parentLabel != null && parentLabel.control) (0, _focus.focus)(parentLabel.control, init);
  64. }
  65. }
  66. function findClosest(el, callback) {
  67. do {
  68. if (callback(el)) {
  69. return el;
  70. }
  71. el = el.parentElement;
  72. } while (el && el !== document.body);
  73. return undefined;
  74. }
  75. function click(element, init, {
  76. skipHover = false,
  77. clickCount = 0
  78. } = {}) {
  79. if (!skipHover) (0, _hover.hover)(element, init);
  80. switch (element.tagName) {
  81. case 'LABEL':
  82. clickLabel(element, init, {
  83. clickCount
  84. });
  85. break;
  86. case 'INPUT':
  87. if (element.type === 'checkbox' || element.type === 'radio') {
  88. clickBooleanElement(element, init, {
  89. clickCount
  90. });
  91. } else {
  92. clickElement(element, init, {
  93. clickCount
  94. });
  95. }
  96. break;
  97. default:
  98. clickElement(element, init, {
  99. clickCount
  100. });
  101. }
  102. }
  103. function dblClick(element, init) {
  104. (0, _hover.hover)(element, init);
  105. click(element, init, {
  106. skipHover: true,
  107. clickCount: 0
  108. });
  109. click(element, init, {
  110. skipHover: true,
  111. clickCount: 1
  112. });
  113. _dom.fireEvent.dblClick(element, (0, _utils.getMouseEventOptions)('dblclick', init, 2));
  114. }