import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';
import type { InquiryReadDto } from '@/lib/inquiries-api';
import { InquiryDetailDialog } from '../inquiry-detail-dialog';
// Mock the hook
const mockMarkReadMutate = vi.fn();
vi.mock('@/lib/hooks/use-inquiries', () => ({
useMarkInquiryRead: () => ({
mutate: mockMarkReadMutate,
isPending: false,
}),
}));
// Mock InquiryStatusBadge
vi.mock('@/components/inquiries/inquiry-row', () => ({
InquiryStatusBadge: ({ isRead }: { isRead: boolean }) => (
{isRead ? 'Đã đọc' : 'Chưa đọc'}
),
}));
// Mock Dialog
vi.mock('@/components/ui/dialog', () => ({
Dialog: ({ children, open }: { children: React.ReactNode; open: boolean }) =>
open ?
{children}
: null,
DialogContent: ({ children }: { children: React.ReactNode }) => {children}
,
DialogHeader: ({ children }: { children: React.ReactNode }) => {children}
,
DialogTitle: ({ children }: { children: React.ReactNode }) => {children}
,
DialogDescription: ({ children }: { children: React.ReactNode }) => {children}
,
DialogFooter: ({ children }: { children: React.ReactNode }) => {children}
,
}));
const mockInquiry: InquiryReadDto = {
id: 'inq-1',
listingId: 'listing-1',
listingTitle: 'Căn hộ 3PN Quận 2',
userId: 'user-1',
userName: 'Nguyễn Minh C',
userPhone: '0912345678',
message: 'Tôi muốn xem nhà vào thứ 7 tuần sau',
phone: null,
isRead: false,
createdAt: '2026-02-10T09:00:00Z',
};
describe('InquiryDetailDialog', () => {
beforeEach(() => {
mockMarkReadMutate.mockClear();
});
it('returns null when inquiry is null', () => {
const { container } = render(
,
);
expect(container.firstChild).toBeNull();
});
it('renders dialog title', () => {
render(
,
);
expect(screen.getByText('Chi tiết liên hệ')).toBeInTheDocument();
});
it('renders listing title', () => {
render(
,
);
expect(screen.getByText('Căn hộ 3PN Quận 2')).toBeInTheDocument();
});
it('renders user name', () => {
render(
,
);
expect(screen.getByText('Nguyễn Minh C')).toBeInTheDocument();
});
it('renders phone number', () => {
render(
,
);
expect(screen.getByText(/0912345678/)).toBeInTheDocument();
});
it('renders inquiry message', () => {
render(
,
);
expect(screen.getByText('Tôi muốn xem nhà vào thứ 7 tuần sau')).toBeInTheDocument();
});
it('renders unread status', () => {
render(
,
);
expect(screen.getByText('Chưa đọc')).toBeInTheDocument();
});
it('renders mark as read button when unread', () => {
render(
,
);
expect(screen.getByText('Đánh dấu đã đọc')).toBeInTheDocument();
});
it('does not render mark as read button when already read', () => {
const readInquiry = { ...mockInquiry, isRead: true };
render(
,
);
expect(screen.queryByText('Đánh dấu đã đọc')).not.toBeInTheDocument();
});
it('calls mutate when mark as read is clicked', async () => {
const user = userEvent.setup();
render(
,
);
await user.click(screen.getByText('Đánh dấu đã đọc'));
expect(mockMarkReadMutate).toHaveBeenCalledWith('inq-1', expect.any(Object));
});
it('renders quick contact links', () => {
render(
,
);
// Emoji prefixed text
const content = document.body.textContent;
expect(content).toContain('Gọi điện');
expect(content).toContain('Zalo');
});
it('renders close button', () => {
render(
,
);
expect(screen.getByText('Đóng')).toBeInTheDocument();
});
it('calls onOpenChange when close is clicked', async () => {
const user = userEvent.setup();
const onOpenChange = vi.fn();
render(
,
);
await user.click(screen.getByText('Đóng'));
expect(onOpenChange).toHaveBeenCalledWith(false);
});
it('uses inquiry.phone when available over userPhone', () => {
const inquiryWithPhone = { ...mockInquiry, phone: '0987654321' };
render(
,
);
expect(screen.getByText(/0987654321/)).toBeInTheDocument();
});
});