From bf83709d6c33339d63aa7beb01bdd94f5c1da75d Mon Sep 17 00:00:00 2001 From: JamesNg Date: Tue, 22 Apr 2025 15:28:34 -0400 Subject: [PATCH 1/4] created unit tests for checkIns router --- backend/globalConfig.json | 2 +- backend/routers/checkIns.router.test.js | 116 ++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 backend/routers/checkIns.router.test.js diff --git a/backend/globalConfig.json b/backend/globalConfig.json index ae7585862..a46096f29 100644 --- a/backend/globalConfig.json +++ b/backend/globalConfig.json @@ -1 +1 @@ -{"mongoUri":"mongodb://127.0.0.1:56074/jest?","mongoDBName":"jest"} \ No newline at end of file +{"mongoUri":"mongodb://127.0.0.1:41727/jest?","mongoDBName":"jest"} \ No newline at end of file diff --git a/backend/routers/checkIns.router.test.js b/backend/routers/checkIns.router.test.js new file mode 100644 index 000000000..fa9933d62 --- /dev/null +++ b/backend/routers/checkIns.router.test.js @@ -0,0 +1,116 @@ +// Mock and import CheckIn model +jest.mock('../models/checkIn.model'); +jest.mock('../models/user.model'); +const { CheckIn, User } = require('../models'); + +// Import the check-ins router +const express = require('express'); +const supertest = require('supertest'); +const checkInsRouter = require('./checkIns.router'); + +// Create a new Express application for testing +const testapp = express(); +testapp.use('/api/checkins', checkInsRouter); +const request = supertest(testapp); + +describe('Unit tests for checkIns router', () => { + // Mock data for check-ins + const mockCheckIns = [ + { id: 1, eventId: 'event1', userId: 'user1', checkedIn: true, createdDate: String(new Date()) }, + { id: 2, eventId: 'event2', userId: 'user2', checkedIn: true, createdDate: String(new Date()) }, + ]; + + // Clear mocks after each test + afterEach(() => { + jest.clearAllMocks(); + }); + + describe('READ', () => { + it('should return a list of check-ins with GET /api/checkins', async (done) => { + // Mock Mongoose method + CheckIn.find.mockResolvedValue(mockCheckIns); + + const response = await request.get('/api/checkins'); + + // Tests + expect(CheckIn.find).toHaveBeenCalled(); + expect(response.status).toBe(200); + expect(response.body).toEqual(mockCheckIns); + + // Marks completion of test + done(); + }); + + it('should return a single check-in by id with GET /api/checkins/:id', async (done) => { + // Mock Mongoose method + CheckIn.findById.mockResolvedValue(mockCheckIns[0]); + + const response = await request.get('/api/checkins/1'); + + // Tests + expect(CheckIn.findById).toHaveBeenCalledWith('1'); + expect(response.status).toBe(200); + expect(response.body).toEqual(mockCheckIns[0]); + + // Marks completion of test + done(); + }); + + it('should return a single check-in by eventId with GET /api/checkins/findEvent/:id', async (done) => { + // Mock user for populating documents from User model + const mockUser = { + id: 'user2', + name: { + firstName: 'test', + lastName: 'user', + }, + }; + + // Mock Mongoose methods + User.findById.mockResolvedValue(mockUser); + CheckIn.find.mockReturnValue({ + populate: jest.fn().mockResolvedValue({ ...mockCheckIns[1], userId: 'user2' }), + }); + + const response = await request.get('/api/checkins/findEvent/event2'); + + // Tests + + expect(CheckIn.find).toHaveBeenCalledWith({ + eventId: 'event2', + userId: { $ne: 'undefined' }, + }); + expect(CheckIn.find().populate).toHaveBeenCalledWith({ path: 'userId', model: 'User' }); + expect(response.status).toBe(200); + expect(response.body).toEqual(mockCheckIns[1]); + + // Marks completion of test + done(); + }); + }); + + describe('CREATE', () => { + it('should create a new check-in with POST /api/checkins', async (done) => { + // Mock new check-in data + const newCheckIn = { + id: 3, + eventId: 'event3', + userId: 'user3', + checkedIn: true, + createdDate: String(new Date()), + }; + + // Mock create method + CheckIn.create.mockResolvedValue(newCheckIn); + + const response = await request.post('/api/checkins').send(newCheckIn); + + // Tests + expect(CheckIn.create).toHaveBeenCalled(); + expect(response.status).toBe(201); + + // Marks completion of test + done(); + }); + }); +}); From 4718f16d4856607b3d061f66cc856823584f79e5 Mon Sep 17 00:00:00 2001 From: JamesNg Date: Tue, 27 May 2025 11:15:20 -0400 Subject: [PATCH 2/4] updated changes to unit tests for checkIns router --- backend/globalConfig.json | 2 +- backend/package.json | 1 + backend/routers/checkIns.router.test.js | 10 +++++----- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/backend/globalConfig.json b/backend/globalConfig.json index a46096f29..5580db2f4 100644 --- a/backend/globalConfig.json +++ b/backend/globalConfig.json @@ -1 +1 @@ -{"mongoUri":"mongodb://127.0.0.1:41727/jest?","mongoDBName":"jest"} \ No newline at end of file +{"mongoUri":"mongodb://127.0.0.1:36717/jest?","mongoDBName":"jest"} \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index 64d56b684..b3038427a 100644 --- a/backend/package.json +++ b/backend/package.json @@ -8,6 +8,7 @@ "format": "prettier --check .", "test": "jest", "test:watch": "jest --watch", + "test:checkins": "jest --watch checkIns.router.test.js", "start": "node server.js", "dev": "nodemon server.js", "client": "npm run start --prefix client", diff --git a/backend/routers/checkIns.router.test.js b/backend/routers/checkIns.router.test.js index fa9933d62..efbe72a8f 100644 --- a/backend/routers/checkIns.router.test.js +++ b/backend/routers/checkIns.router.test.js @@ -1,7 +1,6 @@ // Mock and import CheckIn model jest.mock('../models/checkIn.model'); -jest.mock('../models/user.model'); -const { CheckIn, User } = require('../models'); +const { CheckIn } = require('../models'); // Import the check-ins router const express = require('express'); @@ -10,6 +9,8 @@ const checkInsRouter = require('./checkIns.router'); // Create a new Express application for testing const testapp = express(); +// Allows for body parsing +testapp.use(express.json()); testapp.use('/api/checkins', checkInsRouter); const request = supertest(testapp); @@ -67,9 +68,8 @@ describe('Unit tests for checkIns router', () => { }; // Mock Mongoose methods - User.findById.mockResolvedValue(mockUser); CheckIn.find.mockReturnValue({ - populate: jest.fn().mockResolvedValue({ ...mockCheckIns[1], userId: 'user2' }), + populate: jest.fn().mockResolvedValue(mockCheckIns[1]), }); const response = await request.get('/api/checkins/findEvent/event2'); @@ -106,7 +106,7 @@ describe('Unit tests for checkIns router', () => { const response = await request.post('/api/checkins').send(newCheckIn); // Tests - expect(CheckIn.create).toHaveBeenCalled(); + expect(CheckIn.create).toHaveBeenCalledWith(newCheckIn); expect(response.status).toBe(201); // Marks completion of test From ab820500fa536dc3833bbfb7a1a21af95b311826 Mon Sep 17 00:00:00 2001 From: JamesNg Date: Tue, 27 May 2025 11:27:24 -0400 Subject: [PATCH 3/4] updated test to GET /api/checkins/findEvent/:id --- backend/globalConfig.json | 2 +- backend/routers/checkIns.router.test.js | 22 ++++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/backend/globalConfig.json b/backend/globalConfig.json index 5580db2f4..ababd7aef 100644 --- a/backend/globalConfig.json +++ b/backend/globalConfig.json @@ -1 +1 @@ -{"mongoUri":"mongodb://127.0.0.1:36717/jest?","mongoDBName":"jest"} \ No newline at end of file +{"mongoUri":"mongodb://127.0.0.1:33211/jest?","mongoDBName":"jest"} \ No newline at end of file diff --git a/backend/routers/checkIns.router.test.js b/backend/routers/checkIns.router.test.js index efbe72a8f..c08a03b57 100644 --- a/backend/routers/checkIns.router.test.js +++ b/backend/routers/checkIns.router.test.js @@ -57,32 +57,26 @@ describe('Unit tests for checkIns router', () => { done(); }); - it('should return a single check-in by eventId with GET /api/checkins/findEvent/:id', async (done) => { - // Mock user for populating documents from User model - const mockUser = { - id: 'user2', - name: { - firstName: 'test', - lastName: 'user', - }, - }; + it('should return a list of users who have checked into a specific event with GET /api/checkins/findEvent/:id', async (done) => { + // Mock specific checkIn + const mockCheckIn = mockCheckIns[1]; + const { eventId } = mockCheckIn; // Mock Mongoose methods CheckIn.find.mockReturnValue({ - populate: jest.fn().mockResolvedValue(mockCheckIns[1]), + populate: jest.fn().mockResolvedValue(mockCheckIn), }); - const response = await request.get('/api/checkins/findEvent/event2'); + const response = await request.get(`/api/checkins/findEvent/${eventId}`); // Tests - expect(CheckIn.find).toHaveBeenCalledWith({ - eventId: 'event2', + eventId: eventId, userId: { $ne: 'undefined' }, }); expect(CheckIn.find().populate).toHaveBeenCalledWith({ path: 'userId', model: 'User' }); expect(response.status).toBe(200); - expect(response.body).toEqual(mockCheckIns[1]); + expect(response.body).toEqual(mockCheckIn); // Marks completion of test done(); From a8fc96315f3249b8a39894d2a79907c0d5e967aa Mon Sep 17 00:00:00 2001 From: JamesNg Date: Tue, 27 May 2025 11:56:35 -0400 Subject: [PATCH 4/4] removed temporarily added script for testing checkIns router --- backend/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/package.json b/backend/package.json index b3038427a..64d56b684 100644 --- a/backend/package.json +++ b/backend/package.json @@ -8,7 +8,6 @@ "format": "prettier --check .", "test": "jest", "test:watch": "jest --watch", - "test:checkins": "jest --watch checkIns.router.test.js", "start": "node server.js", "dev": "nodemon server.js", "client": "npm run start --prefix client",