138 lines
4.0 KiB
TypeScript
138 lines
4.0 KiB
TypeScript
import { ContactMapper } from '../../../../src/Application/DTOs/Mappers/ContactMapper';
|
|
import { ContactType, ContactState } from '../../../../src/Domain/Contact/ContactAggregate';
|
|
|
|
describe('ContactMapper', () => {
|
|
const createMockContact = (overrides: any = {}) => ({
|
|
id: 'contact-123',
|
|
name: 'John Doe',
|
|
email: 'john.doe@example.com',
|
|
userid: 'user-456',
|
|
type: ContactType.QUESTION,
|
|
txt: 'This is a test contact message.',
|
|
state: ContactState.ACTIVE,
|
|
createDate: new Date('2024-01-01'),
|
|
updateDate: new Date('2024-01-02'),
|
|
adminResponse: null,
|
|
responseDate: null,
|
|
respondedBy: null,
|
|
...overrides
|
|
});
|
|
|
|
describe('toShortDto', () => {
|
|
it('should map ContactAggregate to ShortContactDto correctly', () => {
|
|
// Arrange
|
|
const contact = createMockContact();
|
|
|
|
// Act
|
|
const result = ContactMapper.toShortDto(contact);
|
|
|
|
// Assert
|
|
expect(result).toEqual({
|
|
id: 'contact-123',
|
|
name: 'John Doe',
|
|
email: 'john.doe@example.com',
|
|
type: ContactType.QUESTION,
|
|
createDate: new Date('2024-01-01'),
|
|
state: ContactState.ACTIVE,
|
|
});
|
|
});
|
|
|
|
it('should handle different contact types', () => {
|
|
// Arrange
|
|
const bugContact = createMockContact({
|
|
id: 'bug-contact',
|
|
type: ContactType.BUG,
|
|
name: 'Bug Reporter'
|
|
});
|
|
|
|
// Act
|
|
const result = ContactMapper.toShortDto(bugContact);
|
|
|
|
// Assert
|
|
expect(result.type).toBe(ContactType.BUG);
|
|
expect(result.name).toBe('Bug Reporter');
|
|
});
|
|
});
|
|
|
|
describe('toDetailDto', () => {
|
|
it('should map ContactAggregate to DetailContactDto correctly', () => {
|
|
// Arrange
|
|
const contact = createMockContact();
|
|
|
|
// Act
|
|
const result = ContactMapper.toDetailDto(contact);
|
|
|
|
// Assert
|
|
expect(result).toEqual({
|
|
id: 'contact-123',
|
|
name: 'John Doe',
|
|
email: 'john.doe@example.com',
|
|
userid: 'user-456',
|
|
type: ContactType.QUESTION,
|
|
txt: 'This is a test contact message.',
|
|
state: ContactState.ACTIVE,
|
|
createDate: new Date('2024-01-01'),
|
|
updateDate: new Date('2024-01-02'),
|
|
adminResponse: null,
|
|
responseDate: null,
|
|
respondedBy: null,
|
|
});
|
|
});
|
|
|
|
it('should handle contact with admin response', () => {
|
|
// Arrange
|
|
const respondedContact = createMockContact({
|
|
adminResponse: 'Thank you for your question. Here is the answer...',
|
|
responseDate: new Date('2024-01-03'),
|
|
respondedBy: 'admin-789'
|
|
});
|
|
|
|
// Act
|
|
const result = ContactMapper.toDetailDto(respondedContact);
|
|
|
|
// Assert
|
|
expect(result.adminResponse).toBe('Thank you for your question. Here is the answer...');
|
|
expect(result.responseDate).toEqual(new Date('2024-01-03'));
|
|
expect(result.respondedBy).toBe('admin-789');
|
|
});
|
|
});
|
|
|
|
describe('toShortDtoList', () => {
|
|
it('should map array of ContactAggregate to array of ShortContactDto', () => {
|
|
// Arrange
|
|
const contacts = [
|
|
createMockContact({ id: 'contact-1', name: 'First Contact' }),
|
|
createMockContact({ id: 'contact-2', name: 'Second Contact', type: ContactType.BUG }),
|
|
createMockContact({ id: 'contact-3', name: 'Third Contact', type: ContactType.SALES })
|
|
];
|
|
|
|
// Act
|
|
const result = ContactMapper.toShortDtoList(contacts);
|
|
|
|
// Assert
|
|
expect(result).toHaveLength(3);
|
|
expect(result[0]).toEqual({
|
|
id: 'contact-1',
|
|
name: 'First Contact',
|
|
email: 'john.doe@example.com',
|
|
type: ContactType.QUESTION,
|
|
createDate: new Date('2024-01-01'),
|
|
state: ContactState.ACTIVE,
|
|
});
|
|
expect(result[1].type).toBe(ContactType.BUG);
|
|
expect(result[2].type).toBe(ContactType.SALES);
|
|
});
|
|
|
|
it('should handle empty array', () => {
|
|
// Arrange
|
|
const contacts: any[] = [];
|
|
|
|
// Act
|
|
const result = ContactMapper.toShortDtoList(contacts);
|
|
|
|
// Assert
|
|
expect(result).toEqual([]);
|
|
});
|
|
});
|
|
});
|