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.

5.4 KB
Raw Blame
import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { createSignal } from 'solid-js';
import { SearchField } from '@/components/shared/SearchField';
import { getSearchFieldInputPaddingRightClass } from '@/components/shared/searchFieldModel';

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

  it('renders the shortcut hint when empty', () => {
    render(() => (
      <SearchField value="" onChange={vi.fn()} placeholder="Search field" shortcutHint="Cmd+K" />
    ));

    expect(screen.getByText('Cmd+K')).toBeInTheDocument();
    expect(screen.getByPlaceholderText('Search field')).toHaveClass('min-h-11');
    expect(screen.getByPlaceholderText('Search field')).toHaveClass('sm:min-h-10');
  });

  it('forwards combobox ownership and active-option semantics', () => {
    render(() => (
      <SearchField
        value=""
        onChange={vi.fn()}
        placeholder="Command search"
        role="combobox"
        ariaAutocomplete="list"
        ariaControls="command-results"
        ariaExpanded={true}
        ariaActiveDescendant="command-result-active"
      />
    ));

    const input = screen.getByRole('combobox', { name: 'Command search' });
    expect(input).toHaveAttribute('aria-autocomplete', 'list');
    expect(input).toHaveAttribute('aria-controls', 'command-results');
    expect(input).toHaveAttribute('aria-expanded', 'true');
    expect(input).toHaveAttribute('aria-activedescendant', 'command-result-active');
  });

  it('reserves mobile input space for every visible trailing action', () => {
    expect(
      getSearchFieldInputPaddingRightClass({
        trailingControlCount: 1,
        showShortcutHint: false,
        showClearButton: true,
      }),
    ).toBe('pr-24 sm:pr-20');
    expect(
      getSearchFieldInputPaddingRightClass({
        trailingControlCount: 2,
        showShortcutHint: false,
        showClearButton: true,
      }),
    ).toBe('pr-36 sm:pr-24');
  });

  it('clears and blurs on focused Escape by default', async () => {
    const Harness = () => {
      const [value, setValue] = createSignal('alpha');
      return <SearchField value={value()} onChange={setValue} placeholder="Escape field" />;
    };

    render(() => <Harness />);

    const input = screen.getByPlaceholderText('Escape field');
    input.focus();
    await fireEvent.keyDown(input, { key: 'Escape' });

    expect(input).toHaveValue('');
    expect(document.activeElement).not.toBe(input);
  });

  it('preserves value on focused Escape when disabled', async () => {
    const Harness = () => {
      const [value, setValue] = createSignal('alpha');
      return (
        <SearchField
          value={value()}
          onChange={setValue}
          placeholder="Stable field"
          clearOnFocusedEscape={false}
        />
      );
    };

    render(() => <Harness />);

    const input = screen.getByPlaceholderText('Stable field');
    input.focus();
    await fireEvent.keyDown(input, { key: 'Escape' });

    expect(input).toHaveValue('alpha');
    expect(document.activeElement).toBe(input);
  });

  it('renders trailing controls alongside the clear button', () => {
    render(() => (
      <SearchField
        value="alpha"
        onChange={vi.fn()}
        placeholder="Trailing field"
        hasTrailingControls
        trailingControls={<button type="button">Extra</button>}
      />
    ));

    expect(screen.getByRole('button', { name: 'Clear search' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Clear search' })).toHaveClass('h-11');
    expect(screen.getByRole('button', { name: 'Clear search' })).toHaveClass('w-11');
    expect(screen.getByRole('button', { name: 'Extra' })).toBeInTheDocument();
  });

  it('invokes explicit keyboard and blur handlers with the input event target', async () => {
    const onKeyDown = vi.fn((event: KeyboardEvent) => event.key);
    const onBlur = vi.fn();

    render(() => (
      <SearchField
        value="alpha"
        onChange={vi.fn()}
        placeholder="Handler field"
        onKeyDown={onKeyDown}
        onBlur={onBlur}
      />
    ));

    const input = screen.getByPlaceholderText('Handler field');
    await fireEvent.keyDown(input, { key: 'Enter' });
    await fireEvent.blur(input);

    expect(onKeyDown).toHaveBeenCalledTimes(1);
    expect(onKeyDown.mock.calls[0][0].currentTarget).toBe(input);
    expect(onKeyDown.mock.results[0].value).toBe('Enter');
    expect(onBlur).toHaveBeenCalledTimes(1);
    expect(onBlur.mock.calls[0][0].currentTarget).toBe(input);
  });

  it('lets explicit keyboard handlers claim the native event', () => {
    const onKeyDown = vi.fn((event: KeyboardEvent) => {
      event.preventDefault();
      event.stopPropagation();
    });
    const onParentKeyDown = vi.fn();

    render(() => (
      <div onKeyDown={onParentKeyDown}>
        <SearchField
          value="alpha"
          onChange={vi.fn()}
          placeholder="Claimed field"
          onKeyDown={onKeyDown}
          clearOnFocusedEscape={false}
        />
      </div>
    ));

    const input = screen.getByPlaceholderText('Claimed field');
    const event = new KeyboardEvent('keydown', {
      key: 'Escape',
      bubbles: true,
      cancelable: true,
    });
    input.dispatchEvent(event);

    expect(onKeyDown).toHaveBeenCalledTimes(1);
    expect(event.defaultPrevented).toBe(true);
    expect(onParentKeyDown).not.toHaveBeenCalled();
  });
});