Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

2.9 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanup, render, screen, waitFor } from '@solidjs/testing-library';
import {
  Tooltip,
  createTooltipSystem,
  showTooltip,
  hideTooltip,
} from '@/components/shared/Tooltip';

describe('Tooltip', () => {
  afterEach(() => {
    cleanup();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
  });

  it('suppresses the singleton tooltip on a coarse touch pointer', () => {
    vi.stubGlobal(
      'matchMedia',
      vi.fn().mockReturnValue({
        matches: false,
        media: '(hover: hover) and (pointer: fine)',
      }),
    );

    const TooltipRoot = createTooltipSystem();
    render(() => <TooltipRoot />);

    showTooltip('touch detail', 120, 80);
    expect(screen.queryByText('touch detail')).toBeNull();
  });

  it('sanitizes tooltip content through the model owner', async () => {
    render(() => <Tooltip content={`<b>"unsafe"</b> & 'quoted'`} x={24} y={24} visible />);

    const tooltip = document.body.querySelector('[data-tooltip="true"]') as HTMLDivElement | null;
    expect(tooltip).not.toBeNull();
    if (!tooltip) return;

    await waitFor(() => {
      expect(tooltip.textContent).toBe(`<b>"unsafe"</b> & 'quoted'`);
      expect(tooltip.innerHTML).not.toContain('<b>');
      expect(tooltip.innerHTML).toContain('&lt;b&gt;');
    });
  });

  it('clamps tooltip position inside the viewport', async () => {
    const getBoundingClientRect = vi
      .spyOn(HTMLDivElement.prototype, 'getBoundingClientRect')
      .mockReturnValue({
        width: 180,
        height: 60,
        left: 0,
        top: 0,
        right: 180,
        bottom: 60,
        x: 0,
        y: 0,
        toJSON: () => ({}),
      } as DOMRect);

    const raf = vi
      .spyOn(window, 'requestAnimationFrame')
      .mockImplementation((callback: FrameRequestCallback) => {
        callback(0);
        return 1;
      });

    render(() => <Tooltip content="CPU" x={2} y={2} visible />);

    const tooltip = document.body.querySelector('[data-tooltip="true"]') as HTMLDivElement | null;
    expect(tooltip).not.toBeNull();
    if (!tooltip) return;

    await waitFor(() => {
      const portal = tooltip.closest('foreignObject');
      expect(portal).not.toBeNull();
      expect(Number.parseFloat(portal?.getAttribute('x') ?? '0')).toBeGreaterThanOrEqual(4);
      expect(Number.parseFloat(portal?.getAttribute('y') ?? '0')).toBeGreaterThanOrEqual(4);
    });

    expect(raf).toHaveBeenCalled();
    expect(getBoundingClientRect).toHaveBeenCalled();
  });

  it('preserves the singleton tooltip API', async () => {
    const TooltipRoot = createTooltipSystem();
    render(() => <TooltipRoot />);

    showTooltip('disk', 120, 80, { direction: 'down' });
    expect(await screen.findByText('disk')).toBeInTheDocument();

    hideTooltip();

    await waitFor(() => {
      expect(screen.queryByText('disk')).toBeNull();
    });
  });
});