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.

3.6 KB
Raw Blame
import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { Toggle, TogglePrimitive } from './Toggle';

describe('Toggle', () => {
  afterEach(() => {
    cleanup();
  });

  it('keeps a disabled toggle on its on or off colour, dimmed', () => {
    render(() => (
      <>
        <Toggle checked disabled ariaLabel="Locked on" />
        <Toggle checked={false} disabled ariaLabel="Locked off" />
      </>
    ));

    const lockedOn = screen.getByRole('button', { name: 'Locked on' });
    const lockedOff = screen.getByRole('button', { name: 'Locked off' });
    expect(lockedOn).toHaveClass('bg-blue-500', 'opacity-50', 'cursor-not-allowed');
    expect(lockedOff).toHaveClass('bg-border', 'opacity-50', 'cursor-not-allowed');
    // bg-base names no theme colour, so it rendered no track at all.
    expect(lockedOn.className).not.toContain('bg-base');
    expect(lockedOff.className).not.toContain('bg-base');
  });

  it('emits the synthetic next checked state and respects preventDefault', () => {
    const onToggle = vi.fn();
    const onChange = vi.fn((event) => event.preventDefault());

    render(() => <TogglePrimitive checked={false} onToggle={onToggle} onChange={onChange} />);

    fireEvent.click(screen.getByRole('button'));
    expect(onChange).toHaveBeenCalledTimes(1);
    expect(onChange.mock.calls[0][0].currentTarget.checked).toBe(true);
    expect(onToggle).not.toHaveBeenCalled();
  });

  it('renders label and description through the shell contract', () => {
    render(() => (
      <Toggle checked={true} label={<span>Enabled</span>} description={<span>Turns it on</span>} />
    ));

    expect(screen.getByText('Enabled')).toBeInTheDocument();
    expect(screen.getByText('Turns it on')).toBeInTheDocument();
  });

  it('uses the visible label and description for the primitive button semantics', () => {
    render(() => (
      <Toggle checked={true} label={<span>Enabled</span>} description={<span>Turns it on</span>} />
    ));

    const button = screen.getByRole('button', { name: 'Enabled' });
    expect(button).toHaveAttribute('aria-labelledby');
    expect(button).toHaveAttribute('aria-describedby');
    expect(button).toHaveAccessibleDescription('Turns it on');
  });

  it('supports an external visible label when the layout owns label placement', () => {
    render(() => (
      <>
        <span id="external-toggle-label">Alert-triggered patrols</span>
        <span id="external-toggle-description">Run when alerts fire.</span>
        <Toggle
          checked={true}
          ariaLabelledBy="external-toggle-label"
          ariaDescribedBy="external-toggle-description"
        />
      </>
    ));

    const button = screen.getByRole('button', { name: 'Alert-triggered patrols' });
    expect(button).toHaveAttribute('aria-labelledby', 'external-toggle-label');
    expect(button).toHaveAttribute('aria-describedby', 'external-toggle-description');
    expect(button).toHaveAccessibleDescription('Run when alerts fire.');
  });

  it('combines external context with its visible status label', () => {
    render(() => (
      <>
        <span id="schedule-section-label">Quiet hours</span>
        <Toggle
          checked={false}
          ariaLabelledBy="schedule-section-label"
          label={<span>Disabled</span>}
        />
      </>
    ));

    const button = screen.getByRole('button', { name: 'Quiet hours Disabled' });
    const labelledBy = button.getAttribute('aria-labelledby')?.split(/\s+/) ?? [];
    expect(labelledBy).toContain('schedule-section-label');
    expect(labelledBy).toHaveLength(2);
  });
});