Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

2.5 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';

vi.mock('../../services/api', () => ({ getDailyAlcohol: vi.fn() }));
vi.mock('../../hooks/useChartColors.js', () => ({ default: () => ({ grid: '#000', axis: '#000' }) }));
// Recharts needs layout; a marker stands in for "a chart was drawn".
vi.mock('recharts', () => {
  return {
    ResponsiveContainer: ({ children }) => <div data-testid="chart">{children}</div>,
    BarChart: ({ data }) => <div data-testid="bars" data-n={data.length} />, Bar: () => null, XAxis: () => null, YAxis: () => null,
    CartesianGrid: () => null, Tooltip: () => null, ReferenceLine: () => null
  };
});
import AlcoholChart from './AlcoholChart';
import * as api from '../../services/api';

afterEach(cleanup);

describe('AlcoholChart load failure vs empty', () => {
  it('hides the chart (no zero-filled days) when the read rejects, and redraws on retry', async () => {
    api.getDailyAlcohol.mockRejectedValueOnce(new Error('boom'));
    render(<AlcoholChart />);
    expect(await screen.findByText(/Could not load daily alcohol\./)).toBeInTheDocument();
    expect(screen.queryByTestId('chart')).not.toBeInTheDocument();
    expect(api.getDailyAlcohol.mock.calls.at(-1)[2]).toEqual({ silent: true });
    api.getDailyAlcohol.mockResolvedValueOnce([]);
    fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
    expect(await screen.findByTestId('chart')).toBeInTheDocument();
  });

  it('draws the chart for a successful empty payload', async () => {
    api.getDailyAlcohol.mockResolvedValueOnce([]);
    render(<AlcoholChart />);
    expect(await screen.findByTestId('chart')).toBeInTheDocument();
    expect(screen.queryByText(/Could not load daily alcohol/)).not.toBeInTheDocument();
  });
});

describe('AlcoholChart superseded loads (#12951)', () => {
  it('draws the selected range when an earlier range resolves last', async () => {
    let resolveMonth;
    api.getDailyAlcohol.mockImplementationOnce(() => new Promise((resolve) => { resolveMonth = resolve; }));
    api.getDailyAlcohol.mockResolvedValueOnce([]);
    render(<AlcoholChart />);
    fireEvent.click(screen.getByRole('button', { name: '7 Days' }));
    const bars = await screen.findByTestId('bars');
    expect(bars).toHaveAttribute('data-n', '8');

    resolveMonth([]);
    await new Promise((resolve) => setTimeout(resolve, 0));
    expect(screen.getByTestId('bars')).toHaveAttribute('data-n', '8');
  });
});