UNPKG

@ilhamtahir/nestjs-mapper

Version:

NestJS integration for @ilhamtahir/ts-mapper - A MapStruct-like object mapping library for TypeScript

95 lines 3.67 kB
"use strict"; 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 testing_1 = require("@nestjs/testing"); const common_1 = require("@nestjs/common"); const mapper_module_1 = require("../mapper.module"); const mapper_decorator_1 = require("../decorators/mapper.decorator"); const ts_mapper_1 = require("@ilhamtahir/ts-mapper"); // Test entities and DTOs class TestEntity { constructor(data = {}) { this.id = data.id || 0; this.name = data.name || ''; } } class TestDto { constructor() { this.id = 0; this.name = ''; } } let TestMapper = class TestMapper { toDto(entity) { return (0, ts_mapper_1.transform)(this, 'toDto', entity, TestDto); } }; TestMapper = __decorate([ (0, mapper_decorator_1.Mapper)() ], TestMapper); let TestService = class TestService { constructor(testMapper) { this.testMapper = testMapper; } processEntity(entity) { return this.testMapper.toDto(entity); } }; TestService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [TestMapper]) ], TestService); describe('MapperModule', () => { let module; let testService; let testMapper; beforeEach(async () => { module = await testing_1.Test.createTestingModule({ imports: [mapper_module_1.MapperModule.forRoot()], providers: [TestService], }).compile(); testService = module.get(TestService); testMapper = module.get(TestMapper); }); afterEach(async () => { await module.close(); }); it('should be defined', () => { expect(module).toBeDefined(); }); it('should provide mapper instances', () => { expect(testMapper).toBeDefined(); expect(testMapper).toBeInstanceOf(TestMapper); }); it('should inject mappers into services', () => { expect(testService).toBeDefined(); const entity = new TestEntity({ id: 1, name: 'Test' }); const dto = testService.processEntity(entity); expect(dto.id).toBe(1); expect(dto.name).toBe('Test'); }); it('should work with forFeature', async () => { const featureModule = await testing_1.Test.createTestingModule({ imports: [mapper_module_1.MapperModule.forFeature([TestMapper])], providers: [TestService], }).compile(); const featureTestService = featureModule.get(TestService); const featureTestMapper = featureModule.get(TestMapper); expect(featureTestService).toBeDefined(); expect(featureTestMapper).toBeDefined(); const entity = new TestEntity({ id: 2, name: 'Feature Test' }); const dto = featureTestService.processEntity(entity); expect(dto.id).toBe(2); expect(dto.name).toBe('Feature Test'); await featureModule.close(); }); }); //# sourceMappingURL=mapper.module.spec.js.map