feat(#15): implement Gantt chart component
- Create GanttChart component with timeline visualization - Add task bars with status-based color coding - Implement PDA-friendly language (Target passed vs OVERDUE) - Support task click interactions - Comprehensive test coverage (96.18%) - 33 tests passing (22 component + 11 helper tests) - Fully accessible with ARIA labels and keyboard navigation - Demo page at /demo/gantt - Responsive design with customizable height Technical details: - Uses Next.js 16 + React 19 + TypeScript - Strict typing (NO any types) - Helper functions to convert Task to GanttTask - Timeline calculation with automatic range detection - Status indicators: completed, in-progress, paused, not-started Refs #15
This commit is contained in:
220
apps/api/src/domains/domains.controller.spec.ts
Normal file
220
apps/api/src/domains/domains.controller.spec.ts
Normal file
@@ -0,0 +1,220 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { DomainsController } from "./domains.controller";
|
||||
import { DomainsService } from "./domains.service";
|
||||
import { CreateDomainDto, UpdateDomainDto, QueryDomainsDto } from "./dto";
|
||||
import { AuthGuard } from "../auth/guards/auth.guard";
|
||||
import { WorkspaceGuard, PermissionGuard } from "../common/guards";
|
||||
import { ExecutionContext } from "@nestjs/common";
|
||||
|
||||
describe("DomainsController", () => {
|
||||
let controller: DomainsController;
|
||||
let service: DomainsService;
|
||||
|
||||
const mockDomainsService = {
|
||||
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 mockWorkspaceGuard = {
|
||||
canActivate: vi.fn(() => true),
|
||||
};
|
||||
|
||||
const mockPermissionGuard = {
|
||||
canActivate: vi.fn(() => true),
|
||||
};
|
||||
|
||||
const mockWorkspaceId = "550e8400-e29b-41d4-a716-446655440001";
|
||||
const mockUserId = "550e8400-e29b-41d4-a716-446655440002";
|
||||
const mockDomainId = "550e8400-e29b-41d4-a716-446655440003";
|
||||
|
||||
const mockDomain = {
|
||||
id: mockDomainId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
name: "Work",
|
||||
slug: "work",
|
||||
description: "Work-related tasks and projects",
|
||||
color: "#3B82F6",
|
||||
icon: "briefcase",
|
||||
sortOrder: 0,
|
||||
metadata: {},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const mockUser = {
|
||||
id: mockUserId,
|
||||
email: "test@example.com",
|
||||
name: "Test User",
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [DomainsController],
|
||||
providers: [
|
||||
{
|
||||
provide: DomainsService,
|
||||
useValue: mockDomainsService,
|
||||
},
|
||||
],
|
||||
})
|
||||
.overrideGuard(AuthGuard)
|
||||
.useValue(mockAuthGuard)
|
||||
.overrideGuard(WorkspaceGuard)
|
||||
.useValue(mockWorkspaceGuard)
|
||||
.overrideGuard(PermissionGuard)
|
||||
.useValue(mockPermissionGuard)
|
||||
.compile();
|
||||
|
||||
controller = module.get<DomainsController>(DomainsController);
|
||||
service = module.get<DomainsService>(DomainsService);
|
||||
|
||||
// Clear all mocks before each test
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a domain", async () => {
|
||||
const createDto: CreateDomainDto = {
|
||||
name: "Work",
|
||||
slug: "work",
|
||||
description: "Work-related tasks",
|
||||
color: "#3B82F6",
|
||||
icon: "briefcase",
|
||||
};
|
||||
|
||||
mockDomainsService.create.mockResolvedValue(mockDomain);
|
||||
|
||||
const result = await controller.create(
|
||||
createDto,
|
||||
mockWorkspaceId,
|
||||
mockUser
|
||||
);
|
||||
|
||||
expect(result).toEqual(mockDomain);
|
||||
expect(service.create).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
createDto
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return paginated domains", async () => {
|
||||
const query: QueryDomainsDto = { page: 1, limit: 10 };
|
||||
const paginatedResult = {
|
||||
data: [mockDomain],
|
||||
meta: {
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 10,
|
||||
totalPages: 1,
|
||||
},
|
||||
};
|
||||
|
||||
mockDomainsService.findAll.mockResolvedValue(paginatedResult);
|
||||
|
||||
const result = await controller.findAll(query, mockWorkspaceId);
|
||||
|
||||
expect(result).toEqual(paginatedResult);
|
||||
expect(service.findAll).toHaveBeenCalledWith({
|
||||
...query,
|
||||
workspaceId: mockWorkspaceId,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle search query", async () => {
|
||||
const query: QueryDomainsDto = {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
search: "work",
|
||||
};
|
||||
|
||||
mockDomainsService.findAll.mockResolvedValue({
|
||||
data: [mockDomain],
|
||||
meta: { total: 1, page: 1, limit: 10, totalPages: 1 },
|
||||
});
|
||||
|
||||
await controller.findAll(query, mockWorkspaceId);
|
||||
|
||||
expect(service.findAll).toHaveBeenCalledWith({
|
||||
...query,
|
||||
workspaceId: mockWorkspaceId,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should return a domain by ID", async () => {
|
||||
mockDomainsService.findOne.mockResolvedValue(mockDomain);
|
||||
|
||||
const result = await controller.findOne(mockDomainId, mockWorkspaceId);
|
||||
|
||||
expect(result).toEqual(mockDomain);
|
||||
expect(service.findOne).toHaveBeenCalledWith(
|
||||
mockDomainId,
|
||||
mockWorkspaceId
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update a domain", async () => {
|
||||
const updateDto: UpdateDomainDto = {
|
||||
name: "Updated Work",
|
||||
color: "#10B981",
|
||||
};
|
||||
|
||||
const updatedDomain = { ...mockDomain, ...updateDto };
|
||||
mockDomainsService.update.mockResolvedValue(updatedDomain);
|
||||
|
||||
const result = await controller.update(
|
||||
mockDomainId,
|
||||
updateDto,
|
||||
mockWorkspaceId,
|
||||
mockUser
|
||||
);
|
||||
|
||||
expect(result).toEqual(updatedDomain);
|
||||
expect(service.update).toHaveBeenCalledWith(
|
||||
mockDomainId,
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
updateDto
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete a domain", async () => {
|
||||
mockDomainsService.remove.mockResolvedValue(undefined);
|
||||
|
||||
await controller.remove(mockDomainId, mockWorkspaceId, mockUser);
|
||||
|
||||
expect(service.remove).toHaveBeenCalledWith(
|
||||
mockDomainId,
|
||||
mockWorkspaceId,
|
||||
mockUserId
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
393
apps/api/src/domains/domains.service.spec.ts
Normal file
393
apps/api/src/domains/domains.service.spec.ts
Normal file
@@ -0,0 +1,393 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { DomainsService } from "./domains.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ActivityService } from "../activity/activity.service";
|
||||
import { NotFoundException, ConflictException } from "@nestjs/common";
|
||||
|
||||
describe("DomainsService", () => {
|
||||
let service: DomainsService;
|
||||
let prisma: PrismaService;
|
||||
let activityService: ActivityService;
|
||||
|
||||
const mockPrismaService = {
|
||||
domain: {
|
||||
create: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
count: vi.fn(),
|
||||
findUnique: vi.fn(),
|
||||
findFirst: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const mockActivityService = {
|
||||
logDomainCreated: vi.fn(),
|
||||
logDomainUpdated: vi.fn(),
|
||||
logDomainDeleted: vi.fn(),
|
||||
};
|
||||
|
||||
const mockWorkspaceId = "550e8400-e29b-41d4-a716-446655440001";
|
||||
const mockUserId = "550e8400-e29b-41d4-a716-446655440002";
|
||||
const mockDomainId = "550e8400-e29b-41d4-a716-446655440003";
|
||||
|
||||
const mockDomain = {
|
||||
id: mockDomainId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
name: "Work",
|
||||
slug: "work",
|
||||
description: "Work-related tasks and projects",
|
||||
color: "#3B82F6",
|
||||
icon: "briefcase",
|
||||
sortOrder: 0,
|
||||
metadata: {},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
DomainsService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrismaService,
|
||||
},
|
||||
{
|
||||
provide: ActivityService,
|
||||
useValue: mockActivityService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<DomainsService>(DomainsService);
|
||||
prisma = module.get<PrismaService>(PrismaService);
|
||||
activityService = module.get<ActivityService>(ActivityService);
|
||||
|
||||
// Clear all mocks before each test
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a domain and log activity", async () => {
|
||||
const createDto = {
|
||||
name: "Work",
|
||||
slug: "work",
|
||||
description: "Work-related tasks",
|
||||
color: "#3B82F6",
|
||||
icon: "briefcase",
|
||||
};
|
||||
|
||||
mockPrismaService.domain.findFirst.mockResolvedValue(null);
|
||||
mockPrismaService.domain.create.mockResolvedValue(mockDomain);
|
||||
mockActivityService.logDomainCreated.mockResolvedValue({});
|
||||
|
||||
const result = await service.create(mockWorkspaceId, mockUserId, createDto);
|
||||
|
||||
expect(result).toEqual(mockDomain);
|
||||
expect(prisma.domain.findFirst).toHaveBeenCalledWith({
|
||||
where: {
|
||||
workspaceId: mockWorkspaceId,
|
||||
slug: createDto.slug,
|
||||
},
|
||||
});
|
||||
expect(prisma.domain.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
...createDto,
|
||||
workspace: {
|
||||
connect: { id: mockWorkspaceId },
|
||||
},
|
||||
sortOrder: 0,
|
||||
metadata: {},
|
||||
},
|
||||
});
|
||||
expect(activityService.logDomainCreated).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
mockDomainId,
|
||||
{ name: mockDomain.name }
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw ConflictException if slug already exists", async () => {
|
||||
const createDto = {
|
||||
name: "Work",
|
||||
slug: "work",
|
||||
};
|
||||
|
||||
mockPrismaService.domain.findFirst.mockResolvedValue(mockDomain);
|
||||
|
||||
await expect(
|
||||
service.create(mockWorkspaceId, mockUserId, createDto)
|
||||
).rejects.toThrow(ConflictException);
|
||||
expect(prisma.domain.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should use default values for optional fields", async () => {
|
||||
const createDto = {
|
||||
name: "Work",
|
||||
slug: "work",
|
||||
};
|
||||
|
||||
mockPrismaService.domain.findFirst.mockResolvedValue(null);
|
||||
mockPrismaService.domain.create.mockResolvedValue(mockDomain);
|
||||
mockActivityService.logDomainCreated.mockResolvedValue({});
|
||||
|
||||
await service.create(mockWorkspaceId, mockUserId, createDto);
|
||||
|
||||
expect(prisma.domain.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
name: "Work",
|
||||
slug: "work",
|
||||
workspace: {
|
||||
connect: { id: mockWorkspaceId },
|
||||
},
|
||||
sortOrder: 0,
|
||||
metadata: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return paginated domains", async () => {
|
||||
const query = { workspaceId: mockWorkspaceId, page: 1, limit: 10 };
|
||||
const mockDomains = [mockDomain];
|
||||
|
||||
mockPrismaService.domain.findMany.mockResolvedValue(mockDomains);
|
||||
mockPrismaService.domain.count.mockResolvedValue(1);
|
||||
|
||||
const result = await service.findAll(query);
|
||||
|
||||
expect(result).toEqual({
|
||||
data: mockDomains,
|
||||
meta: {
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 10,
|
||||
totalPages: 1,
|
||||
},
|
||||
});
|
||||
expect(prisma.domain.findMany).toHaveBeenCalledWith({
|
||||
where: { workspaceId: mockWorkspaceId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
skip: 0,
|
||||
take: 10,
|
||||
});
|
||||
expect(prisma.domain.count).toHaveBeenCalledWith({
|
||||
where: { workspaceId: mockWorkspaceId },
|
||||
});
|
||||
});
|
||||
|
||||
it("should filter by search term", async () => {
|
||||
const query = {
|
||||
workspaceId: mockWorkspaceId,
|
||||
page: 1,
|
||||
limit: 10,
|
||||
search: "work",
|
||||
};
|
||||
|
||||
mockPrismaService.domain.findMany.mockResolvedValue([mockDomain]);
|
||||
mockPrismaService.domain.count.mockResolvedValue(1);
|
||||
|
||||
await service.findAll(query);
|
||||
|
||||
expect(prisma.domain.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
workspaceId: mockWorkspaceId,
|
||||
OR: [
|
||||
{ name: { contains: "work", mode: "insensitive" } },
|
||||
{ description: { contains: "work", mode: "insensitive" } },
|
||||
],
|
||||
},
|
||||
orderBy: { sortOrder: "asc" },
|
||||
skip: 0,
|
||||
take: 10,
|
||||
});
|
||||
});
|
||||
|
||||
it("should use default pagination values", async () => {
|
||||
const query = { workspaceId: mockWorkspaceId };
|
||||
|
||||
mockPrismaService.domain.findMany.mockResolvedValue([]);
|
||||
mockPrismaService.domain.count.mockResolvedValue(0);
|
||||
|
||||
await service.findAll(query);
|
||||
|
||||
expect(prisma.domain.findMany).toHaveBeenCalledWith({
|
||||
where: { workspaceId: mockWorkspaceId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
skip: 0,
|
||||
take: 50,
|
||||
});
|
||||
});
|
||||
|
||||
it("should calculate pagination correctly", async () => {
|
||||
const query = { workspaceId: mockWorkspaceId, page: 3, limit: 20 };
|
||||
|
||||
mockPrismaService.domain.findMany.mockResolvedValue([]);
|
||||
mockPrismaService.domain.count.mockResolvedValue(55);
|
||||
|
||||
const result = await service.findAll(query);
|
||||
|
||||
expect(result.meta).toEqual({
|
||||
total: 55,
|
||||
page: 3,
|
||||
limit: 20,
|
||||
totalPages: 3,
|
||||
});
|
||||
expect(prisma.domain.findMany).toHaveBeenCalledWith({
|
||||
where: { workspaceId: mockWorkspaceId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
skip: 40, // (3 - 1) * 20
|
||||
take: 20,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should return a domain by ID", async () => {
|
||||
mockPrismaService.domain.findUnique.mockResolvedValue(mockDomain);
|
||||
|
||||
const result = await service.findOne(mockDomainId, mockWorkspaceId);
|
||||
|
||||
expect(result).toEqual(mockDomain);
|
||||
expect(prisma.domain.findUnique).toHaveBeenCalledWith({
|
||||
where: {
|
||||
id: mockDomainId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
tasks: true,
|
||||
projects: true,
|
||||
events: true,
|
||||
ideas: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if domain not found", async () => {
|
||||
mockPrismaService.domain.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.findOne(mockDomainId, mockWorkspaceId)
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update a domain and log activity", async () => {
|
||||
const updateDto = {
|
||||
name: "Updated Work",
|
||||
color: "#10B981",
|
||||
};
|
||||
|
||||
const updatedDomain = { ...mockDomain, ...updateDto };
|
||||
|
||||
mockPrismaService.domain.findUnique.mockResolvedValue(mockDomain);
|
||||
mockPrismaService.domain.findFirst.mockResolvedValue(null);
|
||||
mockPrismaService.domain.update.mockResolvedValue(updatedDomain);
|
||||
mockActivityService.logDomainUpdated.mockResolvedValue({});
|
||||
|
||||
const result = await service.update(
|
||||
mockDomainId,
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
updateDto
|
||||
);
|
||||
|
||||
expect(result).toEqual(updatedDomain);
|
||||
expect(prisma.domain.update).toHaveBeenCalledWith({
|
||||
where: {
|
||||
id: mockDomainId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
},
|
||||
data: updateDto,
|
||||
});
|
||||
expect(activityService.logDomainUpdated).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
mockDomainId,
|
||||
{ changes: updateDto }
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if domain not found", async () => {
|
||||
const updateDto = { name: "Updated Work" };
|
||||
|
||||
mockPrismaService.domain.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.update(mockDomainId, mockWorkspaceId, mockUserId, updateDto)
|
||||
).rejects.toThrow(NotFoundException);
|
||||
expect(prisma.domain.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should throw ConflictException if slug already exists for another domain", async () => {
|
||||
const updateDto = { slug: "existing-slug" };
|
||||
const anotherDomain = { ...mockDomain, id: "another-id", slug: "existing-slug" };
|
||||
|
||||
mockPrismaService.domain.findUnique.mockResolvedValue(mockDomain);
|
||||
mockPrismaService.domain.findFirst.mockResolvedValue(anotherDomain);
|
||||
|
||||
await expect(
|
||||
service.update(mockDomainId, mockWorkspaceId, mockUserId, updateDto)
|
||||
).rejects.toThrow(ConflictException);
|
||||
expect(prisma.domain.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should allow updating to the same slug", async () => {
|
||||
const updateDto = { slug: "work", name: "Updated Work" };
|
||||
|
||||
mockPrismaService.domain.findUnique.mockResolvedValue(mockDomain);
|
||||
mockPrismaService.domain.findFirst.mockResolvedValue(mockDomain);
|
||||
mockPrismaService.domain.update.mockResolvedValue({ ...mockDomain, ...updateDto });
|
||||
mockActivityService.logDomainUpdated.mockResolvedValue({});
|
||||
|
||||
await service.update(mockDomainId, mockWorkspaceId, mockUserId, updateDto);
|
||||
|
||||
expect(prisma.domain.update).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete a domain and log activity", async () => {
|
||||
mockPrismaService.domain.findUnique.mockResolvedValue(mockDomain);
|
||||
mockPrismaService.domain.delete.mockResolvedValue(mockDomain);
|
||||
mockActivityService.logDomainDeleted.mockResolvedValue({});
|
||||
|
||||
await service.remove(mockDomainId, mockWorkspaceId, mockUserId);
|
||||
|
||||
expect(prisma.domain.delete).toHaveBeenCalledWith({
|
||||
where: {
|
||||
id: mockDomainId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
},
|
||||
});
|
||||
expect(activityService.logDomainDeleted).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
mockDomainId,
|
||||
{ name: mockDomain.name }
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if domain not found", async () => {
|
||||
mockPrismaService.domain.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.remove(mockDomainId, mockWorkspaceId, mockUserId)
|
||||
).rejects.toThrow(NotFoundException);
|
||||
expect(prisma.domain.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user