lecoffre-back/src/test/services/super-admin/CustomersService.test.ts
2023-05-04 12:17:03 +02:00

167 lines
7.9 KiB
TypeScript

import "module-alias/register";
import "reflect-metadata";
import { Customer } from "le-coffre-resources/dist/SuperAdmin";
import CustomersService from "@Services/super-admin/CustomersService/CustomersService";
import { PrismaClient } from "@prisma/client";
import { customer, customerContact, customerContact_, customer_ } from "@Test/config/MockedData";
import Container from "typedi";
import CustomersRepository from "@Repositories/CustomersRepository";
const prisma = new PrismaClient();
const CustomersServiceTest = new CustomersService(Container.get(CustomersRepository));
afterAll(async () => {
/*
* Clean database after all tests execution.
* Due to cascade deletion, if addresses are deleted, all items following tables are dropped: contacts, customers, offices
*/
const deleteAddresses = prisma.addresses.deleteMany();
await prisma.$transaction([deleteAddresses]);
await prisma.$disconnect();
});
describe("test create function", () => {
it("should create a new customer", async () => {
const customerCreated = await CustomersServiceTest.create(customer);
expect(customerCreated?.status).toEqual("PENDING");
// verify if customer contact is created in db
const contactCreated = await prisma.contacts.findUnique({ where: { uid: customerCreated.contact_uid } });
expect(contactCreated?.first_name).toEqual(customer.contact!.first_name);
expect(contactCreated?.last_name).toEqual(customer.contact!.last_name);
expect(contactCreated?.cell_phone_number).toEqual(customer.contact!.cell_phone_number);
expect(contactCreated?.phone_number).toEqual(customer.contact!.phone_number);
expect(contactCreated?.civility).toEqual(customer.contact!.civility);
expect(contactCreated?.email).toEqual(customer.contact!.email);
// verify if customer address is created in db
const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid! } });
expect(addressForContactCreated?.address).toEqual(customer.contact!.address?.address);
expect(addressForContactCreated?.zip_code).toEqual(customer.contact!.address?.zip_code);
expect(addressForContactCreated?.city).toEqual(customer.contact!.address?.city);
});
it("should not create an customer already created", async () => {
// try to create the same customer
async function duplicateCustomer() {
await CustomersServiceTest.create(customer);
}
await expect(duplicateCustomer).rejects.toThrow();
});
it("should not create an new customer with an email already created", async () => {
let newCustomer: Customer = JSON.parse(JSON.stringify(customer_));
newCustomer.contact!.email = customerContact.email;
// try to create a new customer with already used email
async function createCustomerWithDuplicateEmail() {
await CustomersServiceTest.create(newCustomer);
}
await expect(createCustomerWithDuplicateEmail).rejects.toThrow();
});
it("should not create an customer with an phone number already created", async () => {
let newCustomer: Customer = JSON.parse(JSON.stringify(customer_));
newCustomer.contact!.cell_phone_number = customerContact.cell_phone_number;
// try to create a new customer with already used cellphone number
async function duplicateCustomer() {
await CustomersServiceTest.create(newCustomer);
}
await expect(duplicateCustomer).rejects.toThrow();
});
it("should create an new customer if unique attributes differ from existing customers", async () => {
let newCustomer: Customer = JSON.parse(JSON.stringify(customer));
newCustomer.contact!.email = customerContact_.email;
newCustomer.contact!.cell_phone_number = customerContact_.cell_phone_number;
const customerCreated = await CustomersServiceTest.create(newCustomer);
expect(customerCreated?.status).toEqual("PENDING");
// verify if customer_ contact is created in db
const contactCreated = await prisma.contacts.findUnique({ where: { uid: customerCreated.contact_uid } });
expect(contactCreated?.first_name).toEqual(customer.contact!.first_name);
expect(contactCreated?.last_name).toEqual(customer.contact!.last_name);
expect(contactCreated?.cell_phone_number).toEqual(customer_.contact!.cell_phone_number);
expect(contactCreated?.phone_number).toEqual(customer.contact!.phone_number);
expect(contactCreated?.civility).toEqual(customer.contact!.civility);
expect(contactCreated?.email).toEqual(customer_.contact!.email);
// verify if customer_ address is created in db
const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid! } });
expect(addressForContactCreated?.address).toEqual(customer.contact!.address?.address);
expect(addressForContactCreated?.zip_code).toEqual(customer.contact!.address?.zip_code);
expect(addressForContactCreated?.city).toEqual(customer.contact!.address?.city);
});
});
describe("test update function", () => {
it("should update an customer's data", async () => {
const customerCreated = await prisma.customers.findFirstOrThrow({ where: { contact: { email: customer_.contact!.email } } });
// update the last customer created with his own new office and own contact
const updatedCustomer = await CustomersServiceTest.update(customerCreated.uid, customer_);
expect(updatedCustomer?.status).toEqual("ERRONED");
// verify if customer_ contact is created in db
const existingContact = await prisma.contacts.findUnique({ where: { uid: updatedCustomer.contact_uid } });
expect(existingContact?.first_name).toEqual(customer_.contact!.first_name);
expect(existingContact?.last_name).toEqual(customer_.contact!.last_name);
expect(existingContact?.cell_phone_number).toEqual(customer_.contact!.cell_phone_number);
expect(existingContact?.phone_number).toEqual(customer_.contact!.phone_number);
expect(existingContact?.civility).toEqual(customer_.contact!.civility);
expect(existingContact?.email).toEqual(customer_.contact!.email);
// verify if customer_ address is created in db
const addressForExistingContact = await prisma.addresses.findUnique({ where: { uid: existingContact?.address_uid! } });
expect(addressForExistingContact?.address).toEqual(customer_.contact!.address?.address);
expect(addressForExistingContact?.zip_code).toEqual(customer_.contact!.address?.zip_code);
expect(addressForExistingContact?.city).toEqual(customer_.contact!.address?.city);
});
it("should not update an customer with an email already used", async () => {
const customerUid = (await prisma.customers.findFirstOrThrow({ where: { contact: { email: customer_.contact!.email } } })).uid;
let updatedCustomer: Customer = JSON.parse(JSON.stringify(customer_));
updatedCustomer.contact!.email = customerContact.email;
// try to create a new customer with already used email
async function updateCustomerWithDuplicateEmail() {
await CustomersServiceTest.update(customerUid, updatedCustomer);
}
await expect(updateCustomerWithDuplicateEmail).rejects.toThrow();
});
it("should not update an customer with an phone number already used", async () => {
const customerUid = (await prisma.customers.findFirstOrThrow({ where: { contact: { email: customer_.contact!.email } } })).uid;
let updatedCustomer: Customer = JSON.parse(JSON.stringify(customer_));
updatedCustomer.contact!.cell_phone_number = customerContact.cell_phone_number;
// try to create a new customer with already used email
async function updateCustomerWithDuplicateEmail() {
await CustomersServiceTest.update(customerUid, updatedCustomer);
}
await expect(updateCustomerWithDuplicateEmail).rejects.toThrow();
});
});
describe("test get function", () => {
it("should return an array of Customers", async () => {
const req = {}
const customers = await CustomersServiceTest.get(req);
// verify result typing
expect(customers).toBeInstanceOf(Array<Customer>);
expect(customers.length).toEqual(2);
// verify result content
const customersCreated = await prisma.customers.findMany();
expect(customers).toContainEqual(customersCreated[0]);
expect(customers).toContainEqual(customersCreated[1]);
});
});