``import { Test, TestingModule } from '@nestjs/testing';
import { ChatsGateway } from './chats.gateway';
import { Server } from 'socket.io';
describe('ChatsGateway', () => {
let gateway: ChatsGateway;
let server: Server;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ChatsGateway],
}).compile();
gateway = module.get<ChatsGateway>(ChatsGateway);
server = new Server(); // Mock WebSocket Server
gateway.server = server;
});
it('should be defined', () => {
expect(gateway).toBeDefined();
});
it('should log a message when a client connects', () => {
const mockClient = { id: '1234' } as any;
jest.spyOn(console, 'log');
gateway.handleConnection(mockClient);
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Client connected'));
});
it('should log a message when a client disconnects', () => {
const mockClient = { id: '1234' } as any;
jest.spyOn(console, 'log');
gateway.handleDisconnect(mockClient);
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Client disconnected'));
});
});``