Skip to content

Commit

Permalink
test(visualizator-be): Add unit tests for service methods
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 authored and dudo50 committed Jul 8, 2024
1 parent 6f5c009 commit 237e171
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/visualizator-be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"testRegex": ".*\\.test\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
Expand Down
125 changes: 125 additions & 0 deletions apps/visualizator-be/src/channels/channels.service.test.ts
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',
);
});
});
});
5 changes: 5 additions & 0 deletions apps/visualizator-be/src/messages/count-option.ts
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',
}
3 changes: 2 additions & 1 deletion apps/visualizator-be/src/messages/messages.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { AccountXcmCountType } from './models/account-msg-count.model';
import { AssetCount } from './models/asset-count.model';
import { MessageCountByDay } from './models/message-count-by-day.model';
import { MessageCountByStatus } from './models/message-count-by-status.model';
import { CountOption, MessageCount } from './models/message-count.model';
import { MessageCount } from './models/message-count.model';
import { CountOption } from './count-option';

@Resolver(() => Message)
export class MessageResolver {
Expand Down
Loading

0 comments on commit 237e171

Please sign in to comment.