-
Notifications
You must be signed in to change notification settings - Fork 0
User module unit test #44
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
Open
DennisTemoye
wants to merge
5
commits into
main
Choose a base branch
from
user-module-unit-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
566bf00
Unit test: created test for user controller
Olamideod-tech bc9cf72
created the unit test for the user module
DennisTemoye 75bdf58
new:created unit test for user module
DennisTemoye f910460
new:created unit test for user module
DennisTemoye 43a31e6
Merge branch 'main' into user-module-unit-test
DennisTemoye 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -68,7 +68,8 @@ | |
| "@faker-js/faker": "^8.4.1", | ||
| "@nestjs/cli": "^10.0.0", | ||
| "@nestjs/schematics": "^10.0.0", | ||
| "@nestjs/testing": "^11.0.15", | ||
|
|
||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DennisTemoye an extra space added |
||
| "@types/bcryptjs": "^2.4.4", | ||
| "@types/express": "^4.17.17", | ||
| "@types/jest": "^29.5.2", | ||
|
|
||
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
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 |
|---|---|---|
| @@ -1,30 +1,94 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { JwtService } from '@nestjs/jwt'; | ||
| import { TestModule } from '../testkits'; | ||
| import { DataLogsController } from './data.logs.controller'; | ||
| import { DataLogsService } from './data.logs.service'; | ||
| import { JwtAdminsGuard } from '../auth/guards/jwt.admins.guard'; | ||
| import { JwtUsersGuard } from '../auth/guards/jwt.users.guard'; | ||
| import { NotFoundException } from '@nestjs/common'; | ||
|
|
||
| describe('DataLogsController', () => { | ||
| let dataLogsController: DataLogsController; | ||
| let controller: DataLogsController; | ||
| let logsService: DataLogsService; | ||
|
|
||
| const mockLogsService = { | ||
| findAll: jest.fn(), | ||
| publicLog: jest.fn(), | ||
| remove: jest.fn(), | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| const app: TestingModule = await Test.createTestingModule({ | ||
| imports: [TestModule], | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [DataLogsController], | ||
| providers: [DataLogsService], | ||
| providers: [ | ||
| { | ||
| provide: DataLogsService, | ||
| useValue: mockLogsService, | ||
| }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| dataLogsController = app.get<DataLogsController>( | ||
| DataLogsController, | ||
| ) as DataLogsController; | ||
| controller = module.get<DataLogsController>(DataLogsController); | ||
| logsService = module.get<DataLogsService>(DataLogsService); | ||
| }); | ||
|
|
||
| describe('findAll', () => { | ||
| it('should return all logs for admins', async () => { | ||
| const mockResponse = [{ id: '1', message: 'Log entry' }]; | ||
| mockLogsService.findAll.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await controller.findAll({ query: {} } as any); | ||
| expect(result).toEqual(mockResponse); | ||
| expect(logsService.findAll).toHaveBeenCalledWith({ query: {} }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('logUser', () => { | ||
| it('should log a user activity', async () => { | ||
| const mockRequest = { body: { message: 'User action' } }; | ||
| const mockSource = 'user-app'; | ||
| mockLogsService.publicLog.mockResolvedValue({ success: true }); | ||
|
|
||
| const result = await controller.logUser(mockRequest as any, mockSource); | ||
| expect(result).toEqual({ success: true }); | ||
| expect(logsService.publicLog).toHaveBeenCalledWith( | ||
| mockRequest, | ||
| mockSource, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| global.dataLogsService = app.get<DataLogsService>(DataLogsService); | ||
| global.jwtService = app.get<JwtService>(JwtService); | ||
| describe('logAdmin', () => { | ||
| it('should log an admin activity', async () => { | ||
| const mockRequest = { body: { message: 'Admin action' } }; | ||
| const mockSource = 'admin-dashboard'; | ||
| mockLogsService.publicLog.mockResolvedValue({ success: true }); | ||
|
|
||
| const result = await controller.logAdmin(mockRequest as any, mockSource); | ||
| expect(result).toEqual({ success: true }); | ||
| expect(logsService.publicLog).toHaveBeenCalledWith( | ||
| mockRequest, | ||
| mockSource, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('dataLogsController', () => { | ||
| it('should return true for dataLogsController"', () => { | ||
| expect(!!dataLogsController).toBe(true); | ||
| describe('remove', () => { | ||
| it('should remove a data log by ID', async () => { | ||
| const mockLogId = 'log123'; | ||
| mockLogsService.remove.mockResolvedValue({ success: true }); | ||
|
|
||
| const result = await controller.remove(mockLogId); | ||
| expect(result).toEqual({ success: true }); | ||
| expect(logsService.remove).toHaveBeenCalledWith(mockLogId); | ||
| }); | ||
|
|
||
| it('should throw NotFoundException if log is not found', async () => { | ||
| mockLogsService.remove.mockImplementationOnce(() => { | ||
| throw new NotFoundException('Log not found'); | ||
| }); | ||
|
|
||
| await expect(controller.remove('invalid-log-id')).rejects.toThrow( | ||
| NotFoundException, | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no new change in package.json; There is no need to push your local installation changes. (package-lock.json)