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.

4.8 KB
Raw Blame
import { fireEvent, render, screen } from '@solidjs/testing-library';
import { describe, expect, it, vi } from 'vitest';
import { SummaryRowActionButton } from '@/components/shared/SummaryRowActionButton';

describe('SummaryRowActionButton', () => {
  it.each(['disclosure', 'scope'] as const)(
    'prevents a second native Space activation for %s [single-space-activation]',
    (kind) => {
      const onAction = vi.fn();
      render(() =>
        kind === 'disclosure' ? (
          <SummaryRowActionButton
            kind="disclosure"
            subjectLabel="alpha"
            expanded={false}
            hideWhenRowTappableOnMobile={false}
            onAction={onAction}
          />
        ) : (
          <SummaryRowActionButton
            kind="scope"
            subjectLabel="alpha"
            pressed={false}
            onAction={onAction}
          />
        ),
      );
      const button = screen.getByRole('button');
      fireEvent.keyDown(button, { key: ' ', code: 'Space' });
      expect(onAction).toHaveBeenCalledTimes(1);
      const keyup = new KeyboardEvent('keyup', {
        key: ' ',
        code: 'Space',
        bubbles: true,
        cancelable: true,
      });
      expect(button.dispatchEvent(keyup)).toBe(false);
      expect(keyup.defaultPrevented).toBe(true);
      expect(onAction).toHaveBeenCalledTimes(1);

      fireEvent.keyDown(button, { key: 'Enter', code: 'Enter' });
      expect(onAction).toHaveBeenCalledTimes(2);
      const enterUp = new KeyboardEvent('keyup', {
        key: 'Enter',
        code: 'Enter',
        bubbles: true,
        cancelable: true,
      });
      expect(button.dispatchEvent(enterUp)).toBe(true);
    },
  );

  it('owns disclosure semantics with aria-expanded and aria-controls', () => {
    const onAction = vi.fn();
    render(() => (
      <SummaryRowActionButton
        kind="disclosure"
        subjectLabel="alpha"
        expanded={true}
        controlsId="summary-row-detail-alpha"
        hideWhenRowTappableOnMobile={false}
        onAction={onAction}
      />
    ));

    const button = screen.getByRole('button', { name: 'Collapse alpha' });
    expect(button).toHaveClass('h-9');
    expect(button).toHaveClass('w-9');
    expect(button).toHaveClass('sm:h-6');
    expect(button).toHaveClass('sm:w-6');
    // A visible phone chevron overhangs the row instead of setting a 37px
    // row height; the 36px target stays.
    expect(button).toHaveClass('-mx-1', '-my-2.5', 'sm:m-0');
    expect(button).toHaveAttribute('aria-expanded', 'true');
    expect(button).toHaveAttribute('aria-controls', 'summary-row-detail-alpha');

    fireEvent.click(button);
    expect(onAction).toHaveBeenCalledTimes(1);
  });

  it('owns pinned scope semantics with aria-pressed', () => {
    const onAction = vi.fn();
    render(() => (
      <SummaryRowActionButton
        kind="scope"
        subjectLabel="tower"
        pressed={true}
        onAction={onAction}
      />
    ));

    const button = screen.getByRole('button', {
      name: 'Unpin summary scope for tower',
    });
    expect(button).toHaveAttribute('aria-pressed', 'true');
    expect(button).toHaveTextContent('Pinned');

    fireEvent.click(button);
    expect(onAction).toHaveBeenCalledTimes(1);
  });

  it('clears preview and blurs on Escape', () => {
    const onPreviewClear = vi.fn();
    render(() => (
      <SummaryRowActionButton
        kind="scope"
        subjectLabel="tower"
        pressed={false}
        onAction={vi.fn()}
        onPreviewClear={onPreviewClear}
      />
    ));

    const button = screen.getByRole('button', {
      name: 'Pin summary scope for tower',
    }) as HTMLButtonElement;
    button.blur = vi.fn();

    fireEvent.keyDown(button, { key: 'Escape' });
    expect(onPreviewClear).toHaveBeenCalledTimes(1);
    expect(button.blur).toHaveBeenCalledTimes(1);
  });

  it('activates disclosure buttons from keyboard space', () => {
    const onAction = vi.fn();
    render(() => (
      <SummaryRowActionButton
        kind="disclosure"
        subjectLabel="alpha"
        expanded={false}
        controlsId="summary-row-detail-alpha"
        hideWhenRowTappableOnMobile={false}
        onAction={onAction}
      />
    ));

    const button = screen.getByRole('button', { name: 'Expand alpha' });
    fireEvent.keyDown(button, { key: 'Space', code: 'Space' });
    expect(onAction).toHaveBeenCalledTimes(1);
  });

  it('removes redundant mobile chrome when the whole row is tappable', () => {
    render(() => (
      <SummaryRowActionButton
        kind="disclosure"
        subjectLabel="alpha"
        expanded={false}
        hideWhenRowTappableOnMobile
        onAction={vi.fn()}
      />
    ));

    const button = screen.getByRole('button', { name: 'Expand alpha' });
    expect(button).toHaveClass('sr-only');
    expect(button).toHaveClass('focus:not-sr-only');
    expect(button).toHaveClass('sm:not-sr-only');
  });
});