Initial commit with Email Import Wizard and Task Processor updates
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||
import { SettingsController } from './settings.controller';
|
||||
import { DocumentType } from '../database/entities/document-type.entity';
|
||||
import { Postprocessing } from '../database/entities/postprocessing.entity';
|
||||
import { PostprocessingAction } from '../database/entities/postprocessing-action.entity';
|
||||
import { UserClient } from '../database/entities/user-client.entity';
|
||||
import { Client } from '../database/entities/client.entity';
|
||||
import { Setting } from '../database/entities/setting.entity';
|
||||
|
||||
const makeRepo = (data: any[] = []) => ({
|
||||
find: jest.fn().mockResolvedValue(data),
|
||||
findOneByOrFail: jest.fn().mockResolvedValue(data[0] ?? {}),
|
||||
create: jest.fn().mockImplementation((d) => d),
|
||||
save: jest.fn().mockImplementation((d) => Promise.resolve({ Id: 99, ...d })),
|
||||
update: jest.fn().mockResolvedValue({ affected: 1 }),
|
||||
delete: jest.fn().mockResolvedValue({ affected: 1 }),
|
||||
});
|
||||
|
||||
describe('SettingsController', () => {
|
||||
let controller: SettingsController;
|
||||
let docTypeRepo: ReturnType<typeof makeRepo>;
|
||||
let ppRepo: ReturnType<typeof makeRepo>;
|
||||
let ppActionRepo: ReturnType<typeof makeRepo>;
|
||||
let userClientRepo: ReturnType<typeof makeRepo>;
|
||||
|
||||
beforeEach(async () => {
|
||||
docTypeRepo = makeRepo([{ Id: 1, DocumentTypeId: 10, TitelTemplate: 'T' }]);
|
||||
ppRepo = makeRepo([{ Id: 1, Name: 'Rule1', Order: 1, IsActive: true }]);
|
||||
ppActionRepo = makeRepo([]);
|
||||
userClientRepo = makeRepo([{ Id: 1, UserId: 'u1', ClientId: 2 }]);
|
||||
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [SettingsController],
|
||||
providers: [
|
||||
{ provide: getRepositoryToken(DocumentType), useValue: docTypeRepo },
|
||||
{ provide: getRepositoryToken(Postprocessing), useValue: ppRepo },
|
||||
{ provide: getRepositoryToken(PostprocessingAction), useValue: ppActionRepo },
|
||||
{ provide: getRepositoryToken(UserClient), useValue: userClientRepo },
|
||||
{ provide: getRepositoryToken(Client), useValue: makeRepo() },
|
||||
{ provide: getRepositoryToken(Setting), useValue: makeRepo([{ ID: 1, Typ: 1, Wert: 'v' }]) },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<SettingsController>(SettingsController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
// Document Types
|
||||
it('getDocumentTypes returns list', async () => {
|
||||
const result = await controller.getDocumentTypes();
|
||||
expect(result).toHaveLength(1);
|
||||
expect(docTypeRepo.find).toHaveBeenCalledWith({ order: { Id: 'ASC' } });
|
||||
});
|
||||
|
||||
it('updateDocumentType calls update + findOneByOrFail', async () => {
|
||||
await controller.updateDocumentType('1', { TitelTemplate: 'New' });
|
||||
expect(docTypeRepo.update).toHaveBeenCalledWith(1, { TitelTemplate: 'New' });
|
||||
expect(docTypeRepo.findOneByOrFail).toHaveBeenCalledWith({ Id: 1 });
|
||||
});
|
||||
|
||||
// Postprocessing
|
||||
it('getPostprocessingRules returns ordered list', async () => {
|
||||
const result = await controller.getPostprocessingRules();
|
||||
expect(result).toHaveLength(1);
|
||||
expect(ppRepo.find).toHaveBeenCalledWith({ order: { Order: 'ASC' } });
|
||||
});
|
||||
|
||||
it('createPostprocessingRule creates and saves', async () => {
|
||||
const result = await controller.createPostprocessingRule({ Name: 'New' } as any);
|
||||
expect(ppRepo.create).toHaveBeenCalledWith({ Name: 'New' });
|
||||
expect(ppRepo.save).toHaveBeenCalled();
|
||||
expect(result).toHaveProperty('Id', 99);
|
||||
});
|
||||
|
||||
it('deletePostprocessingRule deletes', async () => {
|
||||
const result = await controller.deletePostprocessingRule('5');
|
||||
expect(ppRepo.delete).toHaveBeenCalledWith(5);
|
||||
expect(result).toEqual({ deleted: true });
|
||||
});
|
||||
|
||||
// User Clients
|
||||
it('getUserClients returns list', async () => {
|
||||
const result = await controller.getUserClients();
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('createUserClient creates', async () => {
|
||||
const result = await controller.createUserClient({ UserId: 'u2', ClientId: 3 } as any);
|
||||
expect(userClientRepo.create).toHaveBeenCalled();
|
||||
expect(result).toHaveProperty('Id', 99);
|
||||
});
|
||||
|
||||
it('deleteUserClient deletes', async () => {
|
||||
const result = await controller.deleteUserClient('1');
|
||||
expect(userClientRepo.delete).toHaveBeenCalledWith(1);
|
||||
expect(result).toEqual({ deleted: true });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user