- Added unit tests for WebSocket event emissions in `GroupsService` and `ProjectsService` (create, update, delete, and collaborator actions). - Added new test files for `WebSocketsGateway` and `WebSocketsService` to ensure correct event handling and gateway connections. - Improved test structure and coverage for real-time functionalities.
126 lines
4.3 KiB
TypeScript
126 lines
4.3 KiB
TypeScript
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { WebSocketsService } from './websockets.service';
|
|
import { WebSocketsGateway } from './websockets.gateway';
|
|
|
|
describe('WebSocketsService', () => {
|
|
let service: WebSocketsService;
|
|
let mockWebSocketsGateway: Partial<WebSocketsGateway>;
|
|
|
|
beforeEach(async () => {
|
|
// Create mock for WebSocketsGateway
|
|
mockWebSocketsGateway = {
|
|
emitProjectUpdated: jest.fn(),
|
|
emitCollaboratorAdded: jest.fn(),
|
|
emitGroupCreated: jest.fn(),
|
|
emitGroupUpdated: jest.fn(),
|
|
emitPersonAddedToGroup: jest.fn(),
|
|
emitPersonRemovedFromGroup: jest.fn(),
|
|
emitNotification: jest.fn(),
|
|
emitProjectNotification: jest.fn(),
|
|
};
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
WebSocketsService,
|
|
{
|
|
provide: WebSocketsGateway,
|
|
useValue: mockWebSocketsGateway,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<WebSocketsService>(WebSocketsService);
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe('emitProjectUpdated', () => {
|
|
it('should call gateway.emitProjectUpdated with correct parameters', () => {
|
|
const projectId = 'project1';
|
|
const data = { action: 'updated', project: { id: projectId } };
|
|
|
|
service.emitProjectUpdated(projectId, data);
|
|
|
|
expect(mockWebSocketsGateway.emitProjectUpdated).toHaveBeenCalledWith(projectId, data);
|
|
});
|
|
});
|
|
|
|
describe('emitCollaboratorAdded', () => {
|
|
it('should call gateway.emitCollaboratorAdded with correct parameters', () => {
|
|
const projectId = 'project1';
|
|
const data = { project: { id: projectId }, user: { id: 'user1' } };
|
|
|
|
service.emitCollaboratorAdded(projectId, data);
|
|
|
|
expect(mockWebSocketsGateway.emitCollaboratorAdded).toHaveBeenCalledWith(projectId, data);
|
|
});
|
|
});
|
|
|
|
describe('emitGroupCreated', () => {
|
|
it('should call gateway.emitGroupCreated with correct parameters', () => {
|
|
const projectId = 'project1';
|
|
const data = { action: 'created', group: { id: 'group1' } };
|
|
|
|
service.emitGroupCreated(projectId, data);
|
|
|
|
expect(mockWebSocketsGateway.emitGroupCreated).toHaveBeenCalledWith(projectId, data);
|
|
});
|
|
});
|
|
|
|
describe('emitGroupUpdated', () => {
|
|
it('should call gateway.emitGroupUpdated with correct parameters', () => {
|
|
const projectId = 'project1';
|
|
const data = { action: 'updated', group: { id: 'group1' } };
|
|
|
|
service.emitGroupUpdated(projectId, data);
|
|
|
|
expect(mockWebSocketsGateway.emitGroupUpdated).toHaveBeenCalledWith(projectId, data);
|
|
});
|
|
});
|
|
|
|
describe('emitPersonAddedToGroup', () => {
|
|
it('should call gateway.emitPersonAddedToGroup with correct parameters', () => {
|
|
const projectId = 'project1';
|
|
const data = { group: { id: 'group1' }, person: { id: 'person1' } };
|
|
|
|
service.emitPersonAddedToGroup(projectId, data);
|
|
|
|
expect(mockWebSocketsGateway.emitPersonAddedToGroup).toHaveBeenCalledWith(projectId, data);
|
|
});
|
|
});
|
|
|
|
describe('emitPersonRemovedFromGroup', () => {
|
|
it('should call gateway.emitPersonRemovedFromGroup with correct parameters', () => {
|
|
const projectId = 'project1';
|
|
const data = { group: { id: 'group1' }, person: { id: 'person1' } };
|
|
|
|
service.emitPersonRemovedFromGroup(projectId, data);
|
|
|
|
expect(mockWebSocketsGateway.emitPersonRemovedFromGroup).toHaveBeenCalledWith(projectId, data);
|
|
});
|
|
});
|
|
|
|
describe('emitNotification', () => {
|
|
it('should call gateway.emitNotification with correct parameters', () => {
|
|
const userId = 'user1';
|
|
const data = { type: 'info', message: 'Test notification' };
|
|
|
|
service.emitNotification(userId, data);
|
|
|
|
expect(mockWebSocketsGateway.emitNotification).toHaveBeenCalledWith(userId, data);
|
|
});
|
|
});
|
|
|
|
describe('emitProjectNotification', () => {
|
|
it('should call gateway.emitProjectNotification with correct parameters', () => {
|
|
const projectId = 'project1';
|
|
const data = { type: 'info', message: 'Test project notification' };
|
|
|
|
service.emitProjectNotification(projectId, data);
|
|
|
|
expect(mockWebSocketsGateway.emitProjectNotification).toHaveBeenCalledWith(projectId, data);
|
|
});
|
|
});
|
|
}); |