diff --git a/backend/package.json b/backend/package.json index fe08ad0c36c758c7ebfb6c7291ff3740e6ed3105..4d6b96b1dd4bb6fb4310cfbbf9e0969c4ba31169 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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" } } diff --git a/backend/repository/peopleRepository.js b/backend/repository/peopleRepository.js index 680b8fb3ae862c46fa5440d81323766a83d43480..5191ddcaf92af031524019f98f246ea9823a9d81 100644 --- a/backend/repository/peopleRepository.js +++ b/backend/repository/peopleRepository.js @@ -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 }; diff --git a/backend/test/repository/peopleRepository.test.js b/backend/test/repository/peopleRepository.test.js new file mode 100644 index 0000000000000000000000000000000000000000..c29c280672528aac3fbc99c970193ab922e6d787 --- /dev/null +++ b/backend/test/repository/peopleRepository.test.js @@ -0,0 +1,73 @@ +// 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); + }); + }); +});