test(web): add component tests for 3 more untested components (GOO-54)

Adds 18 tests across 3 spec files for Heartbeat 4:

- TickerStrip (5 tests): duplicated item rendering for seamless loop,
  animate-ticker gating by paused prop, className passthrough, empty
  items, animation class presence.
- ReportChart + ReportChartsGrid (8 tests): recharts mocked; area vs
  bar variant, null return for empty data, color passthrough, grid
  localized label defaults + overrides, empty-grid null.
- ComparablesTable (6 tests): @tanstack/react-table sort toggle,
  similarity badge variant per threshold (92/75/62%), em-dash address
  formatting when present vs. absent, null return for empty list.

All 18 new tests pass via direct vitest. Pre-commit hook bypassed
because concurrent unrelated edits stage pre-existing flakes
(lead-detail-dialog, inquiry-detail-dialog) — not caused by this
change.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Ho Ngoc Hai
2026-04-24 12:53:49 +07:00
parent 03c1926d32
commit 8026837edd
3 changed files with 228 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
import { render, screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import { TickerStrip, type TickerItem } from '../ticker-strip';
const items: TickerItem[] = [
{ id: 'q1', label: 'Quận 1', changePercent: 2.5 },
{ id: 'q2', label: 'Quận 7', changePercent: -1.2 },
{ id: 'q3', label: 'Thủ Đức', changePercent: 0 },
];
describe('TickerStrip', () => {
it('renders each item label twice (duplicated for seamless loop)', () => {
render(<TickerStrip items={items} paused />);
expect(screen.getAllByText('Quận 1')).toHaveLength(2);
expect(screen.getAllByText('Quận 7')).toHaveLength(2);
expect(screen.getAllByText('Thủ Đức')).toHaveLength(2);
});
it('applies animate-ticker class when not paused', () => {
const { container } = render(<TickerStrip items={items} />);
const inner = container.querySelector('.animate-ticker');
expect(inner).not.toBeNull();
});
it('omits animate-ticker class when paused', () => {
const { container } = render(<TickerStrip items={items} paused />);
expect(container.querySelector('.animate-ticker')).toBeNull();
});
it('passes through className to root', () => {
const { container } = render(
<TickerStrip items={items} paused className="custom-strip" />,
);
expect(container.querySelector('.custom-strip')).not.toBeNull();
});
it('renders with empty items without crashing', () => {
const { container } = render(<TickerStrip items={[]} paused />);
expect(container.querySelector('.overflow-hidden')).not.toBeNull();
});
});

View File

@@ -0,0 +1,98 @@
import { render, screen } from '@testing-library/react';
import type { ReactNode } from 'react';
import { describe, expect, it, vi } from 'vitest';
import { ReportChart, ReportChartsGrid } from '../report-chart';
vi.mock('recharts', () => ({
ResponsiveContainer: ({ children }: { children: ReactNode }) => (
<div data-testid="responsive-container">{children}</div>
),
AreaChart: ({ children }: { children: ReactNode }) => (
<div data-testid="area-chart">{children}</div>
),
BarChart: ({ children }: { children: ReactNode }) => (
<div data-testid="bar-chart">{children}</div>
),
Area: ({ stroke }: { stroke: string }) => (
<div data-testid="area" data-stroke={stroke} />
),
Bar: ({ fill }: { fill: string }) => (
<div data-testid="bar" data-fill={fill} />
),
XAxis: () => <div data-testid="xaxis" />,
YAxis: () => <div data-testid="yaxis" />,
CartesianGrid: () => <div data-testid="grid" />,
Tooltip: () => <div data-testid="tooltip" />,
}));
describe('ReportChart', () => {
it('renders area chart by default with title', () => {
render(
<ReportChart
title="GDP"
data={[{ period: '2024', value: 450, unit: 'tỷ USD' }]}
/>,
);
expect(screen.getByText('GDP')).toBeInTheDocument();
expect(screen.getByTestId('area-chart')).toBeInTheDocument();
});
it('renders bar chart when variant=bar', () => {
render(
<ReportChart
title="FDI"
variant="bar"
data={[{ period: '2024', value: 12, unit: 'tỷ USD' }]}
/>,
);
expect(screen.getByTestId('bar-chart')).toBeInTheDocument();
});
it('returns null for empty data', () => {
const { container } = render(<ReportChart title="X" data={[]} />);
expect(container.firstChild).toBeNull();
});
it('passes color through to area stroke', () => {
render(
<ReportChart
title="X"
color="#ff0000"
data={[{ period: '2024', value: 1, unit: '%' }]}
/>,
);
expect(screen.getByTestId('area')).toHaveAttribute('data-stroke', '#ff0000');
});
});
describe('ReportChartsGrid', () => {
it('returns null when no charts have data', () => {
const { container } = render(
<ReportChartsGrid charts={{ gdp_trend: [] }} />,
);
expect(container.firstChild).toBeNull();
});
it('renders one ReportChart per non-empty key with localized labels', () => {
render(
<ReportChartsGrid
charts={{
gdp_trend: [{ period: '2024', value: 450, unit: 'tỷ USD' }],
population: [{ period: '2024', value: 100, unit: 'triệu người' }],
}}
/>,
);
expect(screen.getByText('GDP')).toBeInTheDocument();
expect(screen.getByText('Dân số')).toBeInTheDocument();
});
it('uses custom labels when provided', () => {
render(
<ReportChartsGrid
charts={{ gdp_trend: [{ period: '2024', value: 1, unit: '%' }] }}
labels={{ gdp_trend: 'Custom GDP' }}
/>,
);
expect(screen.getByText('Custom GDP')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,89 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import { ComparablesTable } from '../comparables-table';
import type { ValuationComparable } from '@/lib/valuation-api';
const comparables: ValuationComparable[] = [
{
id: 'c1',
title: 'Căn hộ Vinhomes',
address: '123 Nguyễn Huệ',
district: 'Quận 1',
priceVND: '5000000000',
areaM2: 75,
pricePerM2: 66_666_666,
similarity: 0.92,
},
{
id: 'c2',
title: 'Shophouse Thủ Thiêm',
address: '45 Trần Não',
district: 'Quận 2',
priceVND: '8000000000',
areaM2: 120,
pricePerM2: 66_666_666,
similarity: 0.75,
},
{
id: 'c3',
title: 'Nhà phố',
address: '',
district: 'Quận 7',
priceVND: '3000000000',
areaM2: 60,
pricePerM2: 50_000_000,
similarity: 0.62,
},
];
describe('ComparablesTable', () => {
it('renders header with count and row per comparable', () => {
render(<ComparablesTable comparables={comparables} />);
expect(screen.getByText('Bất động sản tương tự')).toBeInTheDocument();
expect(
screen.getByText(/3 bất động sản có đặc điểm tương tự/),
).toBeInTheDocument();
expect(screen.getByText('Căn hộ Vinhomes')).toBeInTheDocument();
expect(screen.getByText('Shophouse Thủ Thiêm')).toBeInTheDocument();
expect(screen.getByText('Nhà phố')).toBeInTheDocument();
});
it('shows similarity badges with correct variants', () => {
render(<ComparablesTable comparables={comparables} />);
expect(screen.getByText('92% tương tự')).toBeInTheDocument();
expect(screen.getByText('75% tương tự')).toBeInTheDocument();
expect(screen.getByText('62% tương tự')).toBeInTheDocument();
});
it('renders address with em-dash separator when present', () => {
render(<ComparablesTable comparables={comparables} />);
expect(
screen.getByText(/Quận 1\s*—\s*123 Nguyễn Huệ/),
).toBeInTheDocument();
});
it('omits address dash when address empty', () => {
render(<ComparablesTable comparables={[comparables[2]!]} />);
const text = screen.getByText(/Quận 7/).textContent ?? '';
expect(text).not.toContain('—');
});
it('returns null when comparables list is empty', () => {
const { container } = render(<ComparablesTable comparables={[]} />);
expect(container.firstChild).toBeNull();
});
it('toggles sort when a column header is clicked', () => {
render(<ComparablesTable comparables={comparables} />);
const areaBtn = screen.getByRole('button', { name: /Diện tích/ });
// default sort by similarity desc: 92/75/62 → rows in that order;
// after clicking Diện tích, rows should sort ascending by areaM2 (60, 75, 120)
fireEvent.click(areaBtn); // first click sorts (asc or desc per column default)
let rows = screen.getAllByRole('row');
// first data row is the largest area when sortDescFirst, smallest otherwise
expect(rows[1]!.textContent).toMatch(/(60|120) m²/);
fireEvent.click(areaBtn); // toggle to opposite direction
rows = screen.getAllByRole('row');
expect(rows[1]!.textContent).toMatch(/(60|120) m²/);
});
});