radix.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Required setup adapted from https://github.com/radix-ui/primitives/issues/856#issuecomment-928704064
  3. *
  4. * Implements a custom PointerEvent that can then be used to trigger the radix dropdown.
  5. * This is required as JSdom has not implemented the PointerEvent (see https://github.com/testing-library/react-testing-library/issues/838#issuecomment-735259406)
  6. *
  7. * Furthermore, ResizeObserver and DomRect are both not available in JSdom (see https://github.com/radix-ui/primitives/issues/856)
  8. *
  9. * Effects of this setup file:
  10. * - sets PointerEvent to window
  11. * - sets ResizeObserver to window
  12. * - sets DOMRect to window
  13. *
  14. * Needed to interact with dropdown components, with the `clickDropdown` helper.
  15. */
  16. class PointerEvent extends Event {
  17. constructor(type, props) {
  18. super(type, props)
  19. if (props.button != null) {
  20. this.button = props.button
  21. }
  22. if (props.ctrlKey != null) {
  23. this.ctrlKey = props.ctrlKey
  24. }
  25. }
  26. }
  27. window.PointerEvent = PointerEvent
  28. window.HTMLElement.prototype.scrollIntoView = function () {}
  29. // // https://github.com/radix-ui/primitives/issues/420#issuecomment-771615182
  30. window.ResizeObserver = class ResizeObserver {
  31. constructor(cb) {
  32. this.cb = cb
  33. }
  34. observe() {}
  35. unobserve() {}
  36. disconnect() {}
  37. }