75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { ProjectController } from "./project.controller";
|
|
import { ProjectService } from "./project.service";
|
|
import { ProjectRepository } from "./project.repository";
|
|
|
|
describe("ProjectController", () => {
|
|
let controller: ProjectController;
|
|
|
|
const mockRepository = {
|
|
create: jest.fn(),
|
|
findAll: jest.fn(),
|
|
findOne: jest.fn(),
|
|
update: jest.fn(),
|
|
remove: jest.fn(),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [ProjectController],
|
|
providers: [ProjectService, { provide: ProjectRepository, useValue: mockRepository }],
|
|
}).compile();
|
|
|
|
controller = module.get<ProjectController>(ProjectController);
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(controller).toBeDefined();
|
|
});
|
|
|
|
describe("findAll", () => {
|
|
it("should return an array", async () => {
|
|
mockRepository.findAll.mockResolvedValue([{ id: "1", name: "Test" }]);
|
|
const result = await controller.findAll();
|
|
expect(Array.isArray(result)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("findOne", () => {
|
|
it("should return an object with the given id", async () => {
|
|
const expected = { id: "1", name: "Test" };
|
|
mockRepository.findOne.mockResolvedValue(expected);
|
|
const result = await controller.findOne("1");
|
|
expect(result).toEqual(expected);
|
|
});
|
|
});
|
|
|
|
describe("create", () => {
|
|
it("should return the created body", async () => {
|
|
const body = { name: "Test Project" };
|
|
mockRepository.create.mockResolvedValue(body);
|
|
const result = await controller.create(body);
|
|
expect(result).toEqual(body);
|
|
});
|
|
});
|
|
|
|
describe("update", () => {
|
|
it("should merge id with the update body", async () => {
|
|
const body = { name: "Updated" };
|
|
mockRepository.findOne.mockResolvedValue({ id: "1" });
|
|
mockRepository.update.mockResolvedValue({ id: "1", ...body });
|
|
const result = await controller.update("1", body);
|
|
expect(result).toEqual({ id: "1", ...body });
|
|
});
|
|
});
|
|
|
|
describe("remove", () => {
|
|
it("should return the removed id", async () => {
|
|
mockRepository.findOne.mockResolvedValue({ id: "1" });
|
|
mockRepository.remove.mockResolvedValue({ id: "1" });
|
|
const result = await controller.remove("1");
|
|
expect(result).toEqual({ id: "1" });
|
|
});
|
|
});
|
|
});
|