generated from hackforla/.github-hackforla-base-repo-template
-
-
Couldn't load subscription status.
- Fork 97
add test for events routers #1956
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
Open
angela-lee1
wants to merge
11
commits into
hackforla:development
Choose a base branch
from
angela-lee1:unit-test-events-router
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+247
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7483a61
add test for events routers
angela-lee1 c7d2eaf
clean up
angela-lee1 5b20a5e
Merge branch 'development' into unit-test-events-router
JackHaeg d05144d
Merge branch 'development' into unit-test-events-router
angela-lee1 b220119
Merge branch 'unit-test-events-router' of https://github.com/angela-l…
angela-lee1 5eed10b
Merge branch 'development' into unit-test-events-router
angela-lee1 2cad60a
Merge branch 'unit-test-events-router' of https://github.com/angela-l…
angela-lee1 7500c86
reuse mockEvent data
angela-lee1 ab54814
remove redundant assertions and add comments
angela-lee1 70577df
Merge branch 'development' into unit-test-events-router
jng34 a049902
Merge branch 'development' into unit-test-events-router
JackHaeg 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 |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| const express = require('express'); | ||
|
|
||
| const supertest = require('supertest'); | ||
| // Mock the Mongoose Event model | ||
| jest.mock('../models/event.model', () => ({ | ||
| Event: { | ||
| find: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| const { Event } = require('../models/event.model'); | ||
| //Mock the EventController to isolate router tests router tests fro controller logic | ||
| jest.mock('../controllers', () => ({ | ||
| EventController: { | ||
| event_list: jest.fn(), | ||
|
|
||
| create: jest.fn(), | ||
|
|
||
| event_by_id: jest.fn(), | ||
|
|
||
| destroy: jest.fn(), | ||
|
|
||
| update: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| const { EventController } = require('../controllers'); | ||
|
|
||
| const eventsRouter = require('./events.router'); | ||
| //Setup a test application | ||
| const testapp = express(); | ||
|
|
||
| testapp.use(express.json()); | ||
|
|
||
| testapp.use(express.urlencoded({ extended: false })); | ||
|
|
||
| testapp.use('/api/events', eventsRouter); | ||
|
|
||
| const request = supertest(testapp); | ||
|
|
||
| describe('Unit Tests for events.router.js', () => { | ||
| //Mock data | ||
| const mockEvent = { | ||
| _id: 'event123', | ||
|
|
||
| name: 'Test Event', | ||
|
|
||
| project: 'projectABC', | ||
|
|
||
| date: '2025-01-01T10:00:00Z', | ||
| }; | ||
|
|
||
| const mockEventId = 'event123'; | ||
|
|
||
| const mockProjectId = 'projectABC'; | ||
|
|
||
| const mockUpdatedEventData = { | ||
| name: 'Updated Test Event Name', | ||
| }; | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
| // Test suite for GET /api/events (event_list) | ||
|
|
||
| describe('GET /api/events (event_list)', () => { | ||
| it('should call EventController.event_list and return a list of events', async (done) => { | ||
| EventController.event_list.mockImplementationOnce((req, res) => | ||
| res.status(200).send([mockEvent]), | ||
| ); | ||
|
|
||
| const response = await request.get('/api/events'); | ||
|
|
||
| expect(EventController.event_list).toHaveBeenCalledWith( | ||
| expect.anything(), | ||
|
|
||
| expect.anything(), | ||
|
|
||
| expect.anything(), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(200); | ||
|
|
||
| expect(response.body).toEqual([mockEvent]); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
| // Test suite for POST /api/events (create) | ||
| describe('POST /api/events (create)', () => { | ||
| it('should call EventController.create and return the created event', async (done) => { | ||
| EventController.create.mockImplementationOnce((req, res) => res.status(201).send(mockEvent)); | ||
|
|
||
| const newEventData = { | ||
| name: mockEvent.name, | ||
|
|
||
| project: mockEvent.project, | ||
|
|
||
| date: mockEvent.date, | ||
| }; | ||
|
|
||
| const response = await request.post('/api/events/').send(newEventData); | ||
|
|
||
| expect(EventController.create).toHaveBeenCalledWith( | ||
| expect.objectContaining({ body: newEventData }), | ||
|
|
||
| expect.anything(), | ||
|
|
||
| expect.anything(), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(201); | ||
|
|
||
| expect(response.body).toEqual(mockEvent); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
| // Test suite for GET /api/events/:EventId (event_by_id) | ||
| describe('GET /api/events/:EventId (event_by_id)', () => { | ||
| it('should call EventController.event_by_id and return a specific event', async (done) => { | ||
| EventController.event_by_id.mockImplementationOnce((req, res) => | ||
| res.status(200).send(mockEvent), | ||
| ); | ||
|
|
||
| const response = await request.get(`/api/events/${mockEventId}`); | ||
|
|
||
| expect(EventController.event_by_id).toHaveBeenCalledWith( | ||
| expect.objectContaining({ params: { EventId: mockEventId } }), | ||
|
|
||
| expect.anything(), | ||
|
|
||
| expect.anything(), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(200); | ||
|
|
||
| expect(response.body).toEqual(mockEvent); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
| // Test suite for DELETE /api/events/:EventId (destroy) | ||
| describe('DELETE /api/events/:EventId (destroy)', () => { | ||
| it('should call EventController.destroy and return 204 No Content', async (done) => { | ||
| EventController.destroy.mockImplementationOnce((req, res) => res.status(204).send()); | ||
|
|
||
| const response = await request.delete(`/api/events/${mockEventId}`); | ||
|
|
||
| expect(EventController.destroy).toHaveBeenCalledWith( | ||
| expect.objectContaining({ params: { EventId: mockEventId } }), | ||
|
|
||
| expect.anything(), | ||
|
|
||
| expect.anything(), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(204); | ||
|
|
||
| expect(response.body).toEqual({}); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
| // Test suite for PATCH /api/events/:EventId (update) | ||
| describe('PATCH /api/events/:EventId (update)', () => { | ||
| it('should call EventController.update and return the updated event', async (done) => { | ||
| EventController.update.mockImplementationOnce((req, res) => | ||
| res.status(200).send({ ...mockEvent, ...mockUpdatedEventData }), | ||
| ); | ||
|
|
||
| const response = await request.patch(`/api/events/${mockEventId}`).send(mockUpdatedEventData); | ||
|
|
||
| expect(EventController.update).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| params: { EventId: mockEventId }, | ||
|
|
||
| body: mockUpdatedEventData, | ||
| }), | ||
|
|
||
| expect.anything(), | ||
|
|
||
| expect.anything(), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(200); | ||
|
|
||
| expect(response.body).toEqual({ ...mockEvent, ...mockUpdatedEventData }); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
| // Test suite for GET /api/events/nexteventbyproject/:id | ||
| describe('GET /api/events/nexteventbyproject/:id', () => { | ||
| it('should return the last event for a given project ID directly from the router', async (done) => { | ||
| const mockEventsForProject = [ | ||
| { _id: 'eventA', project: mockProjectId, name: 'Event A' }, | ||
|
|
||
| { _id: 'eventB', project: mockProjectId, name: 'Event B' }, | ||
|
|
||
| { _id: 'eventC', project: mockProjectId, name: 'Event C' }, | ||
| ]; | ||
|
|
||
| Event.find.mockImplementationOnce(() => ({ | ||
| populate: jest.fn().mockReturnThis(), | ||
|
|
||
| then: jest.fn(function (callback) { | ||
| return Promise.resolve(callback(mockEventsForProject)); | ||
| }), | ||
|
|
||
| catch: jest.fn(), | ||
| })); | ||
|
|
||
| const response = await request.get(`/api/events/nexteventbyproject/${mockProjectId}`); | ||
|
|
||
| expect(Event.find).toHaveBeenCalledWith({ project: mockProjectId }); | ||
|
|
||
| expect(Event.find.mock.results[0].value.populate).toHaveBeenCalledWith('project'); | ||
|
|
||
| expect(response.status).toBe(200); | ||
|
|
||
| expect(response.body).toEqual(mockEventsForProject[mockEventsForProject.length - 1]); | ||
|
|
||
| done(); | ||
| }); | ||
| // Test case for error handling when fetching next event by project | ||
| it('should return 500 if an error occurs when fetching next event by project', async (done) => { | ||
| const mockError = new Error('Simulated database error for next event by project'); | ||
|
|
||
| Event.find.mockImplementationOnce(() => ({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write some comments for this and other sections |
||
| populate: jest.fn().mockReturnThis(), | ||
|
|
||
| then: jest.fn(() => Promise.reject(mockError)), | ||
|
|
||
| catch: jest.fn(function (callback) { | ||
| return Promise.resolve(callback(mockError)); | ||
| }), | ||
| })); | ||
|
|
||
| const response = await request.get(`/api/events/nexteventbyproject/${mockProjectId}`); | ||
|
|
||
| expect(response.status).toBe(500); | ||
|
|
||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't really need this object. We could just reuse. mockEvent