|
| 1 | +// Mock and import Question model, import question router |
| 2 | +jest.mock('../models/question.model'); |
| 3 | +const { Question } = require('../models'); |
| 4 | +const questionsRouter = require('./questions.router'); |
| 5 | + |
| 6 | +// Create a test app with Express |
| 7 | +const express = require('express'); |
| 8 | +const supertest = require('supertest'); |
| 9 | +const testapp = express(); |
| 10 | +// Allow for body parsing of JSON data |
| 11 | +testapp.use(express.json()); |
| 12 | +// Allow for body parsing of HTML data |
| 13 | +testapp.use(express.urlencoded({ extended: false })); |
| 14 | +testapp.use('/api/questions/', questionsRouter); |
| 15 | +const request = supertest(testapp); |
| 16 | + |
| 17 | +describe('Unit tests for questions router', () => { |
| 18 | + // Clear all mocks after each test |
| 19 | + afterEach(() => { |
| 20 | + jest.clearAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('READ', () => { |
| 24 | + // Mock question data |
| 25 | + const mockQuestions = [ |
| 26 | + { |
| 27 | + id: 1, |
| 28 | + questionText: 'What is your favorite color?', |
| 29 | + htmlName: 'favoriteColor', |
| 30 | + answers: { |
| 31 | + answerOneText: 'Red', |
| 32 | + answerTwoText: 'Blue', |
| 33 | + answerThreeText: 'Green', |
| 34 | + answerFourText: 'Yellow', |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + id: 2, |
| 39 | + questionText: 'What is your favorite food?', |
| 40 | + htmlName: 'favoriteFood', |
| 41 | + answers: { |
| 42 | + answerOneText: 'Pizza', |
| 43 | + answerTwoText: 'Cheeseburger', |
| 44 | + answerThreeText: 'Sushi', |
| 45 | + answerFourText: 'Chicken', |
| 46 | + }, |
| 47 | + }, |
| 48 | + ]; |
| 49 | + |
| 50 | + it('should return all questions with GET /api/questions', async (done) => { |
| 51 | + // Mock the Question.find() method |
| 52 | + Question.find.mockResolvedValue(mockQuestions); |
| 53 | + |
| 54 | + // Mock the request to the API |
| 55 | + const response = await request.get('/api/questions'); |
| 56 | + |
| 57 | + // Tests |
| 58 | + expect(Question.find).toHaveBeenCalled(); |
| 59 | + expect(response.status).toBe(200); |
| 60 | + expect(response.body).toEqual(mockQuestions); |
| 61 | + |
| 62 | + // Marks completion of tests |
| 63 | + done(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return 400 status code when there is an error with GET /api/questions', async (done) => { |
| 67 | + // Mock the error thrown when find method is called |
| 68 | + const error = new Error('Database error'); |
| 69 | + Question.find.mockRejectedValue(error); |
| 70 | + |
| 71 | + // Mock console log function |
| 72 | + const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 73 | + |
| 74 | + // Mock the request to the API |
| 75 | + const response = await request.get('/api/questions'); |
| 76 | + |
| 77 | + // Tests |
| 78 | + expect(Question.find).toHaveBeenCalled(); |
| 79 | + expect(consoleLogSpy).toHaveBeenCalledWith(error); |
| 80 | + expect(response.status).toBe(400); |
| 81 | + |
| 82 | + // Clean up and restores original console log function |
| 83 | + consoleLogSpy.mockRestore(); |
| 84 | + // Marks completion of tests |
| 85 | + done(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return a specific question with GET /api/questions/:id', async (done) => { |
| 89 | + // Mock the Question.findById() method |
| 90 | + const mockQuestion = mockQuestions[0]; |
| 91 | + const { id } = mockQuestion; |
| 92 | + Question.findById.mockResolvedValue(mockQuestion); |
| 93 | + |
| 94 | + // Mock the request to the API |
| 95 | + const response = await request.get(`/api/questions/${id}`); |
| 96 | + |
| 97 | + // Tests |
| 98 | + expect(Question.findById).toHaveBeenCalledWith(`${id}`); |
| 99 | + expect(response.status).toBe(200); |
| 100 | + expect(response.body).toEqual(mockQuestion); |
| 101 | + |
| 102 | + // Marks completion of tests |
| 103 | + done(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should return 400 status code when there is an error with GET /api/questions/:id', async (done) => { |
| 107 | + // Mock user id |
| 108 | + const id = mockQuestions[0].id; |
| 109 | + |
| 110 | + // Mock the error when findById method is called |
| 111 | + const error = new Error('Database error'); |
| 112 | + Question.findById.mockRejectedValue(error); |
| 113 | + |
| 114 | + // Mock console log function |
| 115 | + const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 116 | + |
| 117 | + // Mock the request to the API |
| 118 | + const response = await request.get(`/api/questions/${id}`); |
| 119 | + |
| 120 | + // Tests |
| 121 | + expect(Question.findById).toHaveBeenCalledWith(`${id}`); |
| 122 | + expect(consoleLogSpy).toHaveBeenCalledWith(error); |
| 123 | + expect(response.status).toBe(400); |
| 124 | + |
| 125 | + // Clean up and restores original console log function |
| 126 | + consoleLogSpy.mockRestore(); |
| 127 | + // Marks completion of tests |
| 128 | + done(); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('CREATE', () => { |
| 133 | + it('should create a new question with POST /api/questions/', async (done) => { |
| 134 | + // Mock the Question.create() method |
| 135 | + const newQuestion = { |
| 136 | + id: 3, |
| 137 | + questionText: 'What is your favorite animal?', |
| 138 | + htmlName: 'favoriteAnimal', |
| 139 | + answers: { |
| 140 | + answerOneText: 'Dog', |
| 141 | + answerTwoText: 'Cat', |
| 142 | + answerThreeText: 'Bird', |
| 143 | + answerFourText: 'Fish', |
| 144 | + }, |
| 145 | + }; |
| 146 | + |
| 147 | + // Mock Question.create method |
| 148 | + Question.create.mockResolvedValue(newQuestion); |
| 149 | + |
| 150 | + // Mock the request to the API |
| 151 | + const response = await request.post('/api/questions/').send(newQuestion); |
| 152 | + |
| 153 | + // Tests |
| 154 | + expect(Question.create).toHaveBeenCalledWith(newQuestion); |
| 155 | + expect(response.status).toBe(201); |
| 156 | + |
| 157 | + // Marks completion of tests |
| 158 | + done(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should return 400 status code when there is an error with POST /api/questions', async (done) => { |
| 162 | + // Mock the error thrown when create method is called |
| 163 | + const error = new Error('Database error'); |
| 164 | + Question.create.mockRejectedValue(error); |
| 165 | + |
| 166 | + // Mock console log function |
| 167 | + const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 168 | + |
| 169 | + // Mock the request to the API |
| 170 | + const response = await request.post('/api/questions'); |
| 171 | + |
| 172 | + // Tests |
| 173 | + expect(Question.create).toHaveBeenCalled(); |
| 174 | + expect(consoleLogSpy).toHaveBeenCalledWith(error); |
| 175 | + expect(response.status).toBe(400); |
| 176 | + |
| 177 | + // Clean up and restores original console log function |
| 178 | + consoleLogSpy.mockRestore(); |
| 179 | + // Marks completion of tests |
| 180 | + done(); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments