import { model, Types } from 'mongoose'; import { UnitsService } from './units.service'; import { UnitSchema } from '../../_schemas/anlagen/units'; import { UnitTypeDto } from '../../_dto/anlagen/units'; describe('UnitsService.copy', () => { it('copies legacy components with empty names using the existing fallback', async () => { const anlageId = new Types.ObjectId(); const componentId = new Types.ObjectId(); const source = { _id: new Types.ObjectId(), anlageId, name: 'SC', type: UnitTypeDto.GENERAL, components: [{ _id: componentId, name: '', type: 'Allgemein' }], }; const UnitModel = model('UnitCopyRegression', UnitSchema); const save = jest.spyOn(UnitModel.prototype, 'save').mockImplementation(async function () { return this; }); const find = jest.spyOn(UnitModel, 'findById').mockReturnValue({ exec: async () => source } as any); const signals = { find: jest.fn(() => ({ exec: async () => [] })) }; const service = new UnitsService(UnitModel as any, signals as any, {} as any, { getSelected: async () => ({ id: anlageId.toString() }) } as any); try { const result = await service.copy(source._id.toString(), { name: 'SC Kopie' }); expect(result.name).toBe('SC Kopie'); expect(result.components[0].name).toBe('unbenannt'); expect(result.components[0].id).not.toBe(componentId.toString()); expect(source.components[0].name).toBe(''); expect(save).toHaveBeenCalledTimes(1); } finally { save.mockRestore(); find.mockRestore(); } }); });