feat: add unit tests for featured listings, neighborhood scores + price history chart

- Add unit tests for FeatureListingHandler (6 tests) and ActivateFeaturedListingHandler (6 tests)
- Add unit tests for NeighborhoodScoreServiceImpl (5 tests) and GetNeighborhoodScoreHandler (2 tests)
- Add PriceHistoryChart component with recharts LineChart for listing detail page
- Wire up price history API client and integrate chart into listing detail view

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Ho Ngoc Hai
2026-04-16 18:21:44 +07:00
parent 0dda2bffdb
commit 8e9d021465
7 changed files with 560 additions and 14 deletions

View File

@@ -0,0 +1,113 @@
import { ActivateFeaturedListingHandler } from '../event-handlers/activate-featured-listing.handler';
describe('ActivateFeaturedListingHandler', () => {
let handler: ActivateFeaturedListingHandler;
let mockPrisma: {
payment: { findUnique: ReturnType<typeof vi.fn> };
listing: { findUnique: ReturnType<typeof vi.fn>; update: ReturnType<typeof vi.fn> };
};
let mockLogger: { log: ReturnType<typeof vi.fn> };
beforeEach(() => {
mockPrisma = {
payment: { findUnique: vi.fn() },
listing: { findUnique: vi.fn(), update: vi.fn() },
};
mockLogger = { log: vi.fn() };
handler = new ActivateFeaturedListingHandler(
mockPrisma as any,
mockLogger as any,
);
});
it('activates featured listing for 7 days on 199000 VND payment', async () => {
mockPrisma.payment.findUnique.mockResolvedValue({
type: 'FEATURED_LISTING',
transactionId: 'listing-1',
amountVND: 199000n,
});
mockPrisma.listing.findUnique.mockResolvedValue({ featuredUntil: null });
mockPrisma.listing.update.mockResolvedValue({});
await handler.handle({ aggregateId: 'pay-1' } as any);
expect(mockPrisma.listing.update).toHaveBeenCalledWith({
where: { id: 'listing-1' },
data: { featuredUntil: expect.any(Date) },
});
const updateCall = mockPrisma.listing.update.mock.calls[0][0];
const featuredUntil = updateCall.data.featuredUntil as Date;
const diffDays = Math.round((featuredUntil.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
expect(diffDays).toBe(7);
});
it('activates featured listing for 3 days on 99000 VND payment', async () => {
mockPrisma.payment.findUnique.mockResolvedValue({
type: 'FEATURED_LISTING',
transactionId: 'listing-1',
amountVND: 99000n,
});
mockPrisma.listing.findUnique.mockResolvedValue({ featuredUntil: null });
mockPrisma.listing.update.mockResolvedValue({});
await handler.handle({ aggregateId: 'pay-1' } as any);
const updateCall = mockPrisma.listing.update.mock.calls[0][0];
const featuredUntil = updateCall.data.featuredUntil as Date;
const diffDays = Math.round((featuredUntil.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
expect(diffDays).toBe(3);
});
it('extends from existing featuredUntil if still in the future', async () => {
const futureDate = new Date(Date.now() + 5 * 24 * 60 * 60 * 1000); // 5 days from now
mockPrisma.payment.findUnique.mockResolvedValue({
type: 'FEATURED_LISTING',
transactionId: 'listing-1',
amountVND: 199000n,
});
mockPrisma.listing.findUnique.mockResolvedValue({ featuredUntil: futureDate });
mockPrisma.listing.update.mockResolvedValue({});
await handler.handle({ aggregateId: 'pay-1' } as any);
const updateCall = mockPrisma.listing.update.mock.calls[0][0];
const featuredUntil = updateCall.data.featuredUntil as Date;
// Should extend from futureDate (5 days out) + 7 days = ~12 days from now
const diffDays = Math.round((featuredUntil.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
expect(diffDays).toBe(12);
});
it('ignores non-FEATURED_LISTING payments', async () => {
mockPrisma.payment.findUnique.mockResolvedValue({
type: 'SUBSCRIPTION',
transactionId: 'listing-1',
amountVND: 199000n,
});
await handler.handle({ aggregateId: 'pay-1' } as any);
expect(mockPrisma.listing.update).not.toHaveBeenCalled();
});
it('ignores payments without transactionId', async () => {
mockPrisma.payment.findUnique.mockResolvedValue({
type: 'FEATURED_LISTING',
transactionId: null,
amountVND: 199000n,
});
await handler.handle({ aggregateId: 'pay-1' } as any);
expect(mockPrisma.listing.update).not.toHaveBeenCalled();
});
it('ignores payments that do not exist', async () => {
mockPrisma.payment.findUnique.mockResolvedValue(null);
await handler.handle({ aggregateId: 'pay-1' } as any);
expect(mockPrisma.listing.update).not.toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,128 @@
import { ListingEntity } from '@modules/listings/domain/entities/listing.entity';
import { type IListingRepository } from '@modules/listings/domain/repositories/listing.repository';
import { Price } from '@modules/listings/domain/value-objects/price.vo';
import { FeatureListingCommand } from '../commands/feature-listing/feature-listing.command';
import { FeatureListingHandler } from '../commands/feature-listing/feature-listing.handler';
function createListing(
id = 'listing-1',
sellerId = 'seller-1',
agentId: string | null = null,
status: 'DRAFT' | 'PENDING_REVIEW' | 'ACTIVE' = 'ACTIVE',
): ListingEntity {
const price = Price.create(2_000_000_000n).unwrap();
const listing = ListingEntity.createNew(id, 'prop-1', sellerId, 'SALE', price, 80, agentId ?? undefined);
if (status === 'PENDING_REVIEW' || status === 'ACTIVE') listing.submitForReview();
if (status === 'ACTIVE') listing.approve();
listing.clearDomainEvents();
return listing;
}
describe('FeatureListingHandler', () => {
let handler: FeatureListingHandler;
let mockListingRepo: Pick<IListingRepository, 'findById'>;
let mockCommandBus: { execute: ReturnType<typeof vi.fn> };
let mockLogger: { log: ReturnType<typeof vi.fn>; error: ReturnType<typeof vi.fn> };
beforeEach(() => {
mockListingRepo = { findById: vi.fn() };
mockCommandBus = {
execute: vi.fn().mockResolvedValue({
paymentId: 'pay-1',
paymentUrl: 'https://pay.example.com/checkout',
providerTxId: 'tx-1',
}),
};
mockLogger = { log: vi.fn(), error: vi.fn() };
handler = new FeatureListingHandler(
mockListingRepo as any,
mockCommandBus as any,
mockLogger as any,
);
});
it('creates payment for a valid feature request', async () => {
const listing = createListing('listing-1', 'seller-1', null, 'ACTIVE');
(mockListingRepo.findById as ReturnType<typeof vi.fn>).mockResolvedValue(listing);
const command = new FeatureListingCommand(
'listing-1', 'seller-1', '7_days', 'VNPAY',
'https://goodgo.vn/callback', '127.0.0.1',
);
const result = await handler.execute(command);
expect(result.paymentId).toBe('pay-1');
expect(result.paymentUrl).toBe('https://pay.example.com/checkout');
expect(result.package_).toBe('7_days');
expect(result.priceVND).toBe('199000');
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
it('allows the assigned agent to feature the listing', async () => {
const listing = createListing('listing-1', 'seller-1', 'agent-1', 'ACTIVE');
(mockListingRepo.findById as ReturnType<typeof vi.fn>).mockResolvedValue(listing);
const command = new FeatureListingCommand(
'listing-1', 'agent-1', '3_days', 'MOMO',
'https://goodgo.vn/callback', '127.0.0.1',
);
const result = await handler.execute(command);
expect(result.paymentId).toBe('pay-1');
expect(result.priceVND).toBe('99000');
});
it('rejects feature request from unauthorized user', async () => {
const listing = createListing('listing-1', 'seller-1', null, 'ACTIVE');
(mockListingRepo.findById as ReturnType<typeof vi.fn>).mockResolvedValue(listing);
const command = new FeatureListingCommand(
'listing-1', 'stranger', '7_days', 'VNPAY',
'https://goodgo.vn/callback', '127.0.0.1',
);
await expect(handler.execute(command)).rejects.toThrow(/người bán|môi giới/);
});
it('rejects feature request for non-ACTIVE listing', async () => {
const listing = createListing('listing-1', 'seller-1', null, 'DRAFT');
(mockListingRepo.findById as ReturnType<typeof vi.fn>).mockResolvedValue(listing);
const command = new FeatureListingCommand(
'listing-1', 'seller-1', '7_days', 'VNPAY',
'https://goodgo.vn/callback', '127.0.0.1',
);
await expect(handler.execute(command)).rejects.toThrow(/hoạt động/);
});
it('throws NotFoundException for non-existent listing', async () => {
(mockListingRepo.findById as ReturnType<typeof vi.fn>).mockResolvedValue(null);
const command = new FeatureListingCommand(
'nonexistent', 'seller-1', '7_days', 'VNPAY',
'https://goodgo.vn/callback', '127.0.0.1',
);
await expect(handler.execute(command)).rejects.toThrow('Listing');
});
it('uses correct pricing for each package', async () => {
const listing = createListing('listing-1', 'seller-1', null, 'ACTIVE');
(mockListingRepo.findById as ReturnType<typeof vi.fn>).mockResolvedValue(listing);
for (const [pkg, expectedPrice] of [
['3_days', '99000'],
['7_days', '199000'],
['30_days', '499000'],
] as const) {
const command = new FeatureListingCommand(
'listing-1', 'seller-1', pkg, 'VNPAY',
'https://goodgo.vn/callback', '127.0.0.1',
);
const result = await handler.execute(command);
expect(result.priceVND).toBe(expectedPrice);
}
});
});