Skip to content
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Prueba de Backend NodeJS
Crear un CRUD para crear productos conectado a MongoDB.

# URL PRODUCCION
https://backend-node-evaluation-2021.herokuapp.com/
### Instalación
```
npm install
Expand Down
25 changes: 13 additions & 12 deletions e2e/categories.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const USER = encodeURIComponent(config.dbUser);
const PASSWORD = encodeURIComponent(config.dbPassword);
const DB_NAME = config.dbName;

const MONGO_URI = `${config.dbConnection}://${USER}:${PASSWORD}@${config.dbHost}:${config.dbPort}?retryWrites=true&w=majority`;
const collection = 'categories';
const MONGO_URI = `${config.dbConnection}://${USER}:${PASSWORD}@${config.dbHost}?retryWrites=true&w=majority`;
const collection = "categories";

describe("Tests to categories", () => {
let app;
Expand Down Expand Up @@ -37,14 +37,16 @@ describe("Tests to categories", () => {
it("should create a new category", async (done) => {
const newCategory = {
name: "Category 1",
image: 'https://via.placeholder.com/150',
image: "https://via.placeholder.com/150",
};
return request(app)
.post("/api/categories")
.send(newCategory)
.expect(201)
.then(async ({ body }) => {
const rta = await database.collection(collection).findOne({ _id: ObjectId(body._id) });
const rta = await database
.collection(collection)
.findOne({ _id: ObjectId(body._id) });
expect(body.name).toBe(rta.name);
expect(body.image).toBe(rta.image);
done();
Expand All @@ -61,7 +63,9 @@ describe("Tests to categories", () => {
.then(async ({ body }) => {
expect(body.length).toBe(1);
const model = body[0];
const rta = await database.collection(collection).findOne({ _id: ObjectId(model._id) });
const rta = await database
.collection(collection)
.findOne({ _id: ObjectId(model._id) });
expect(model.name).toBe(rta.name);
expect(model.image).toBe(rta.image);
done();
Expand All @@ -76,7 +80,7 @@ describe("Tests to categories", () => {
expect(categories.length > 0).toBe(true);
const category = categories[0];
const changes = {
name: 'change',
name: "change",
};
return request(app)
.put(`/api/categories/${category._id}`)
Expand Down Expand Up @@ -109,17 +113,16 @@ describe("Tests to categories", () => {
});

describe("GET /api/categories/{id}/products", () => {

it("should return a list products by category", async (done) => {
const categories = await database.collection(collection).find().toArray();
expect(categories.length > 0).toBe(true);
const category = categories[0];
const products = [
{ name: "Red", price: 200, categoryId: `${category._id}` },
{ name: "Red", price: 200, categoryId: `${category._id}` },
{ name: "Blue", price: 300, categoryId: `${category._id}` },
{ name: "Leon", price: 400 }
{ name: "Leon", price: 400 },
];
await database.collection('products').insertMany(products);
await database.collection("products").insertMany(products);
return request(app)
.get(`/api/categories/${category._id}/products`)
.expect(200)
Expand Down Expand Up @@ -148,6 +151,4 @@ describe("Tests to categories", () => {
.catch((err) => done(err));
});
});


});
14 changes: 8 additions & 6 deletions e2e/product.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const USER = encodeURIComponent(config.dbUser);
const PASSWORD = encodeURIComponent(config.dbPassword);
const DB_NAME = config.dbName;

const MONGO_URI = `${config.dbConnection}://${USER}:${PASSWORD}@${config.dbHost}:${config.dbPort}?retryWrites=true&w=majority`;
const collection = 'products';
const MONGO_URI = `${config.dbConnection}://${USER}:${PASSWORD}@${config.dbHost}?retryWrites=true&w=majority`;
const collection = "products";

describe("Tests to products", () => {
let app;
Expand Down Expand Up @@ -44,7 +44,9 @@ describe("Tests to products", () => {
.send(newProduct)
.expect(201)
.then(async ({ body }) => {
const rta = await database.collection(collection).findOne({ _id: ObjectId(body._id) });
const rta = await database
.collection(collection)
.findOne({ _id: ObjectId(body._id) });
expect(body.name).toBe(rta.name);
expect(body.price).toBe(rta.price);
done();
Expand All @@ -61,7 +63,9 @@ describe("Tests to products", () => {
.then(async ({ body }) => {
expect(body.length).toBe(1);
const product = body[0];
const rta = await database.collection(collection).findOne({ _id: ObjectId(product._id) });
const rta = await database
.collection(collection)
.findOne({ _id: ObjectId(product._id) });
expect(product.name).toBe(rta.name);
expect(product.price).toBe(rta.price);
done();
Expand Down Expand Up @@ -123,6 +127,4 @@ describe("Tests to products", () => {
.catch((err) => done(err));
});
});


});
143 changes: 143 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
"description": "Reto 9 Octubre 26: Curso de Backend con Node",
"main": "index.js",
"dependencies": {
"@hapi/boom": "^9.1.2",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"joi": "^17.4.0",
"mongodb": "^3.6.6"
},
"devDependencies": {
"@types/mongodb": "^3.6.12",
"jest": "^26.6.3",
"nodemon": "^1.19.4",
"supertest": "^6.1.3"
},
"scripts": {
"dev": "DEBUG=app:* nodemon src/index.js",
"start": "NODE_ENV=production node src/index.js",
"test:e2e": "jest --forceExit --config ./e2e/jest-e2e.json"
"test:e2e": "jest --forceExit --config ./e2e/jest-e2e.json",
"test": "jest"
},
"repository": {
"type": "git",
Expand Down
Loading