acceptanceTestTooltip.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {useEffect, useState} from 'react';
  2. import {DO_NOT_USE_TOOLTIP, InternalTooltipProps} from 'sentry/components/tooltip';
  3. import domId from 'sentry/utils/domId';
  4. type Tooltip = {
  5. id: string;
  6. setOpen: (isOpen: boolean) => void;
  7. };
  8. /**
  9. * XXX: This is NOT a reflux store.
  10. *
  11. * This is purely used for acceptance tests where we want to open all tooltips
  12. * in the DOM at once for capturing visual snapshots of tooltips being open.
  13. */
  14. class TooltipStore {
  15. tooltips = new Map<string, Tooltip>();
  16. constructor() {
  17. window.__openAllTooltips = this.openAllTooltips;
  18. window.__closeAllTooltips = this.closeAllTooltips;
  19. }
  20. /**
  21. * Called via window.__openAllTooltips in selenium tests to check tooltip snapshots
  22. */
  23. openAllTooltips = () => {
  24. for (const tooltip of this.tooltips.values()) {
  25. tooltip.setOpen(true);
  26. }
  27. return true;
  28. };
  29. /**
  30. * Called via window.__closeAllTooltips in selenium tests to cleanup tooltips
  31. * after taking a snapshot
  32. */
  33. closeAllTooltips = () => {
  34. for (const tooltip of this.tooltips.values()) {
  35. tooltip.setOpen(false);
  36. }
  37. return true;
  38. };
  39. addTooltip(id: string, setOpen: Tooltip['setOpen']) {
  40. this.tooltips.set(id, {id, setOpen});
  41. }
  42. removeTooltip(id: string) {
  43. this.tooltips.delete(id);
  44. }
  45. }
  46. const tooltipStore = new TooltipStore();
  47. export function AcceptanceTestTooltip(props: InternalTooltipProps) {
  48. const [forceVisible, setForceVisible] = useState<undefined | boolean>(undefined);
  49. useEffect(() => {
  50. const tooltipId = domId('tooltip-');
  51. tooltipStore.addTooltip(tooltipId, setForceVisible);
  52. return () => {
  53. tooltipStore.removeTooltip(tooltipId);
  54. };
  55. }, []);
  56. return <DO_NOT_USE_TOOLTIP forceVisible={forceVisible} {...props} />;
  57. }