Skip to content

Commit

Permalink
[tests:18] Adicionando testes de supplier-form
Browse files Browse the repository at this point in the history
  • Loading branch information
AlGouvea committed Aug 19, 2024
1 parent 7cc833d commit 901e40c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ yarn-error.log
.idea
.vscode
.env
package-lock.json

/coverage
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
99 changes: 99 additions & 0 deletions src/__tests__/suppllierFormController.test.js
Original file line number Diff line number Diff line change
@@ -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);

Check warning on line 32 in src/__tests__/suppllierFormController.test.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
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: "[email protected]",
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);
});
});

0 comments on commit 901e40c

Please sign in to comment.