generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 97
Unit Testing for CheckIns Router #1906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dannyprikaz
merged 8 commits into
hackforla:development
from
jng34:unitTestingForCheckIns
Jun 17, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bf83709
created unit tests for checkIns router
jng34 5e619a8
Merge branch 'development' into unitTestingForCheckIns
dannyprikaz 4718f16
updated changes to unit tests for checkIns router
jng34 ab82050
updated test to GET /api/checkins/findEvent/:id
jng34 a8fc963
removed temporarily added script for testing checkIns router
jng34 b049361
Merge branch 'development' into unitTestingForCheckIns
dannyprikaz ebedad0
Merge branch 'development' into unitTestingForCheckIns
jng34 e9ef784
Merge branch 'development' into unitTestingForCheckIns
dannyprikaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| {"mongoUri":"mongodb://127.0.0.1:43943/jest?","mongoDBName":"jest"} | ||
| {"mongoUri":"mongodb://127.0.0.1:43943/jest?","mongoDBName":"jest"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Mock and import CheckIn model | ||
| jest.mock('../models/checkIn.model'); | ||
| const { CheckIn } = 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(); | ||
| // Allows for body parsing | ||
| testapp.use(express.json()); | ||
| 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 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({ | ||
jng34 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| populate: jest.fn().mockResolvedValue(mockCheckIn), | ||
| }); | ||
|
|
||
| const response = await request.get(`/api/checkins/findEvent/${eventId}`); | ||
|
|
||
| // Tests | ||
| expect(CheckIn.find).toHaveBeenCalledWith({ | ||
| eventId: eventId, | ||
| userId: { $ne: 'undefined' }, | ||
| }); | ||
| expect(CheckIn.find().populate).toHaveBeenCalledWith({ path: 'userId', model: 'User' }); | ||
| expect(response.status).toBe(200); | ||
| expect(response.body).toEqual(mockCheckIn); | ||
|
|
||
| // 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).toHaveBeenCalledWith(newCheckIn); | ||
| expect(response.status).toBe(201); | ||
|
|
||
| // Marks completion of test | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.