-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(visualizator-be): Add unit tests for service methods
- Loading branch information
1 parent
6f5c009
commit 237e171
Showing
7 changed files
with
548 additions
and
14 deletions.
There are no files selected for viewing
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
125 changes: 125 additions & 0 deletions
125
apps/visualizator-be/src/channels/channels.service.test.ts
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,125 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { Channel } from './channel.entity'; | ||
import { ChannelService } from './channels.service'; | ||
|
||
describe('ChannelService', () => { | ||
let service: ChannelService; | ||
let mockRepository: Partial<Record<keyof Repository<Channel>, jest.Mock>>; | ||
|
||
beforeEach(async () => { | ||
mockRepository = { | ||
query: jest.fn(), | ||
}; | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
ChannelService, | ||
{ | ||
provide: getRepositoryToken(Channel), | ||
useValue: mockRepository, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<ChannelService>(ChannelService); | ||
}); | ||
|
||
describe('findAll', () => { | ||
const startTime = 1633046400; | ||
const endTime = 1633132800; | ||
|
||
it('should return an array of channels on successful fetch', async () => { | ||
const expectedResponse = [ | ||
{ id: 1, senderId: 101, recipientId: 201, totalCount: 5 }, | ||
{ id: 2, senderId: 102, recipientId: 202, totalCount: 3 }, | ||
]; | ||
|
||
mockRepository.query.mockResolvedValue(expectedResponse); | ||
|
||
const result = await service.findAll(startTime, endTime); | ||
|
||
expect(result).toEqual( | ||
expectedResponse.map((channel) => ({ | ||
id: channel.id, | ||
sender: channel.senderId, | ||
recipient: channel.recipientId, | ||
message_count: channel.totalCount, | ||
})), | ||
); | ||
expect(mockRepository.query).toHaveBeenCalledWith(expect.any(String), [ | ||
startTime, | ||
endTime, | ||
]); | ||
}); | ||
|
||
it('should return an empty array when no channels are found', async () => { | ||
mockRepository.query.mockResolvedValue([]); | ||
|
||
const result = await service.findAll(startTime, endTime); | ||
|
||
expect(result).toEqual([]); | ||
expect(mockRepository.query).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw an error when the query execution fails', async () => { | ||
mockRepository.query.mockRejectedValue( | ||
new Error('Query execution failed'), | ||
); | ||
|
||
await expect(service.findAll(startTime, endTime)).rejects.toThrow( | ||
'Query execution failed', | ||
); | ||
}); | ||
}); | ||
|
||
describe('findOne', () => { | ||
const channelId = 1; | ||
|
||
it('should return a channel when it exists', async () => { | ||
const expectedResponse = [ | ||
{ | ||
id: channelId, | ||
senderId: 101, | ||
recipientId: 201, | ||
totalCount: 5, | ||
status: 'accepted', | ||
}, | ||
]; | ||
|
||
mockRepository.query.mockResolvedValue(expectedResponse); | ||
|
||
const result = await service.findOne(channelId); | ||
|
||
expect(result).toEqual({ | ||
id: channelId, | ||
sender: expectedResponse[0].senderId, | ||
recipient: expectedResponse[0].recipientId, | ||
message_count: expectedResponse[0].totalCount, | ||
status: expectedResponse[0].status, | ||
}); | ||
expect(mockRepository.query).toHaveBeenCalledWith(expect.any(String), [ | ||
channelId, | ||
]); | ||
}); | ||
|
||
it('should throw an error when no channel is found', async () => { | ||
mockRepository.query.mockResolvedValue([]); | ||
|
||
await expect(service.findOne(channelId)).rejects.toThrow( | ||
`Channel with ID ${channelId} not found.`, | ||
); | ||
}); | ||
|
||
it('should throw an error when the query execution fails', async () => { | ||
mockRepository.query.mockRejectedValue( | ||
new Error('Query execution failed'), | ||
); | ||
|
||
await expect(service.findOne(channelId)).rejects.toThrow( | ||
'Query execution failed', | ||
); | ||
}); | ||
}); | ||
}); |
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,5 @@ | ||
export enum CountOption { | ||
ORIGIN = 'origin_para_id', | ||
DESTINATION = 'dest_para_id', | ||
BOTH = 'both', | ||
} |
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
Oops, something went wrong.