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.9 KB
Raw Blame
import { createSignal, For } from 'solid-js';
import { cleanup, render } from '@solidjs/testing-library';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { formatPercent } from '@/utils/format';
import { AnimatedNumber } from './AnimatedNumber';
import { MAX_CONCURRENT_ANIMATED_NUMBER_ENTRIES } from './useAnimatedNumberState';

let queuedFrames: FrameRequestCallback[] = [];
let reducedMotionMatches = false;

function installMotionRuntime() {
  queuedFrames = [];
  Object.defineProperty(window, 'requestAnimationFrame', {
    configurable: true,
    value: vi.fn((callback: FrameRequestCallback) => {
      queuedFrames.push(callback);
      return queuedFrames.length;
    }),
  });
  Object.defineProperty(window, 'cancelAnimationFrame', {
    configurable: true,
    value: vi.fn(),
  });
  Object.defineProperty(window, 'matchMedia', {
    configurable: true,
    value: vi.fn(() => ({
      addEventListener: vi.fn(),
      addListener: vi.fn(),
      matches: reducedMotionMatches,
      media: '(prefers-reduced-motion: reduce)',
      removeEventListener: vi.fn(),
      removeListener: vi.fn(),
    })),
  });
}

function runNextFrame(timestamp: number) {
  const callback = queuedFrames.shift();
  if (!callback) {
    throw new Error('No queued animation frame');
  }
  callback(timestamp);
}

function getReadout(container: HTMLElement): HTMLElement {
  const readout = container.querySelector('[data-animated-number="true"]');
  if (!readout) {
    throw new Error('Animated number readout not found');
  }
  return readout as HTMLElement;
}

describe('AnimatedNumber', () => {
  beforeEach(() => {
    reducedMotionMatches = false;
    installMotionRuntime();
  });

  afterEach(() => {
    cleanup();
    vi.restoreAllMocks();
  });

  it('renders the target value immediately on first paint', () => {
    const { container } = render(() => <AnimatedNumber value={42} format={formatPercent} />);
    const readout = getReadout(container);
    expect(readout).toHaveTextContent('42%');
    expect(readout).toHaveAttribute('aria-label', '42%');
    expect(queuedFrames).toHaveLength(0);
  });

  it('eases changed values through the shared frame loop', () => {
    const [value, setValue] = createSignal(10);
    const { container } = render(() => <AnimatedNumber value={value()} format={formatPercent} />);
    const readout = getReadout(container);

    setValue(50);
    expect(readout).toHaveAttribute('aria-label', '50%');
    expect(readout).toHaveTextContent('10%');

    runNextFrame(0);
    expect(readout).toHaveTextContent('10%');

    runNextFrame(160);
    expect(readout).toHaveTextContent('45%');

    runNextFrame(320);
    expect(readout).toHaveTextContent('50%');
  });

  it('snaps changed values when reduced motion is preferred', () => {
    reducedMotionMatches = true;
    installMotionRuntime();
    const [value, setValue] = createSignal(10);
    const { container } = render(() => <AnimatedNumber value={value()} format={formatPercent} />);
    const readout = getReadout(container);

    setValue(50);
    expect(readout).toHaveTextContent('50%');
    expect(readout).toHaveAttribute('aria-label', '50%');
    expect(queuedFrames).toHaveLength(0);
  });

  it('bounds concurrent animation work when many readouts change together', () => {
    const indices = Array.from(
      { length: MAX_CONCURRENT_ANIMATED_NUMBER_ENTRIES + 1 },
      (_, index) => index,
    );
    const [value, setValue] = createSignal(10);
    const { container } = render(() => (
      <For each={indices}>{() => <AnimatedNumber value={value()} format={formatPercent} />}</For>
    ));

    setValue(50);

    const readouts = Array.from(container.querySelectorAll('[data-animated-number="true"]'));
    expect(queuedFrames).toHaveLength(1);
    expect(readouts.filter((readout) => readout.textContent === '10%')).toHaveLength(
      MAX_CONCURRENT_ANIMATED_NUMBER_ENTRIES,
    );
    expect(readouts.filter((readout) => readout.textContent === '50%')).toHaveLength(1);
  });
});