40 lines
3.1 KiB
TypeScript
40 lines
3.1 KiB
TypeScript
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
|
|
import { defaultMaterialSettings, validMaterialSettings } from '../../_dto/anlagen/materialfluss';
|
|
import { UnitMaterialService } from './unit-material.service';
|
|
const plant = '507f1f77bcf86cd799439011', id = '507f1f77bcf86cd799439012';
|
|
describe('Unit-Materialfluss-Grunddaten', () => {
|
|
let service: UnitMaterialService, units: any, selected: string, rows: any[];
|
|
beforeEach(() => {
|
|
selected = plant; rows = [{ _id: id, name: 'Sieb', components: [{ _id: 'component' }] }];
|
|
units = { find: jest.fn(() => ({ lean: () => ({ exec: async () => rows }) })), updateOne: jest.fn(async () => ({ matchedCount: 1 })) };
|
|
service = new UnitMaterialService(units, { getSelected: async () => ({ id: selected }) } as any,
|
|
{ list: async () => [{ id: 'digital:full', name: 'Voll', type: 'digital' }, { id: 'analog:level', name: 'Füllstand', type: 'analog' }] } as any);
|
|
});
|
|
it('liefert unbekannte Anzahlen, statt feste Zu-/Abgänge zu erfinden', async () => {
|
|
expect((await service.get(id)).settings).toMatchObject({ inputCount: null, outputCount: null });
|
|
});
|
|
it('speichert nur Materialflussdaten und erhält Komponenten und sonstige Unit-Felder', async () => {
|
|
const settings = { ...defaultMaterialSettings(), kind: 'split' as const, inputCount: 1, outputCount: 2, outlets: [{ id: 'fine', label: 'Fein', direction: 'any' as const }, { id: 'coarse', label: 'Grob', direction: 'any' as const }] };
|
|
await service.save(id, { anlageId: plant, version: 0, settings });
|
|
const update = units.updateOne.mock.calls[0][1]; expect(update.$set).toEqual({ materialFlow: settings }); expect(update.$set).not.toHaveProperty('components');
|
|
expect(units.updateOne.mock.calls[0][0].$or).toBeDefined();
|
|
});
|
|
it('verhindert veraltete Revisionen und fremde Anlagen', async () => {
|
|
units.updateOne.mockResolvedValueOnce({ matchedCount: 0 });
|
|
await expect(service.save(id, { anlageId: plant, version: 1, settings: defaultMaterialSettings() })).rejects.toThrow('inzwischen geändert');
|
|
await expect(service.save(id, { anlageId: 'fremd', version: 0, settings: defaultMaterialSettings() })).rejects.toThrow('Anlage geändert');
|
|
});
|
|
it('erlaubt nur vorhandene digitale Eingänge als Speicher-Voll-/Leermeldung', async () => {
|
|
const settings = { ...defaultMaterialSettings(), kind: 'storage' as const, fullSignalId: 'digital:full' };
|
|
await expect(service.save(id, { anlageId: plant, version: 0, settings })).resolves.toMatchObject({ version: 1 });
|
|
settings.fullSignalId = 'analog:level';
|
|
await expect(service.save(id, { anlageId: plant, version: 0, settings })).rejects.toThrow('digitaler Eingang');
|
|
});
|
|
it('prüft Port-Anzahlen, IDs und Richtungen auch auf Schemaebene', () => {
|
|
expect(validMaterialSettings(defaultMaterialSettings())).toBe(true);
|
|
expect(validMaterialSettings({ ...defaultMaterialSettings(), inputCount: -1 })).toBe(false);
|
|
expect(validMaterialSettings({ ...defaultMaterialSettings(), outputCount: 1 })).toBe(false);
|
|
expect(validMaterialSettings({ ...defaultMaterialSettings(), outputCount: 0 })).toBe(true);
|
|
});
|
|
});
|