generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 97
Unit testing for Questions router #1913
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 6 commits into
hackforla:development
from
jng34:unitTestingforQuestions
Jun 24, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae82296
wrote unit tests for questions router
jng34 c3e882a
updated changes to tests
jng34 d981631
removed temporarily added script for testing router
jng34 acd0621
added testing for fail cases
jng34 534e1dc
Merge branch 'development' into unitTestingforQuestions
jng34 24bc69d
Merge branch 'development' into unitTestingforQuestions
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 |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // Mock and import Question model, import question router | ||
| jest.mock('../models/question.model'); | ||
| const { Question } = require('../models'); | ||
| const questionsRouter = require('./questions.router'); | ||
|
|
||
| // Create a test app with Express | ||
| const express = require('express'); | ||
| const supertest = require('supertest'); | ||
| const testapp = express(); | ||
| // Allow for body parsing of JSON data | ||
| testapp.use(express.json()); | ||
| // Allow for body parsing of HTML data | ||
| testapp.use(express.urlencoded({ extended: false })); | ||
| testapp.use('/api/questions/', questionsRouter); | ||
| const request = supertest(testapp); | ||
|
|
||
| describe('Unit tests for questions router', () => { | ||
| // Clear all mocks after each test | ||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('READ', () => { | ||
| // Mock question data | ||
| const mockQuestions = [ | ||
| { | ||
| id: 1, | ||
| questionText: 'What is your favorite color?', | ||
| htmlName: 'favoriteColor', | ||
| answers: { | ||
| answerOneText: 'Red', | ||
| answerTwoText: 'Blue', | ||
| answerThreeText: 'Green', | ||
| answerFourText: 'Yellow', | ||
| }, | ||
| }, | ||
| { | ||
| id: 2, | ||
| questionText: 'What is your favorite food?', | ||
| htmlName: 'favoriteFood', | ||
| answers: { | ||
| answerOneText: 'Pizza', | ||
| answerTwoText: 'Cheeseburger', | ||
| answerThreeText: 'Sushi', | ||
| answerFourText: 'Chicken', | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| it('should return all questions with GET /api/questions', async (done) => { | ||
| // Mock the Question.find() method | ||
| Question.find.mockResolvedValue(mockQuestions); | ||
|
|
||
| // Mock the request to the API | ||
| const response = await request.get('/api/questions'); | ||
|
|
||
| // Tests | ||
| expect(Question.find).toHaveBeenCalled(); | ||
| expect(response.status).toBe(200); | ||
| expect(response.body).toEqual(mockQuestions); | ||
|
|
||
| // Marks completion of tests | ||
| done(); | ||
| }); | ||
|
|
||
| it('should return 400 status code when there is an error with GET /api/questions', async (done) => { | ||
| // Mock the error thrown when find method is called | ||
| const error = new Error('Database error'); | ||
| Question.find.mockRejectedValue(error); | ||
|
|
||
| // Mock console log function | ||
| const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
|
|
||
| // Mock the request to the API | ||
| const response = await request.get('/api/questions'); | ||
|
|
||
| // Tests | ||
| expect(Question.find).toHaveBeenCalled(); | ||
| expect(consoleLogSpy).toHaveBeenCalledWith(error); | ||
| expect(response.status).toBe(400); | ||
|
|
||
| // Clean up and restores original console log function | ||
| consoleLogSpy.mockRestore(); | ||
| // Marks completion of tests | ||
| done(); | ||
| }); | ||
|
|
||
| it('should return a specific question with GET /api/questions/:id', async (done) => { | ||
jng34 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Mock the Question.findById() method | ||
| const mockQuestion = mockQuestions[0]; | ||
| const { id } = mockQuestion; | ||
| Question.findById.mockResolvedValue(mockQuestion); | ||
|
|
||
| // Mock the request to the API | ||
| const response = await request.get(`/api/questions/${id}`); | ||
|
|
||
| // Tests | ||
| expect(Question.findById).toHaveBeenCalledWith(`${id}`); | ||
| expect(response.status).toBe(200); | ||
| expect(response.body).toEqual(mockQuestion); | ||
|
|
||
| // Marks completion of tests | ||
| done(); | ||
| }); | ||
|
|
||
| it('should return 400 status code when there is an error with GET /api/questions/:id', async (done) => { | ||
| // Mock user id | ||
| const id = mockQuestions[0].id; | ||
|
|
||
| // Mock the error when findById method is called | ||
| const error = new Error('Database error'); | ||
| Question.findById.mockRejectedValue(error); | ||
|
|
||
| // Mock console log function | ||
| const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
|
|
||
| // Mock the request to the API | ||
| const response = await request.get(`/api/questions/${id}`); | ||
|
|
||
| // Tests | ||
| expect(Question.findById).toHaveBeenCalledWith(`${id}`); | ||
| expect(consoleLogSpy).toHaveBeenCalledWith(error); | ||
| expect(response.status).toBe(400); | ||
|
|
||
| // Clean up and restores original console log function | ||
| consoleLogSpy.mockRestore(); | ||
| // Marks completion of tests | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('CREATE', () => { | ||
| it('should create a new question with POST /api/questions/', async (done) => { | ||
jng34 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Mock the Question.create() method | ||
| const newQuestion = { | ||
| id: 3, | ||
| questionText: 'What is your favorite animal?', | ||
| htmlName: 'favoriteAnimal', | ||
| answers: { | ||
| answerOneText: 'Dog', | ||
| answerTwoText: 'Cat', | ||
| answerThreeText: 'Bird', | ||
| answerFourText: 'Fish', | ||
| }, | ||
| }; | ||
|
|
||
| // Mock Question.create method | ||
| Question.create.mockResolvedValue(newQuestion); | ||
|
|
||
| // Mock the request to the API | ||
| const response = await request.post('/api/questions/').send(newQuestion); | ||
|
|
||
| // Tests | ||
| expect(Question.create).toHaveBeenCalledWith(newQuestion); | ||
| expect(response.status).toBe(201); | ||
|
|
||
| // Marks completion of tests | ||
| done(); | ||
| }); | ||
|
|
||
| it('should return 400 status code when there is an error with POST /api/questions', async (done) => { | ||
| // Mock the error thrown when create method is called | ||
| const error = new Error('Database error'); | ||
| Question.create.mockRejectedValue(error); | ||
|
|
||
| // Mock console log function | ||
| const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
|
|
||
| // Mock the request to the API | ||
| const response = await request.post('/api/questions'); | ||
|
|
||
| // Tests | ||
| expect(Question.create).toHaveBeenCalled(); | ||
| expect(consoleLogSpy).toHaveBeenCalledWith(error); | ||
| expect(response.status).toBe(400); | ||
|
|
||
| // Clean up and restores original console log function | ||
| consoleLogSpy.mockRestore(); | ||
| // Marks completion of tests | ||
| 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.