Files
stack/apps/api/src/events/events.controller.spec.ts
Jason Woltje 132fe6ba98 feat(#5): Implement CRUD APIs for tasks, events, and projects
Implements comprehensive CRUD APIs following TDD principles with 92.44%
test coverage (exceeds 85% requirement).

Features:
- Tasks API: Full CRUD with filtering, pagination, and subtask support
- Events API: Full CRUD with recurrence support and date filtering
- Projects API: Full CRUD with task/event association
- Authentication guards on all endpoints
- Workspace-scoped queries for multi-tenant isolation
- Activity logging for all operations (CREATED, UPDATED, DELETED, etc.)
- DTOs with class-validator validation
- Comprehensive test suite (221 tests, 44 for new APIs)

Implementation:
- Services: Business logic with Prisma ORM integration
- Controllers: RESTful endpoints with AuthGuard
- Modules: Properly registered in AppModule
- Documentation: Complete API reference in docs/4-api/4-crud-endpoints/

Test Coverage:
- Tasks: 96.1%
- Events: 89.83%
- Projects: 84.21%
- Overall: 92.44%

TDD Workflow:
1. RED: Wrote failing tests first
2. GREEN: Implemented minimal code to pass tests
3. REFACTOR: Improved code quality while maintaining coverage

Refs #5

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-28 18:43:12 -06:00

166 lines
4.2 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from "vitest";
import { Test, TestingModule } from "@nestjs/testing";
import { EventsController } from "./events.controller";
import { EventsService } from "./events.service";
import { AuthGuard } from "../auth/guards/auth.guard";
import { ExecutionContext } from "@nestjs/common";
describe("EventsController", () => {
let controller: EventsController;
let service: EventsService;
const mockEventsService = {
create: vi.fn(),
findAll: vi.fn(),
findOne: vi.fn(),
update: vi.fn(),
remove: vi.fn(),
};
const mockAuthGuard = {
canActivate: vi.fn((context: ExecutionContext) => {
const request = context.switchToHttp().getRequest();
request.user = {
id: "550e8400-e29b-41d4-a716-446655440002",
workspaceId: "550e8400-e29b-41d4-a716-446655440001",
};
return true;
}),
};
const mockWorkspaceId = "550e8400-e29b-41d4-a716-446655440001";
const mockUserId = "550e8400-e29b-41d4-a716-446655440002";
const mockEventId = "550e8400-e29b-41d4-a716-446655440003";
const mockRequest = {
user: {
id: mockUserId,
workspaceId: mockWorkspaceId,
},
};
const mockEvent = {
id: mockEventId,
workspaceId: mockWorkspaceId,
title: "Test Event",
description: "Test Description",
startTime: new Date("2026-02-01T10:00:00Z"),
endTime: new Date("2026-02-01T11:00:00Z"),
allDay: false,
location: "Conference Room A",
recurrence: null,
creatorId: mockUserId,
projectId: null,
metadata: {},
createdAt: new Date(),
updatedAt: new Date(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [EventsController],
providers: [
{
provide: EventsService,
useValue: mockEventsService,
},
],
})
.overrideGuard(AuthGuard)
.useValue(mockAuthGuard)
.compile();
controller = module.get<EventsController>(EventsController);
service = module.get<EventsService>(EventsService);
vi.clearAllMocks();
});
it("should be defined", () => {
expect(controller).toBeDefined();
});
describe("create", () => {
it("should create an event", async () => {
const createDto = {
title: "New Event",
startTime: new Date("2026-02-01T10:00:00Z"),
};
mockEventsService.create.mockResolvedValue(mockEvent);
const result = await controller.create(createDto, mockRequest);
expect(result).toEqual(mockEvent);
expect(service.create).toHaveBeenCalledWith(
mockWorkspaceId,
mockUserId,
createDto
);
});
});
describe("findAll", () => {
it("should return paginated events", async () => {
const query = {
workspaceId: mockWorkspaceId,
};
const paginatedResult = {
data: [mockEvent],
meta: {
total: 1,
page: 1,
limit: 50,
totalPages: 1,
},
};
mockEventsService.findAll.mockResolvedValue(paginatedResult);
const result = await controller.findAll(query, mockRequest);
expect(result).toEqual(paginatedResult);
});
});
describe("findOne", () => {
it("should return an event by id", async () => {
mockEventsService.findOne.mockResolvedValue(mockEvent);
const result = await controller.findOne(mockEventId, mockRequest);
expect(result).toEqual(mockEvent);
});
});
describe("update", () => {
it("should update an event", async () => {
const updateDto = {
title: "Updated Event",
};
const updatedEvent = { ...mockEvent, ...updateDto };
mockEventsService.update.mockResolvedValue(updatedEvent);
const result = await controller.update(mockEventId, updateDto, mockRequest);
expect(result).toEqual(updatedEvent);
});
});
describe("remove", () => {
it("should delete an event", async () => {
mockEventsService.remove.mockResolvedValue(undefined);
await controller.remove(mockEventId, mockRequest);
expect(service.remove).toHaveBeenCalledWith(
mockEventId,
mockWorkspaceId,
mockUserId
);
});
});
});