Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/globalConfig.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"mongoUri":"mongodb://127.0.0.1:56074/jest?","mongoDBName":"jest"}
{"mongoUri":"mongodb://127.0.0.1:35691/jest?","mongoDBName":"jest"}
115 changes: 115 additions & 0 deletions backend/routers/questions.router.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Mock and import Question model
jest.mock('../models/question.model');
const { Question } = require('../models');

// Import question router
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', () => {
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 a specific question with GET /api/questions/:id', async (done) => {
// 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();
});
});

describe('CREATE', () => {
it('should create a new question with POST /api/questions/', async (done) => {
// 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();
});
});
});