Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { WorkspacesController } from "./workspaces.controller";
|
|
import { WorkspacesService } from "./workspaces.service";
|
|
import { AuthGuard } from "../auth/guards/auth.guard";
|
|
import { WorkspaceMemberRole } from "@prisma/client";
|
|
import type { AuthUser } from "@mosaic/shared";
|
|
|
|
describe("WorkspacesController", () => {
|
|
let controller: WorkspacesController;
|
|
let service: WorkspacesService;
|
|
|
|
const mockWorkspacesService = {
|
|
getUserWorkspaces: vi.fn(),
|
|
};
|
|
|
|
const mockUser: AuthUser = {
|
|
id: "user-1",
|
|
email: "test@example.com",
|
|
name: "Test User",
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [WorkspacesController],
|
|
providers: [
|
|
{
|
|
provide: WorkspacesService,
|
|
useValue: mockWorkspacesService,
|
|
},
|
|
],
|
|
})
|
|
.overrideGuard(AuthGuard)
|
|
.useValue({ canActivate: () => true })
|
|
.compile();
|
|
|
|
controller = module.get<WorkspacesController>(WorkspacesController);
|
|
service = module.get<WorkspacesService>(WorkspacesService);
|
|
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("GET /api/workspaces", () => {
|
|
it("should call service with authenticated user id", async () => {
|
|
mockWorkspacesService.getUserWorkspaces.mockResolvedValueOnce([]);
|
|
|
|
await controller.getUserWorkspaces(mockUser);
|
|
|
|
expect(service.getUserWorkspaces).toHaveBeenCalledWith("user-1");
|
|
});
|
|
|
|
it("should return workspace list from service", async () => {
|
|
const mockWorkspaces = [
|
|
{
|
|
id: "ws-1",
|
|
name: "My Workspace",
|
|
ownerId: "user-1",
|
|
role: WorkspaceMemberRole.OWNER,
|
|
createdAt: new Date("2026-01-01"),
|
|
},
|
|
];
|
|
mockWorkspacesService.getUserWorkspaces.mockResolvedValueOnce(mockWorkspaces);
|
|
|
|
const result = await controller.getUserWorkspaces(mockUser);
|
|
|
|
expect(result).toEqual(mockWorkspaces);
|
|
});
|
|
|
|
it("should propagate service errors", async () => {
|
|
mockWorkspacesService.getUserWorkspaces.mockRejectedValueOnce(new Error("Database error"));
|
|
|
|
await expect(controller.getUserWorkspaces(mockUser)).rejects.toThrow("Database error");
|
|
});
|
|
});
|
|
});
|