@ilhamtahir/ts-mapper
Version:
[](https://www.npmjs.com/package/@ilhamtahir/ts-mapper) [](https://www.npmjs.com/package/@ilhamtahir/ts-mapper)
124 lines • 4.89 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../index");
// Test entities and DTOs
class UserEntity {
constructor(data = {}) {
this.id = data.id || 0;
this.fullName = data.fullName || '';
this.email = data.email || '';
this.profile = data.profile || { bio: '', avatar: '' };
}
}
class UserDto {
constructor() {
this.id = 0;
this.name = '';
this.email = '';
this.bio = '';
this.avatar = '';
}
}
let UserMapper = class UserMapper {
toDto(entity) {
return (0, index_1.transform)(this, 'toDto', entity, UserDto);
}
toEntity(dto) {
return (0, index_1.transform)(this, 'toEntity', dto, UserEntity);
}
};
__decorate([
(0, index_1.Mapping)({ source: 'fullName', target: 'name' }),
(0, index_1.Mapping)({ source: 'profile.bio', target: 'bio' }),
(0, index_1.Mapping)({ source: 'profile.avatar', target: 'avatar' }),
__metadata("design:type", Function),
__metadata("design:paramtypes", [UserEntity]),
__metadata("design:returntype", UserDto)
], UserMapper.prototype, "toDto", null);
__decorate([
(0, index_1.Mapping)({ source: 'name', target: 'fullName' }),
(0, index_1.Mapping)({ source: 'bio', target: 'profile.bio' }),
(0, index_1.Mapping)({ source: 'avatar', target: 'profile.avatar' }),
__metadata("design:type", Function),
__metadata("design:paramtypes", [UserDto]),
__metadata("design:returntype", UserEntity)
], UserMapper.prototype, "toEntity", null);
UserMapper = __decorate([
(0, index_1.Mapper)()
], UserMapper);
describe('Mapper', () => {
let mapper;
beforeEach(() => {
mapper = new UserMapper();
});
describe('toDto', () => {
it('should map basic fields correctly', () => {
const entity = new UserEntity({
id: 1,
fullName: 'John Doe',
email: 'john@example.com',
profile: {
bio: 'Software Developer',
avatar: 'avatar.jpg',
},
});
const dto = mapper.toDto(entity);
expect(dto.id).toBe(1);
expect(dto.name).toBe('John Doe');
expect(dto.email).toBe('john@example.com');
expect(dto.bio).toBe('Software Developer');
expect(dto.avatar).toBe('avatar.jpg');
});
it('should handle null nested properties gracefully', () => {
const entity = new UserEntity({
id: 1,
fullName: 'John Doe',
email: 'john@example.com',
profile: null,
});
const dto = mapper.toDto(entity);
expect(dto.id).toBe(1);
expect(dto.name).toBe('John Doe');
expect(dto.email).toBe('john@example.com');
expect(dto.bio || undefined).toBeUndefined();
expect(dto.avatar || undefined).toBeUndefined();
});
});
describe('toEntity', () => {
it('should map DTO to entity correctly', () => {
const dto = new UserDto();
dto.id = 1;
dto.name = 'John Doe';
dto.email = 'john@example.com';
dto.bio = 'Software Developer';
dto.avatar = 'avatar.jpg';
const entity = mapper.toEntity(dto);
expect(entity.id).toBe(1);
expect(entity.fullName).toBe('John Doe');
expect(entity.email).toBe('john@example.com');
expect(entity.profile.bio).toBe('Software Developer');
expect(entity.profile.avatar).toBe('avatar.jpg');
});
});
describe('automatic field mapping', () => {
it('should automatically map fields with same names', () => {
const entity = new UserEntity({
id: 42,
email: 'test@example.com',
});
const dto = mapper.toDto(entity);
expect(dto.id).toBe(42);
expect(dto.email).toBe('test@example.com');
});
});
});
//# sourceMappingURL=mapper.spec.js.map