lecoffre-back/src/test/services/super-admin/DeedService.test.ts

180 lines
5.8 KiB
TypeScript

import "module-alias/register";
import "reflect-metadata";
import { Deed } from "le-coffre-resources/dist/SuperAdmin";
import DeedService from "@Services/super-admin/DeedsService/DeedsService";
import { PrismaClient } from "prisma/prisma-client";
import { deed, deedType, documentType, documentType_, office } from "@Test/config/MockedData";
import DeedsRepository from "@Repositories/DeedsRepository";
import Container from "typedi";
import { initDeedType, initDocumentType, initOffice } from "@Test/config/Init";
const prisma = new PrismaClient();
const DeedServiceTest = new DeedService(Container.get(DeedsRepository));
beforeAll(async () => {
office.uid = (await initOffice(office)).uid;
documentType.uid = (await initDocumentType(documentType, office)).uid;
documentType_.uid = (await initDocumentType(documentType_, office)).uid;
deedType.uid = (await initDeedType(deedType, office, [documentType.uid])).uid;
});
afterAll(async () => {
const deleteDeedTypes = prisma.deedTypes.deleteMany();
const deleteOffices = prisma.offices.deleteMany();
await prisma.$transaction([deleteDeedTypes, deleteOffices]);
await prisma.$disconnect();
});
describe("test create function", () => {
it("should not create a new deed if deed type is unknown", async () => {
let deedWithoutDeedTypeUid: Deed = JSON.parse(JSON.stringify(deed));
deedWithoutDeedTypeUid.deed_type!.uid = "random uid";
// try to create a new deed with unknown deed type
async function createDeedWithUnknownDeedType() {
await DeedServiceTest.create(deedWithoutDeedTypeUid);
}
await expect(createDeedWithUnknownDeedType).rejects.toThrow();
});
it("should create a new deed based on existing deed type", async () => {
let deedWithDeedTypeUid: Deed = JSON.parse(JSON.stringify(deed));
deedWithDeedTypeUid.deed_type!.uid = deedType.uid;
const deedCreated = await DeedServiceTest.create(deedWithDeedTypeUid);
expect(deedCreated.deed_type_uid).toEqual(deedType.uid);
});
it("should have by default the same document types as its deed type ", async () => {
const deedWithDocumentTypes = await prisma.deeds.findFirstOrThrow({ include: { deed_has_document_types: true } });
expect(deedWithDocumentTypes.deed_has_document_types.length).toEqual(1);
expect(deedWithDocumentTypes.deed_has_document_types[0]?.document_type_uid).toEqual(documentType.uid);
});
it("should create a the same deed based on existing deed type", async () => {
let deedWithDeedTypeUid: Deed = JSON.parse(JSON.stringify(deed));
deedWithDeedTypeUid.deed_type!.uid = deedType.uid;
const deedCreated = await DeedServiceTest.create(deedWithDeedTypeUid);
expect(deedCreated.deed_type_uid).toEqual(deedType.uid);
});
it("should not create a new deed based on archivated deed type", async () => {
let deedArchivated: Deed = JSON.parse(JSON.stringify(deed));
deedArchivated.deed_type!.uid = deedType.uid;
await prisma.deedTypes.update({
where: { uid: deedType.uid },
data: {
archived_at: new Date(Date.now()),
},
});
// try to create a new deed with archivated deed type
async function createDeedWithArchivatedDeedType() {
await DeedServiceTest.create(deedArchivated);
}
await expect(createDeedWithArchivatedDeedType).rejects.toThrow("deed type is archived");
});
});
describe("test update function", () => {
it("should add document types to a deed", async () => {
const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uid: deedType.uid } })).uid;
let deedToUpdate: Deed = JSON.parse(JSON.stringify(deed));
deedToUpdate.deed_has_document_types = [
{
document_type: documentType,
deed: new Deed(),
created_at: null,
updated_at: null,
},
{
document_type: documentType_,
deed: new Deed(),
created_at: null,
updated_at: null,
},
];
await DeedServiceTest.update(deedUid, deedToUpdate);
const deedUpdated = await prisma.deeds.findFirstOrThrow({
where: {
uid: deedUid,
},
include: {
deed_has_document_types: true,
},
});
expect(deedUpdated.deed_has_document_types.length).toEqual(2);
});
it("should not add document types to a deed type that already has those document types ", async () => {
const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uid: deedType.uid } })).uid;
let deedToUpdate: Deed = JSON.parse(JSON.stringify(deed));
deedToUpdate.deed_has_document_types = [
{
document_type: documentType,
deed: new Deed(),
created_at: null,
updated_at: null,
},
{
document_type: documentType_,
deed: new Deed(),
created_at: null,
updated_at: null,
},
];
await DeedServiceTest.update(deedUid, deedToUpdate);
const deedUpdated = await prisma.deeds.findFirstOrThrow({
where: {
uid: deedUid,
},
include: {
deed_has_document_types: true,
},
});
expect(deedUpdated.deed_has_document_types.length).toEqual(2);
});
it("should delete document types from a deed", async () => {
const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uid: deedType.uid } })).uid;
let deedToUpdate: Deed = JSON.parse(JSON.stringify(deed));
// set relation between deed and document types empty
deedToUpdate.deed_has_document_types = [];
await DeedServiceTest.update(deedUid, deedToUpdate);
const deedUpdated = await prisma.deeds.findFirstOrThrow({
where: {
uid: deedUid,
},
include: {
deed_has_document_types: true,
},
});
expect(deedUpdated.deed_has_document_types.length).toEqual(0);
});
});
describe("test get function", () => {
it("should return an array of Deeds", async () => {
const deeds = await DeedServiceTest.get({});
// verify result typing
expect(deeds).toBeInstanceOf(Array<Deed>);
expect(deeds.length).toEqual(2);
// verify result content
expect(deeds[0]?.deed_type_uid).toEqual(deedType.uid);
expect(deeds[1]?.deed_type_uid).toEqual(deedType.uid);
});
});