-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa8deb9
commit ff1a5d4
Showing
8 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
api/controllers/coursesControllers/__tests__/registerToCourse.controller.test.js
This file contains 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,57 @@ | ||
jest.mock('../../helpers'); | ||
jest.mock('../../../../database/services/modelServices/courseServices'); | ||
|
||
const { registerToCourseController } = require('../index'); | ||
const { render500ErrorHelper } = require('../../helpers'); | ||
const { | ||
validateMockValueToHaveBeenCalled, | ||
} = require('../../../../utils/test-utils/validators.utils'); | ||
const { | ||
mockRequest, | ||
mockResponse, | ||
} = require('../../../../utils/test-utils/interceptors.utils'); | ||
const { | ||
findOneCourseService, | ||
} = require('../../../../database/services/modelServices/courseServices'); | ||
|
||
let req; | ||
let res; | ||
|
||
describe('registerToCourse Controller Test Suite', () => { | ||
beforeEach(() => { | ||
req = mockRequest(); | ||
res = mockResponse(); | ||
|
||
req.user = { _id: expect.anything() }; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should validate render500ErrorHelper is called ', async () => { | ||
findOneCourseService.mockReturnValueOnce(new Error()); | ||
|
||
await registerToCourseController(req, res); | ||
|
||
validateMockValueToHaveBeenCalled(findOneCourseService); | ||
validateMockValueToHaveBeenCalled(render500ErrorHelper); | ||
}); | ||
|
||
test('should validate res.redirect called ', async () => { | ||
findOneCourseService.mockImplementationOnce(() => ({ | ||
students: [], | ||
save: jest.fn(), | ||
})); | ||
|
||
await registerToCourseController(req, res); | ||
|
||
validateMockValueToHaveBeenCalled(findOneCourseService); | ||
|
||
const { flash } = req; | ||
const { redirect } = res; | ||
|
||
validateMockValueToHaveBeenCalled(flash); | ||
validateMockValueToHaveBeenCalled(redirect); | ||
}); | ||
}); |
This file contains 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
This file contains 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
22 changes: 22 additions & 0 deletions
22
api/controllers/coursesControllers/registerToCourse.controller.js
This file contains 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,22 @@ | ||
const { render500ErrorHelper } = require('../helpers'); | ||
const { | ||
findOneCourseService, | ||
} = require('../../../database/services/modelServices/courseServices'); | ||
|
||
exports.registerToCourseController = async (req, res) => { | ||
const { _id } = req.user; | ||
const { id } = req.params; | ||
|
||
const isFoundCourse = await findOneCourseService(id); | ||
|
||
if (isFoundCourse instanceof Error) { | ||
render500ErrorHelper(res); | ||
return; | ||
} | ||
|
||
isFoundCourse.students.push(_id); | ||
await isFoundCourse.save(); | ||
|
||
req.flash('success_msg', 'Successfully registered to course'); | ||
res.redirect('/my-courses'); | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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