65 lines
3.5 KiB
TypeScript
65 lines
3.5 KiB
TypeScript
import { model, Types } from 'mongoose';
|
|
import { describe, expect, it, jest } from '@jest/globals';
|
|
import { UnitsService } from './units.service';
|
|
import { UnitSchema } from '../../_schemas/anlagen/units';
|
|
import { UnitTypeDto } from '../../_dto/anlagen/units';
|
|
import { defaultMaterialSettings } from '../../_dto/anlagen/materialfluss';
|
|
|
|
describe('UnitsService.findAll', () => {
|
|
it('zeigt ausschließlich Units der aktiven Anlage und liest die Auswahl nach jedem Wechsel neu', async () => {
|
|
const oldId = new Types.ObjectId(), newId = new Types.ObjectId(), emptyId = new Types.ObjectId();
|
|
let selected = oldId;
|
|
const rows = [
|
|
{ _id: new Types.ObjectId(), anlageId: oldId, name: 'Alte Unit', type: UnitTypeDto.GENERAL, components: [] },
|
|
{ _id: new Types.ObjectId(), anlageId: newId, name: 'Neue Unit', type: UnitTypeDto.GENERAL, components: [] },
|
|
];
|
|
const find = jest.fn((filter?: { anlageId: Types.ObjectId }) => ({ exec: async () => rows.filter(row => !filter || row.anlageId.equals(filter.anlageId)) }));
|
|
const service = new UnitsService({ find } as any, {} as any, {} as any,
|
|
{ getSelected: async () => ({ id: selected.toString() }) } as any);
|
|
expect((await service.findAll()).map(row => row.name)).toEqual(['Alte Unit']);
|
|
selected = newId;
|
|
expect((await service.findAll()).map(row => row.name)).toEqual(['Neue Unit']);
|
|
selected = emptyId;
|
|
expect(await service.findAll()).toEqual([]);
|
|
});
|
|
it('liefert ohne ausgewählte Anlage keine ungefilterten Daten', async () => {
|
|
const find = jest.fn();
|
|
const service = new UnitsService({ find } as any, {} as any, {} as any,
|
|
{ getSelected: async () => { throw new Error('Keine Anlage ausgewählt'); } } as any);
|
|
await expect(service.findAll()).rejects.toThrow('Keine Anlage ausgewählt');
|
|
expect(find).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
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,
|
|
materialFlow: { ...defaultMaterialSettings(), kind: 'storage' as const, fullSignalId: 'digital:original' },
|
|
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.mock.contexts[0] as unknown as { materialFlow: { fullSignalId: string | null } }).materialFlow.fullSignalId).toBeNull();
|
|
expect(source.materialFlow.fullSignalId).toBe('digital:original');
|
|
expect(save).toHaveBeenCalledTimes(1);
|
|
} finally {
|
|
save.mockRestore();
|
|
find.mockRestore();
|
|
}
|
|
});
|
|
});
|