Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4aa6f6e
Subir pruebas
mariana-1004 May 21, 2025
d69a2a1
Agregar la funcionalidad de eliminar hidratación
Sofiosu24 May 23, 2025
14bf74a
Eliminar hidratación
Sofiosu24 May 23, 2025
f7abcd9
Merge branch 'develop' of https://github.com/CodeAnd-Co/TECH-NEBRIOS-…
Sofiosu24 May 27, 2025
e80682a
Agrega logica para agregar un nuevo tipo de hidratacion
Emiidk01 May 27, 2025
e0e0ebd
Agrega pruebas unitarias con jest
Emiidk01 May 27, 2025
cae2ef9
Correccion de code smells con lint
Emiidk01 May 27, 2025
351ed16
Trae cambios de develop
Emiidk01 May 28, 2025
7ead0ba
peuqeños cambios de estilo
Emiidk01 May 29, 2025
4d5193b
Traer cambios de develop
Emiidk01 May 29, 2025
0080215
Agregar correciones
Emiidk01 May 29, 2025
f0944b8
Pruebas jest
mariana-1004 Jun 3, 2025
8820dad
Agregar error de no se puede borrar hidrato charola si se usa
Sofiosu24 Jun 5, 2025
0d76473
Actualizar rama
Sofiosu24 Jun 5, 2025
8e715e9
Agregar al modelo una acción asincrona
Sofiosu24 Jun 5, 2025
497a2b0
Agregar cambios de develop
Emiidk01 Jun 6, 2025
68c1ae3
Git pull origin develop
Sofiosu24 Jun 6, 2025
233412b
Merge entre eliminar y editar
Sofiosu24 Jun 6, 2025
9f0b2e0
Merge branch 'develop' of https://github.com/CodeAnd-Co/TECH-NEBRIOS-…
Sofiosu24 Jun 6, 2025
9d10d17
Merge de agregar,editar y eliminar hidratación
Sofiosu24 Jun 6, 2025
0fb37cc
Git pull origin develop 2
Sofiosu24 Jun 7, 2025
bfb4148
Git pull origin develop 2
Sofiosu24 Jun 7, 2025
77262e6
Git pull
Sofiosu24 Jun 7, 2025
e5ae6f2
Quitar utils database
Sofiosu24 Jun 7, 2025
6c9e382
Quitar comillas dobles
Sofiosu24 Jun 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
55 changes: 55 additions & 0 deletions __tests__/editarHidratacion.controller.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { editarHidratacion } = require('../controllers/hidratacion.controller');
const { Hidratacion } = require('../models/hidratacion.model');

jest.mock('../models/hidratacion.model');

describe('editarHidratacion', () => {
let req, res;

beforeEach(() => {
req = {
params: {},
body: {}
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
send: jest.fn()
};
});

it('debe responder con 400 si el id es inválido', async () => {
req.params.idHidratacion = 'abc';

await editarHidratacion(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: 'ID de hidratante no válido' });
});

it('debe editar correctamente y responder con 200', async () => {
req.params.idHidratacion = '1';
req.body = { nombreHidratacion: 'Te', descripcionHidratacion: 'Verde' };

Hidratacion.mockImplementation(() => ({ actualizar: jest.fn().mockResolvedValue() }));

await editarHidratacion(req, res);

expect(res.json).toHaveBeenCalledWith({
success: true,
message: 'Hidratacion actualizado'
});
});

it('debe manejar error y responder con 500', async () => {
req.params.idHidratacion = '1';
req.body = { nombreHidratacion: 'Te', descripcionHidratacion: 'Verde' };

Hidratacion.mockImplementation(() => ({ actualizar: jest.fn().mockRejectedValue(new Error()) }));

await editarHidratacion(req, res);

expect(res.status).toHaveBeenCalledWith(500);
expect(res.send).toHaveBeenCalledWith('Error al editar hidratacion');
});
});
51 changes: 51 additions & 0 deletions __tests__/editarHidratacion.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { Hidratacion } = require('../models/hidratacion.model');

jest.mock('../generated/prisma', () => {
const updateMock = jest.fn();
return {
PrismaClient: jest.fn().mockImplementation(() => ({
HIDRATACION: {
update: updateMock
}
})),
__mocks__: { updateMock }
};
});

const { __mocks__ } = require('../generated/prisma');

describe('Hidratacion.actualizar', () => {
beforeEach(() => {
__mocks__.updateMock.mockClear();
});

it('debe actualizar la hidratación correctamente', async () => {
__mocks__.updateMock.mockResolvedValue({
hidratacionId: 1,
nombre: 'Te',
descripcion: 'Verde'
});

const hidratacion = new Hidratacion(1, 'Te', 'Verde');
const resultado = await hidratacion.actualizar();

expect(__mocks__.updateMock).toHaveBeenCalledWith({
where: { hidratacionId: 1 },
data: { nombre: 'Te', descripcion: 'Verde' }
});

expect(resultado).toEqual({
hidratacionId: 1,
nombre: 'Te',
descripcion: 'Verde'
});
});

it('debe lanzar error si la base de datos falla', async () => {
__mocks__.updateMock.mockRejectedValue(new Error('DB Error'));

const hidratacion = new Hidratacion(1, 'Te', 'Verde');

await expect(hidratacion.actualizar()).rejects.toThrow('DB Error');
});
});
70 changes: 70 additions & 0 deletions __tests__/eliminarHidratacion.controller.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { eliminarHidratacion } = require('../controllers/hidratacion.controller');
const { Hidratacion } = require('../models/hidratacion.model');

