From 901e40cbdc61b5f99b20cbe841f681f62313ea25 Mon Sep 17 00:00:00 2001 From: AlGouvea Date: Mon, 19 Aug 2024 17:00:23 -0300 Subject: [PATCH] [tests:18] Adicionando testes de supplier-form --- .gitignore | 1 + package.json | 10 +- src/__tests__/suppllierFormController.test.js | 99 +++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/suppllierFormController.test.js diff --git a/.gitignore b/.gitignore index 31b1da4..8abdc9c 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +16,6 @@ yarn-error.log .idea .vscode .env +package-lock.json /coverage \ No newline at end of file diff --git a/package.json b/package.json index 9a7dea4..f12adef 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,14 @@ "prettier": "^3.3.3" }, "devDependencies": { - "nodemon": "^3.1.0" + "@eslint/js": "^9.8.0", + "eslint": "^9.8.0", + "husky": "^8.0.0", + "jest": "^29.7.0", + "jest-sonar": "^0.2.16", + "mongodb-memory-server": "^10.0.0", + "nodemon": "^3.1.0", + "prettier": "^3.3.3", + "supertest": "^7.0.0" } } diff --git a/src/__tests__/suppllierFormController.test.js b/src/__tests__/suppllierFormController.test.js new file mode 100644 index 0000000..0be58c5 --- /dev/null +++ b/src/__tests__/suppllierFormController.test.js @@ -0,0 +1,99 @@ +const request = require("supertest"); +const express = require("express"); +const mongoose = require("mongoose"); +const cors = require("cors"); +const routes = require("../routes"); +const { MongoMemoryServer } = require("mongodb-memory-server"); + +const app = express(); +let mongoServer; + +const corsOptions = { + origin: "*", + methods: "GET,HEAD,PUT,PATCH,POST,DELETE", +}; + +app.use(cors(corsOptions)); +app.use(express.json()); +app.use(express.urlencoded({ extended: true })); + +app.use("/", routes); + +beforeAll(async () => { + mongoServer = await MongoMemoryServer.create(); + const uri = mongoServer.getUri(); + + try { + await mongoose.connect(uri, { + useNewUrlParser: true, + useUnifiedTopology: true, + }); + } catch (err) { + console.error("Error connecting to MongoDB:", err); + process.exit(1); + } +}, 30000); + +afterAll(async () => { + await mongoose.connection.close(); + await mongoServer.stop(); +}, 30000); + +describe("Supplier Form Controller Tests", () => { + let supplierId; + + it("should create a new supplier form", async () => { + const res = await request(app) + .post("/SupplierForm/create") + .send({ + supplierData: { + nome: "Test Supplier", + email: "supplier@test.com", + celular: "123456789", + logradouro: "123 Supplier St", + }, + }); + + expect(res.status).toBe(201); + expect(res.body).toHaveProperty("_id"); + expect(res.body.nome).toBe("Test Supplier"); + + supplierId = res.body._id; + }); + + it("should get all supplier forms", async () => { + const res = await request(app).get("/SupplierForm"); + + expect(res.status).toBe(200); + expect(Array.isArray(res.body)).toBe(true); + }); + + it("should get a supplier form by ID", async () => { + const res = await request(app).get(`/SupplierForm/${supplierId}`); + + expect(res.status).toBe(200); + expect(res.body).toHaveProperty("nome", "Test Supplier"); + }); + + it("should update a supplier form by ID", async () => { + const res = await request(app) + .patch(`/SupplierForm/update/${supplierId}`) + .send({ supplierData: { nome: "Updated Supplier" } }); + + expect(res.status).toBe(200); + expect(res.body).toHaveProperty("nome", "Updated Supplier"); + }); + + it("should delete a supplier form by ID", async () => { + const res = await request(app).delete( + `/SupplierForm/delete/${supplierId}` + ); + + expect(res.status).toBe(200); + + const checkSupplier = await request(app).get( + `/SupplierForm/delete/${supplierId}` + ); + expect(checkSupplier.status).toBe(404); + }); +});