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.

8.2 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from 'vitest';
import { batch, createSignal } from 'solid-js';
import { Dialog } from '@/components/shared/Dialog';
import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
import { Toast, ToastContainer } from '@/components/Toast/Toast';

describe('Toast', () => {
  afterEach(() => {
    cleanup();
    vi.clearAllTimers();
    vi.useRealTimers();
  });

  it.each([
    ['error', 'alert', 'assertive'],
    ['warning', 'alert', 'assertive'],
    ['success', 'status', 'polite'],
    ['info', 'status', 'polite'],
  ] as const)('preserves %s announcement semantics without moving focus', (type, role, live) => {
    render(() => (
      <>
        <button>Recovery action</button>
        <ToastContainer />
      </>
    ));
    const action = screen.getByRole('button', { name: 'Recovery action' });
    action.focus();

    window.showToast(type, 'Recovery result', 'Action context');

    const announcement = screen.getByRole(role);
    expect(announcement).toHaveAttribute('aria-live', live);
    expect(announcement).toHaveAttribute('aria-atomic', 'true');
    expect(announcement).toHaveTextContent('Recovery result');
    expect(announcement).toHaveTextContent('Action context');
    expect(action).toHaveFocus();
    expect(screen.queryByRole(role === 'alert' ? 'status' : 'alert')).not.toBeInTheDocument();
  });

  it('keeps a retained success below blocking dialogs, restoring it after close', () => {
    const [open, setOpen] = createSignal(false);
    render(() => (
      <>
        <ToastContainer />
        <Dialog isOpen={open()} onClose={() => setOpen(false)} ariaLabel="Editor">
          <button onClick={() => setOpen(false)}>Close editor</button>
        </Dialog>
      </>
    ));
    window.showToast('success', 'Previous operation completed');
    const notifications = screen.getByRole('region', { name: 'Notifications' });
    expect(notifications).toHaveClass('z-9999');
    setOpen(true);
    expect(screen.getByRole('dialog')).toBeTruthy();
    expect(notifications).toHaveClass('z-50');
    expect(notifications).not.toHaveClass('z-9999');
    expect(notifications.closest('[inert]')).not.toBeNull();
    fireEvent.click(screen.getByRole('button', { name: 'Close editor' }));
    expect(notifications).toHaveClass('z-9999');
    expect(notifications.closest('[inert]')).toBeNull();
    expect(screen.getByText('Previous operation completed')).toBeInTheDocument();
  });

  it.each(['error', 'warning', 'info'] as const)(
    'keeps %s feedback above an open dialog even beside a retained success',
    (type) => {
      const [open, setOpen] = createSignal(false);
      render(() => (
        <>
          <ToastContainer />
          <Dialog isOpen={open()} onClose={() => setOpen(false)} ariaLabel="Editor">
            <button onClick={() => window.showToast(type, 'Operation needs attention')}>
              Try operation
            </button>
            <button onClick={() => setOpen(false)}>Close editor</button>
          </Dialog>
        </>
      ));
      window.showToast('success', 'Previous operation completed');
      const notifications = screen.getByRole('region', { name: 'Notifications' });
      setOpen(true);
      expect(notifications).toHaveClass('z-50');

      fireEvent.click(screen.getByRole('button', { name: 'Try operation' }));

      expect(notifications).toHaveClass('z-9999');
      expect(notifications).not.toHaveClass('z-50');
      expect(screen.getByText('Operation needs attention')).toBeInTheDocument();
      expect(screen.getByRole('dialog')).toBeInTheDocument();
      // Layering must not weaken the existing modal background isolation.
      expect(notifications.closest('[inert]')).not.toBeNull();
      fireEvent.click(screen.getByRole('button', { name: 'Close editor' }));
      expect(notifications.closest('[inert]')).toBeNull();
      expect(notifications).toHaveClass('z-9999');
      expect(screen.getByText('Previous operation completed')).toBeInTheDocument();
    },
  );

  it('keeps all toasts created within a batch', () => {
    render(() => <ToastContainer />);

    batch(() => {
      window.showToast('info', 'First toast');
      window.showToast('success', 'Second toast');
    });

    expect(screen.getByText('First toast')).toBeInTheDocument();
    expect(screen.getByText('Second toast')).toBeInTheDocument();
  });

  it('does not schedule a second removal after manual close', () => {
    vi.useFakeTimers();
    const onRemove = vi.fn();

    render(() => (
      <Toast
        toast={{ id: 'toast-1', type: 'info', title: 'Manual close toast', duration: 1000 }}
        onRemove={onRemove}
      />
    ));

    fireEvent.click(screen.getByRole('button'));

    vi.advanceTimersByTime(300);
    expect(onRemove).toHaveBeenCalledTimes(1);

    vi.advanceTimersByTime(2000);
    expect(onRemove).toHaveBeenCalledTimes(1);
  });

  it('pauses auto-dismiss while the pointer is over the notification', () => {
    vi.useFakeTimers();
    const onRemove = vi.fn();

    render(() => (
      <Toast
        toast={{ id: 'toast-hover', type: 'info', title: 'Hover toast', duration: 1000 }}
        onRemove={onRemove}
      />
    ));

    vi.advanceTimersByTime(600);
    const toast = screen.getByRole('status');
    fireEvent.pointerEnter(toast);
    vi.advanceTimersByTime(5000);
    expect(onRemove).not.toHaveBeenCalled();

    fireEvent.pointerLeave(toast);
    vi.advanceTimersByTime(699);
    expect(onRemove).not.toHaveBeenCalled();
    vi.advanceTimersByTime(1);
    expect(onRemove).toHaveBeenCalledTimes(1);
  });

  it('pauses auto-dismiss while keyboard focus remains in the notification', () => {
    vi.useFakeTimers();
    const onRemove = vi.fn();

    render(() => (
      <Toast
        toast={{ id: 'toast-focus', type: 'error', title: 'Focus toast', duration: 1000 }}
        onRemove={onRemove}
      />
    ));

    vi.advanceTimersByTime(600);
    const dismissButton = screen.getByRole('button', { name: 'Dismiss notification' });
    dismissButton.focus();
    vi.advanceTimersByTime(5000);
    expect(onRemove).not.toHaveBeenCalled();

    dismissButton.blur();
    vi.advanceTimersByTime(700);
    expect(onRemove).toHaveBeenCalledTimes(1);
  });

  it('fits the notification stack and cards inside a 320px viewport', () => {
    render(() => <ToastContainer />);
    window.showToast('info', 'Narrow viewport toast');

    const region = screen.getByRole('region', { name: 'Notifications' });
    expect(region).toHaveClass('inset-x-3', 'sm:left-auto', 'sm:max-w-[500px]');

    const card = screen.getByText('Narrow viewport toast').closest('.flex');
    expect(card).toHaveClass('w-full', 'min-w-0');
    expect(card).not.toHaveClass('min-w-[300px]');
  });

  it('renders detail field in a collapsible details element', () => {
    const onRemove = vi.fn();
    render(() => (
      <Toast
        toast={{
          id: 'err-1',
          type: 'error',
          title: 'Could not connect to the server',
          detail: 'dial tcp smtp.gmail.com:587: connect: connection refused',
        }}
        onRemove={onRemove}
      />
    ));

    // Title is visible
    expect(screen.getByText('Could not connect to the server')).toBeInTheDocument();

    // Detail is inside a <details> element (collapsed by default)
    const details = document.querySelector('details');
    expect(details).toBeInTheDocument();
    expect(details!.open).toBe(false);

    // The "Details" summary is visible
    expect(screen.getByText('Details')).toBeInTheDocument();

    // The raw error text is in the DOM but inside the collapsed details
    expect(
      screen.getByText('dial tcp smtp.gmail.com:587: connect: connection refused'),
    ).toBeInTheDocument();
  });

  it('renders message as visible subtitle without collapsing', () => {
    const onRemove = vi.fn();
    render(() => (
      <Toast
        toast={{
          id: 'err-2',
          type: 'error',
          title: 'Unable to report merge',
          message: 'The node is currently offline',
        }}
        onRemove={onRemove}
      />
    ));

    // No details element when only message is set (no detail)
    expect(document.querySelector('details')).not.toBeInTheDocument();

    // Message is directly visible as a subtitle
    expect(screen.getByText('The node is currently offline')).toBeInTheDocument();
  });
});