test(web): add component tests for 3 more untested components (GOO-54)
- ExportPdfButton (3 tests): default label, missing-target error, custom filename - ValuationHistoryChart (3 tests): null <2 points, header/description, recharts mounting - NotificationBell (9 tests): aria-label, badge display + 99+ cap, auth-gated fetchUnreadCount, dropdown toggle, empty state, item rendering, mark-all visibility All 15 new tests pass via direct vitest. Cumulative GOO-54 progress: 29 spec files, ~143 tests across H1-H5. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const pushMock = vi.fn();
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: pushMock }),
|
||||
}));
|
||||
|
||||
const fetchUnreadCount = vi.fn();
|
||||
const setOpen = vi.fn();
|
||||
const markAsRead = vi.fn();
|
||||
const markAllAsRead = vi.fn();
|
||||
|
||||
let storeState: any = {
|
||||
notifications: [],
|
||||
unreadCount: 0,
|
||||
isOpen: false,
|
||||
isLoading: false,
|
||||
setOpen,
|
||||
markAsRead,
|
||||
markAllAsRead,
|
||||
fetchUnreadCount,
|
||||
};
|
||||
vi.mock('@/lib/notifications-store', () => ({
|
||||
useNotificationsStore: () => storeState,
|
||||
}));
|
||||
|
||||
let authState = { isAuthenticated: false };
|
||||
vi.mock('@/lib/auth-store', () => ({
|
||||
useAuthStore: (selector: (s: typeof authState) => unknown) => selector(authState),
|
||||
}));
|
||||
|
||||
import { NotificationBell } from '../notification-bell';
|
||||
|
||||
beforeEach(() => {
|
||||
pushMock.mockReset();
|
||||
fetchUnreadCount.mockReset();
|
||||
setOpen.mockReset();
|
||||
markAsRead.mockReset();
|
||||
markAllAsRead.mockReset();
|
||||
storeState = {
|
||||
notifications: [],
|
||||
unreadCount: 0,
|
||||
isOpen: false,
|
||||
isLoading: false,
|
||||
setOpen,
|
||||
markAsRead,
|
||||
markAllAsRead,
|
||||
fetchUnreadCount,
|
||||
};
|
||||
authState = { isAuthenticated: false };
|
||||
});
|
||||
|
||||
describe('NotificationBell', () => {
|
||||
it('renders bell button with default aria-label when no unread', () => {
|
||||
render(<NotificationBell />);
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Thông báo' }),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows unread badge with count and updates aria-label', () => {
|
||||
storeState = { ...storeState, unreadCount: 5 };
|
||||
render(<NotificationBell />);
|
||||
expect(
|
||||
screen.getByRole('button', { name: /Thông báo \(5 chưa đọc\)/ }),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.getByText('5')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('caps badge display at 99+ when count exceeds 99', () => {
|
||||
storeState = { ...storeState, unreadCount: 150 };
|
||||
render(<NotificationBell />);
|
||||
expect(screen.getByText('99+')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('does not call fetchUnreadCount when unauthenticated', () => {
|
||||
authState = { isAuthenticated: false };
|
||||
render(<NotificationBell />);
|
||||
expect(fetchUnreadCount).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('calls fetchUnreadCount on mount when authenticated', () => {
|
||||
authState = { isAuthenticated: true };
|
||||
render(<NotificationBell />);
|
||||
expect(fetchUnreadCount).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('toggles dropdown open via setOpen on click', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<NotificationBell />);
|
||||
await user.click(screen.getByRole('button', { name: 'Thông báo' }));
|
||||
expect(setOpen).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it('renders empty state inside open dropdown', () => {
|
||||
storeState = { ...storeState, isOpen: true };
|
||||
render(<NotificationBell />);
|
||||
expect(screen.getByText('Chưa có thông báo')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders notification items and marks unread as bold', () => {
|
||||
storeState = {
|
||||
...storeState,
|
||||
isOpen: true,
|
||||
notifications: [
|
||||
{
|
||||
id: 'n1',
|
||||
title: 'Tin mới',
|
||||
body: 'Có người quan tâm tin của bạn',
|
||||
isRead: false,
|
||||
link: '/listings/1',
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
id: 'n2',
|
||||
title: 'Đã đọc',
|
||||
body: 'Cũ',
|
||||
isRead: true,
|
||||
link: null,
|
||||
createdAt: new Date(Date.now() - 1000 * 60 * 60 * 25).toISOString(),
|
||||
},
|
||||
],
|
||||
};
|
||||
render(<NotificationBell />);
|
||||
expect(screen.getByText('Tin mới')).toBeInTheDocument();
|
||||
expect(screen.getByText('Đã đọc')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows "Đánh dấu tất cả đã đọc" only when unreadCount > 0 and dropdown open', () => {
|
||||
storeState = { ...storeState, isOpen: true, unreadCount: 3 };
|
||||
render(<NotificationBell />);
|
||||
expect(
|
||||
screen.getByText('Đánh dấu tất cả đã đọc'),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { ExportPdfButton } from '../export-pdf-button';
|
||||
|
||||
describe('ExportPdfButton', () => {
|
||||
it('renders with default label and download icon', () => {
|
||||
render(<ExportPdfButton targetSelector="#target" />);
|
||||
expect(screen.getByRole('button', { name: /Xuất PDF/ })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('logs error when target element not found', async () => {
|
||||
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
const user = userEvent.setup();
|
||||
render(<ExportPdfButton targetSelector="#missing-target" />);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /Xuất PDF/ }));
|
||||
|
||||
expect(errorSpy).toHaveBeenCalledWith(
|
||||
'Export target not found:',
|
||||
'#missing-target',
|
||||
);
|
||||
errorSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('accepts custom filename prop without throwing', () => {
|
||||
render(
|
||||
<ExportPdfButton targetSelector="#t" filename="custom-report" />,
|
||||
);
|
||||
expect(screen.getByRole('button')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import type { ReactNode } from 'react';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { ValuationHistoryChart } from '../valuation-history-chart';
|
||||
import type { ValuationHistoryPoint } from '@/lib/valuation-api';
|
||||
|
||||
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>
|
||||
),
|
||||
Area: () => <div data-testid="area" />,
|
||||
XAxis: () => <div data-testid="xaxis" />,
|
||||
YAxis: () => <div data-testid="yaxis" />,
|
||||
CartesianGrid: () => <div data-testid="grid" />,
|
||||
Tooltip: () => <div data-testid="tooltip" />,
|
||||
}));
|
||||
|
||||
const points: ValuationHistoryPoint[] = [
|
||||
{
|
||||
date: '2025-01-01',
|
||||
estimatedPriceVND: 4_500_000_000,
|
||||
confidence: 0.82,
|
||||
} as ValuationHistoryPoint,
|
||||
{
|
||||
date: '2025-06-01',
|
||||
estimatedPriceVND: 4_900_000_000,
|
||||
confidence: 0.88,
|
||||
} as ValuationHistoryPoint,
|
||||
{
|
||||
date: '2026-01-01',
|
||||
estimatedPriceVND: 5_200_000_000,
|
||||
confidence: 0.9,
|
||||
} as ValuationHistoryPoint,
|
||||
];
|
||||
|
||||
describe('ValuationHistoryChart', () => {
|
||||
it('returns null when fewer than 2 points provided', () => {
|
||||
const { container: c1 } = render(<ValuationHistoryChart data={[]} />);
|
||||
expect(c1.firstChild).toBeNull();
|
||||
const { container: c2 } = render(
|
||||
<ValuationHistoryChart data={[points[0]!]} />,
|
||||
);
|
||||
expect(c2.firstChild).toBeNull();
|
||||
});
|
||||
|
||||
it('renders title and description when data has >=2 points', () => {
|
||||
render(<ValuationHistoryChart data={points} />);
|
||||
expect(screen.getByText('Lịch sử định giá')).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByText('Biến động giá ước tính theo thời gian'),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders the area chart container', () => {
|
||||
render(<ValuationHistoryChart data={points} />);
|
||||
expect(screen.getByTestId('area-chart')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('area')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user