Skip to content

Commit

Permalink
✨ Show student list complete
Browse files Browse the repository at this point in the history
  • Loading branch information
reMRKableDev committed Jan 8, 2021
1 parent 35f5bd2 commit 219b70f
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
jest.mock('../../helpers');
jest.mock('../../../../database/services/modelServices/courseServices');

const {
renderStudentListController,
} = require('../renderStudentList.controller');
const {
render500ErrorHelper,
redirectNonexistentCourseHelper,
} = require('../../helpers');
const {
findOneCourseService,
} = require('../../../../database/services/modelServices/courseServices');
const {
validateMockValueToHaveBeenCalled,
} = require('../../../../utils/test-utils/validators.utils');
const {
mockRequest,
mockResponse,
} = require('../../../../utils/test-utils/interceptors.utils');

let req;
let res;

describe('renderStudentList Controller Test Suite', () => {
beforeEach(() => {
req = mockRequest();
res = mockResponse();

req.user = {
local: expect.anything(),
};
});

afterEach(() => {
jest.clearAllMocks();
});

test('should validate render500ErrorHelper is called', async () => {
findOneCourseService.mockReturnValueOnce(new Error());

await renderStudentListController(req, res);

validateMockValueToHaveBeenCalled(render500ErrorHelper);
});

test('should validate redirectNonexistentCourseHelper is called', async () => {
findOneCourseService.mockReturnValueOnce(null);

await renderStudentListController(req, res);

validateMockValueToHaveBeenCalled(redirectNonexistentCourseHelper);
});

test('should validate res.status & res.render is called', async () => {
await renderStudentListController(req, res);

const { status, render } = res;

validateMockValueToHaveBeenCalled(status);
validateMockValueToHaveBeenCalled(render);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ jest.mock('../../../../database/services/modelServices/courseServices');
const {
renderTaughtCourseController,
} = require('../renderTaughtCourse.controller');
const { render500ErrorHelper } = require('../../helpers');
const {
render500ErrorHelper,
redirectNonexistentCourseHelper,
} = require('../../helpers');
const {
findOneCourseService,
} = require('../../../../database/services/modelServices/courseServices');
Expand Down Expand Up @@ -41,16 +44,12 @@ describe('renderTaughtCourse Controller Test Suite', () => {
validateMockValueToHaveBeenCalled(render500ErrorHelper);
});

test('should validate req.flash & res.redirect is called', async () => {
test('should validate redirectNonexistentCourseHelper is called', async () => {
findOneCourseService.mockReturnValueOnce(null);

await renderTaughtCourseController(req, res);

const { flash } = req;
const { redirect } = res;

validateMockValueToHaveBeenCalled(flash);
validateMockValueToHaveBeenCalled(redirect);
validateMockValueToHaveBeenCalled(redirectNonexistentCourseHelper);
});

test('should validate res.status & res.render is called', async () => {
Expand Down
9 changes: 8 additions & 1 deletion api/controllers/coursesControllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ const { renderMyCoursesController } = require('./renderMyCourses.controller');
const {
renderTaughtCourseController,
} = require('./renderTaughtCourse.controller');
const {
renderStudentListController,
} = require('./renderStudentList.controller');

module.exports = { renderMyCoursesController, renderTaughtCourseController };
module.exports = {
renderMyCoursesController,
renderStudentListController,
renderTaughtCourseController,
};
27 changes: 27 additions & 0 deletions api/controllers/coursesControllers/renderStudentList.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const {
findOneCourseService,
} = require('../../../database/services/modelServices/courseServices');

const {
render500ErrorHelper,
redirectNonexistentCourseHelper,
} = require('../helpers');

exports.renderStudentListController = async (req, res) => {
const { local } = req.user;
const { id } = req.params;

const isCourse = await findOneCourseService(id);

if (isCourse instanceof Error) {
render500ErrorHelper(res);
return;
}

if (isCourse === null) {
redirectNonexistentCourseHelper(req, res);
return;
}

res.status(200).render('users/instructors/student-list', { local });
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const {
findOneCourseService,
} = require('../../../database/services/modelServices/courseServices');

const { render500ErrorHelper } = require('../helpers');
const {
render500ErrorHelper,
redirectNonexistentCourseHelper,
} = require('../helpers');

exports.renderTaughtCourseController = async (req, res) => {
const { local } = req.user;
Expand All @@ -16,8 +18,7 @@ exports.renderTaughtCourseController = async (req, res) => {
}

if (isCourse === null) {
req.flash('error_msg', "Course doesn't exist!");
res.redirect(302, '/');
redirectNonexistentCourseHelper(req, res);
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { redirectNonexistentCourseHelper } = require('../index');
const {
mockRequest,
mockResponse,
} = require('../../../../utils/test-utils/interceptors.utils');

const {
validateMockValueToHaveBeenCalled,
} = require('../../../../utils/test-utils/validators.utils');

describe('redirectNonexistentCourse Helper Test Suite', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('should validate res.status & res.render being called', () => {
const req = mockRequest();
const res = mockResponse();

redirectNonexistentCourseHelper(req, res);

const { flash } = req;
const { redirect } = res;

validateMockValueToHaveBeenCalled(flash);
validateMockValueToHaveBeenCalled(redirect);
});
});
4 changes: 4 additions & 0 deletions api/controllers/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const {
const {
renderRegistrationFormWithErrorsHelper,
} = require('./renderRegistrationFormWithErrors.helper');
const {
redirectNonexistentCourseHelper,
} = require('./redirectNonexistentCourse.helper');

module.exports = {
filterCoursesHelper,
Expand All @@ -20,6 +23,7 @@ module.exports = {
authenticateUserHelper,
checkIfValidObjectIdHelper,
handleUpdatedPasswordHelper,
redirectNonexistentCourseHelper,
renderLoginFormWithErrorsHelper,
renderRegistrationFormWithErrorsHelper,
};
4 changes: 4 additions & 0 deletions api/controllers/helpers/redirectNonexistentCourse.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.redirectNonexistentCourseHelper = (request, response) => {
request.flash('error_msg', "Course doesn't exist!");
response.redirect(302, '/');
};
2 changes: 2 additions & 0 deletions api/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
} = require('./instructorsControllers');
const {
renderMyCoursesController,
renderStudentListController,
renderTaughtCourseController,
} = require('./coursesControllers');

Expand All @@ -34,6 +35,7 @@ module.exports = {
registerNewUserController,
createNewCourseController,
renderMyCoursesController,
renderStudentListController,
updateUserProfileController,
renderUserProfileController,
renderTaughtCourseController,
Expand Down
13 changes: 12 additions & 1 deletion api/routes/courses.routes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const { Router } = require('express');
const { isInstructor, isLoggedInUser } = require('../middleware');
const { myCoursesPrefix, myCoursesTeachPrefix } = require('../../configs');
const {
myCoursesPrefix,
myCoursesTeachPrefix,
myCoursesTeachStudentListPrefix,
} = require('../../configs');
const {
renderMyCoursesController,
renderStudentListController,
renderTaughtCourseController,
} = require('../controllers');

Expand All @@ -15,5 +20,11 @@ router.get(
isInstructor,
renderTaughtCourseController
);
router.get(
myCoursesTeachStudentListPrefix,
isLoggedInUser,
isInstructor,
renderStudentListController
);

module.exports = router;
1 change: 1 addition & 0 deletions configs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
newCoursePrefix: '/course/new',
myCoursesPrefix: '/my-courses',
myCoursesTeachPrefix: '/my-courses/teach/:id',
myCoursesTeachStudentListPrefix: '/my-courses/teach/:id/studentList',
myCoursesLearnPrefix: '/my-courses/learn/:id',

/* Database */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const { findOneCourseService, createNewCourseService } = require('../index');
const { fakeCourseData, fakeIdFormatData } = require('../../../../fixtures');
const { createNewUserService } = require('../../userServices');
const {
fakeUserData,
fakeCourseData,
fakeIdFormatData,
} = require('../../../../fixtures');
const {
dbConnect,
dbDisconnect,
Expand All @@ -23,10 +28,17 @@ describe('findOneCourse Service Test Suite', () => {
});

test('should validate successfully finding one saved course', async () => {
const newUser = await createNewUserService(fakeUserData);

const { title, description } = fakeCourseData;

const newCourse = await createNewCourseService(title, description);

const { _id: userId } = newUser;

newCourse.instructors.push(userId);
newCourse.save();

const { _id } = newCourse;

const results = await findOneCourseService(_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {

exports.findOneCourseService = async (courseId) => {
const [foundResults, foundError] = await handleAsyncFunction(
Course.findById(courseId)
Course.findById(courseId).populate('instructors students')
);

return foundResults || foundError;
Expand Down
2 changes: 1 addition & 1 deletion views/users/instructors/courses-taught.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div>
<div>
<p>Registered Students: <span>{{isCourse.students.length}}</span></p>
<a href="/my-courses/{{isCourse._id}}/studentList">See Student List</a>
<a href="/my-courses/teach/{{isCourse._id}}/studentList">See Student List</a>
</div>
<div>
<p>Edit Course</p>
Expand Down
20 changes: 20 additions & 0 deletions views/users/instructors/student-list.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<section>
<h2>Students List</h2>

{{!-- <table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
{{#each nameList}}
<tr>
<td>{{this.firstName}}</td>
<td>{{this.lastName}}</td>
</tr>
{{/each}}
</tbody>
</table> --}}
</section>

0 comments on commit 219b70f

Please sign in to comment.