widget-format.test.ts2.3 KBView on GitHub
/**
 * The rail's formatters. The case that matters is NULL vs ZERO: a window with no replies and
 * a window with instant replies are different facts, and printing "0s" for the first is a lie
 * the user acts on.
 */

import {
  formatCount,
  formatCurrency,
  formatDuration,
  formatStageLabel,
} from '@/modules/home/widgets/format';

describe('formatDuration', () => {
  it('keeps null as null rather than collapsing it to zero', () => {
    expect(formatDuration(null)).toBeNull();
    expect(formatDuration(undefined)).toBeNull();
    expect(formatDuration(Number.NaN)).toBeNull();
    // Zero seconds IS a measurement, and must not be swallowed with the missing ones.
    expect(formatDuration(0)).toBe('0s');
  });

  it('steps up to the coarsest unit that still says something', () => {
    expect(formatDuration(45)).toBe('45s');
    expect(formatDuration(90)).toBe('2m');
    expect(formatDuration(60 * 60)).toBe('1h');
    expect(formatDuration(60 * 60 * 2 + 60 * 14)).toBe('2h 14m');
    expect(formatDuration(60 * 60 * 24 * 3)).toBe('3d');
    expect(formatDuration(60 * 60 * 27)).toBe('1d 3h');
  });
});

describe('formatCurrency', () => {
  it('compacts to the rail width', () => {
    expect(formatCurrency(740_000)).toBe('$740k');
    expect(formatCurrency(1_200_000)).toBe('$1.2M');
    expect(formatCurrency(12_000_000)).toBe('$12M');
    expect(formatCurrency(940)).toBe('$940');
  });

  it('keeps null as null', () => {
    expect(formatCurrency(null)).toBeNull();
    expect(formatCurrency(0)).toBe('$0');
  });
});

describe('formatCount', () => {
  it('separates thousands and keeps null as null', () => {
    expect(formatCount(1234)).toBe('1,234');
    expect(formatCount(0)).toBe('0');
    expect(formatCount(null)).toBeNull();
  });
});

describe('formatStageLabel', () => {
  it('tidies a status slug without pretending to know the vocabulary', () => {
    // Statuses are free text from the user's own pipeline, so this only fixes casing and
    // separators — it must never map onto a fixed set it cannot know.
    expect(formatStageLabel('closed_won')).toBe('Closed won');
    expect(formatStageLabel('initial-discovery')).toBe('Initial discovery');
    expect(formatStageLabel('Demo')).toBe('Demo');
    expect(formatStageLabel('   ')).toBe('Unknown');
  });
});