spyDomListeners.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { ClientFunction as exec } from 'testcafe'
  2. export const installDomListenerListener = exec(() => {
  3. function eql (a, b) {
  4. const aType = typeof a
  5. const bType = typeof b
  6. switch (aType) {
  7. case 'boolean':
  8. return bType === 'boolean' && b === a
  9. case 'undefined':
  10. return bType === 'undefined'
  11. case 'object':
  12. if (a === null) {
  13. return b === null
  14. }
  15. if (b === null) {
  16. return false
  17. }
  18. if (Object.keys(a).length !== Object.keys(b).length) {
  19. return false
  20. }
  21. for (const key of Object.keys(a)) {
  22. if (a[key] !== b[key]) {
  23. return false
  24. }
  25. }
  26. return true
  27. }
  28. return false
  29. }
  30. function spyAddListener (proto) {
  31. const addEventListener = proto.addEventListener
  32. proto.addEventListener = function (type, listener, options) {
  33. if (!this.__listeners) {
  34. this.__listeners = {}
  35. }
  36. if (!this.__listeners[type]) {
  37. this.__listeners[type] = []
  38. }
  39. this.__listeners[type].push({ listener, options })
  40. return addEventListener.apply(this, arguments)
  41. }
  42. }
  43. function spyRemoveListener (proto) {
  44. const removeEventListener = proto.removeEventListener
  45. proto.removeEventListener = function (type, listener, options) {
  46. if (this.__listeners && this.__listeners[type]) {
  47. const arr = this.__listeners[type]
  48. for (let i = arr.length - 1; i >= 0; i--) {
  49. const { listener: otherListener, options: otherOptions } = arr[i]
  50. if (listener === otherListener && eql(options, otherOptions)) {
  51. arr.splice(i, 1)
  52. }
  53. }
  54. }
  55. return removeEventListener.apply(this, arguments)
  56. }
  57. }
  58. function spy (proto) {
  59. spyAddListener(proto)
  60. spyRemoveListener(proto)
  61. }
  62. spy(Element.prototype)
  63. spy(document)
  64. spy(window)
  65. })
  66. export const getNumDomListeners = exec(() => {
  67. function getNumListeners (obj) {
  68. let sum = 0
  69. if (obj.__listeners) {
  70. for (const key of Object.keys(obj.__listeners)) {
  71. sum += obj.__listeners[key].length
  72. }
  73. }
  74. return sum
  75. }
  76. return [...document.querySelectorAll('*')]
  77. .concat([window, document])
  78. .map(getNumListeners)
  79. .reduce((a, b) => a + b, 0)
  80. })