Skip to content
Snippets Groups Projects
Commit 3d56d2b7 authored by Federico Hurtado's avatar Federico Hurtado
Browse files

Unit tests for peopleRepository.

parent e4e55ca9
No related branches found
No related tags found
1 merge request!2able to bypass cas in localhost
......@@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"author": "",
"license": "ISC",
......@@ -21,5 +21,9 @@
"passport": "^0.7.0",
"passport-cas": "^0.1.1",
"sequelize": "^6.37.4"
},
"devDependencies": {
"jest": "^29.7.0",
"jest-mock": "^29.7.0"
}
}
......@@ -47,7 +47,12 @@ async function addPerson(person) {
return results.insertId;
} catch (error) {
console.error(error);
return null;
}
}
async function getPersonById() {}
async function deletePersonById() {}
module.exports = { addPerson };
// Mock the database's pool and query function
jest.mock("../../config/database/database.config", () => {
const queryMock = jest.fn(); // Mock query function
return { pool: { query: queryMock } }; // Return the mocked pool directly
});
const { pool } = require("../../config/database/database.config");
const { addPerson } = require("../../repository/peopleRepository");
/*
Test methods for peopleRepository
*/
describe("Testing peopleRepository.js", () => {
// mock person for testing
const mockPerson = {
firstName: "John",
lastName: "Doe",
middleName: "A",
maidenName: null,
suffix: null,
nickName: "Johnny",
techAlumniChapter: "Tidewater AC",
classYear: "2024",
gradYear: "2024",
gradSemester: "spring",
gender: "male",
};
/*
Tests for the addPerson method
*/
describe("addPerson Tests", () => {
it("Insert works with no errors", async () => {
// Mock the database to return the new insert ID as 1
const mockInsertId = 1;
pool.query.mockResolvedValueOnce([{ insertId: mockInsertId }]);
// call add person
const result = await addPerson(mockPerson);
// Verify the SQL query and parameters
expect(pool.query).toHaveBeenCalledWith(
expect.any(String), // SQL query string, can be further detailed if needed
[
"John",
"Doe",
"A",
null,
null,
"Johnny",
2, // hardcoded for now
"2024",
"2024",
"spring",
"male",
]
);
// expect function to return the new insert id
expect(result).toBe(mockInsertId);
});
it("insert throws an error if query fails", async () => {
// Mock the database to throw an error
const mockError = new Error("Database query failed");
pool.query.mockRejectedValueOnce(mockError);
// call addPerson and expect it to handle the error
const result = await addPerson(mockPerson);
expect(result).toBe(null);
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment