Skip to content

Commit

Permalink
test: fixed chathub tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ehildt committed Sep 15, 2024
1 parent cf31903 commit ed85de9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
29 changes: 16 additions & 13 deletions apps/chat-hub/src/services/chat.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
import { ClientProxy } from '@nestjs/microservices';
import { Test } from '@nestjs/testing';

import { REDIS_PUBSUB } from '@/constants/app.constants';
import { ChatUpsertReq } from '@/dtos/chat-upsert.dto.req';
import { PubSupService } from '@/modules/pubsub/pubsub.service';
import { ChatHubGateway } from '@/modules/web-socket/chat-hub.gateway';

import { ChatService } from './chat.service';
import { ConfigFactoryService } from './config-factory.service';

describe('ChatService', () => {
let chatService: ChatService;
let mockRedisPubSub: jest.Mocked<ClientProxy>;
let mockRedisPubSub: jest.Mocked<PubSupService>;
let mockConfigFactory: Partial<ConfigFactoryService>;

beforeEach(async () => {
mockConfigFactory = {};
const moduleRef = await Test.createTestingModule({
providers: [
ChatHubGateway,
ChatService,
{
provide: ConfigFactoryService,
useValue: mockConfigFactory,
},
{
provide: REDIS_PUBSUB,
provide: PubSupService,
useValue: {
emit: jest.fn(),
publish: jest.fn(),
},
},
],
}).compile();

chatService = moduleRef.get<ChatService>(ChatService);
mockRedisPubSub = moduleRef.get(REDIS_PUBSUB);
chatService = moduleRef.get(ChatService);
mockRedisPubSub = moduleRef.get(PubSupService);
});

afterEach(() => {
Expand All @@ -42,17 +43,19 @@ describe('ChatService', () => {
it('should distribute messages to realms using enabled messaging options', async () => {
const reqs: ChatUpsertReq[] = [
{
topic: 'realm1',
message: 'value1',
topic: { id: '1', name: 'cookies' },
message: 'i love cookies',
publisherId: 'user_1',
},
{
topic: 'realm2',
messages: 'value2',
topic: { id: '2', name: 'how to stop simping' },
message: 'start by going to gym frequently',
publisherId: 'user_1',
},
];

await chatService.onMessage(reqs);
expect(mockRedisPubSub.emit).toHaveBeenCalledTimes(2);
await chatService.publish(reqs);
expect(mockRedisPubSub.publish).toHaveBeenCalledTimes(2);
});
});
});
16 changes: 8 additions & 8 deletions apps/chat-hub/src/services/config-factory.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ConfigService } from '@nestjs/config';
import { Transport } from '@nestjs/microservices';
import { Test, TestingModule } from '@nestjs/testing';

import { ConfigFactoryService } from './config-factory.service';
Expand Down Expand Up @@ -53,13 +52,14 @@ describe('ConfigFactoryService', () => {
});

expect(service.redisPubSub).toEqual({
transport: Transport.REDIS,
options: {
port: 6379,
host: 'localhost',
password: 'redispassword',
username: 'redisuser',
},
autoResubscribe: true,
connectionName: 'CHAT_BRCS',
host: 'localhost',
keepAlive: 3000,
offlineQueue: true,
password: 'redispassword',
port: 6379,
username: 'redisuser',
});
});
});
Expand Down
1 change: 1 addition & 0 deletions cspell.ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ rimraf
Securityscorecard
Servicestack
sibiraj
simping
snapcode
sonarjs
spdx
Expand Down

0 comments on commit ed85de9

Please sign in to comment.