jest.mock('../models/hidratacion.model');

describe('eliminarHidratacion', () => {
let req, res;

beforeEach(() => {
req = {
params: {}
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
send: jest.fn()
};
});

it('debe responder con 400 si el ID no es válido', async () => {
req.params.idHidratacion = '-1';

await eliminarHidratacion(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: 'ID de hidrato no válido' });
});

it('debe rechazar si la hidratación está asignada', async () => {
req.params.idHidratacion = '2';
Hidratacion.mockImplementation(() => ({
isAgregada: jest.fn().mockResolvedValue(1)
}));

await eliminarHidratacion(req, res);

expect(res.status).toHaveBeenCalledWith(409);
expect(res.json).toHaveBeenCalledWith({
success: false,
message: 'La hidratación no se puede eliminar porque está asignado a una charola'
});
});

it('debe eliminar correctamente si no está asignada', async () => {
req.params.idHidratacion = '3';
Hidratacion.mockImplementation(() => ({
isAgregada: jest.fn().mockResolvedValue(0),
eliminar: jest.fn().mockResolvedValue()
}));

await eliminarHidratacion(req, res);

expect(res.json).toHaveBeenCalledWith({
success: true,
message: 'Hidrato eliminado'
});
});

it('debe responder con 500 si ocurre un error inesperado', async () => {
req.params.idHidratacion = '3';
Hidratacion.mockImplementation(() => ({
isAgregada: jest.fn().mockRejectedValue(new Error())
}));

await eliminarHidratacion(req, res);

expect(res.status).toHaveBeenCalledWith(500);
expect(res.send).toHaveBeenCalledWith('Error al eliminar hidrato');
});
});
45 changes: 45 additions & 0 deletions __tests__/eliminarHidratacion.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { Hidratacion } = require('../models/hidratacion.model');
const { PrismaClient } = require('../generated/prisma');

jest.mock('../generated/prisma', () => {
const deleteMock = jest.fn();
return {
PrismaClient: jest.fn(() => ({
HIDRATACION: {
delete: deleteMock,
},
})),
};
});

describe('Modelo Hidratacion - eliminar()', () => {
let prismaInstance;

beforeEach(() => {
jest.clearAllMocks();
// Crea una nueva instancia simulada
prismaInstance = new PrismaClient();
});

it('debe eliminar correctamente el registro', async () => {
const mockResultado = { hidratacionId: 1, nombre: 'Agua' };

prismaInstance.HIDRATACION.delete.mockResolvedValue(mockResultado);

const hidratacion = new Hidratacion(1);
const resultado = await hidratacion.eliminar();

expect(prismaInstance.HIDRATACION.delete).toHaveBeenCalledWith({
where: { hidratacionId: 1 },
});
expect(resultado).toEqual(mockResultado);
});

it('debe lanzar un error si la eliminación falla', async () => {
prismaInstance.HIDRATACION.delete.mockRejectedValue(new Error('Error de BD'));

const hidratacion = new Hidratacion(1);

await expect(hidratacion.eliminar()).rejects.toThrow('Error de BD');
});
});
48 changes: 0 additions & 48 deletions __tests__/hidratacion.controller.test.js

This file was deleted.

49 changes: 0 additions & 49 deletions __tests__/hidratacion.model.test.js

This file was deleted.

69 changes: 69 additions & 0 deletions __tests__/registrarHidratacion.controller.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { registrarHidratacion } = require('../controllers/hidratacion.controller');
const { Hidratacion } = require('../models/hidratacion.model');

jest.mock('../models/hidratacion.model');

describe('registrarHidratacion', () => {
let req, res;

beforeEach(() => {
req = {
body: {}
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
});

it('debe regresar 400 si faltan campos', async () => {
req.body = { nombre: '', descripcion: '' };

await registrarHidratacion(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({
success: false,
message: 'Datos no válidos',
});
});

it('debe registrar correctamente y regresar 200', async () => {
req.body = { nombre: 'Agua', descripcion: 'Natural' };
Hidratacion.mockImplementation(() => ({ agregar: jest.fn().mockResolvedValue() }));

await registrarHidratacion(req, res);

expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
success: true,
message: 'Hidratación registrada exitosamente',
});
});

it('debe manejar error de conexión y regresar 101', async () => {
req.body = { nombre: 'Agua', descripcion: 'Natural' };
Hidratacion.mockImplementation(() => ({ agregar: jest.fn().mockRejectedValue({ code: 'ETIMEDOUT' }) }));

await registrarHidratacion(req, res);

expect(res.status).toHaveBeenCalledWith(101);
expect(res.json).toHaveBeenCalledWith({
success: false,
message: 'Sin conexión a internet',
});
});

it('debe manejar error genérico y regresar 500', async () => {
req.body = { nombre: 'Agua', descripcion: 'Natural' };
Hidratacion.mockImplementation(() => ({ agregar: jest.fn().mockRejectedValue({}) }));

await registrarHidratacion(req, res);

expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({
success: false,
message: 'Error del servidor al registrar hidratación',
});
});
});
Loading