- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
108 lines
3.3 KiB
TypeScript
108 lines
3.3 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { UsersController } from "./users.controller";
|
|
import { UsersService } from "./users.service";
|
|
|
|
describe("UsersController", () => {
|
|
let controller: UsersController;
|
|
let service: UsersService;
|
|
|
|
const mockUsersService = {
|
|
findById: jest.fn().mockResolvedValue({ id: "user-id", firstName: "Test" }),
|
|
update: jest
|
|
.fn()
|
|
.mockResolvedValue({ id: "user-id", firstName: "Updated" }),
|
|
addAddress: jest.fn().mockResolvedValue({ id: "addr-id", title: "Home" }),
|
|
updateAddress: jest
|
|
.fn()
|
|
.mockResolvedValue({ id: "addr-id", title: "Work" }),
|
|
deleteAddress: jest.fn().mockResolvedValue({ success: true }),
|
|
setDefaultAddress: jest.fn().mockResolvedValue({ success: true }),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [UsersController],
|
|
providers: [{ provide: UsersService, useValue: mockUsersService }],
|
|
}).compile();
|
|
|
|
controller = module.get<UsersController>(UsersController);
|
|
service = module.get<UsersService>(UsersService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(controller).toBeDefined();
|
|
});
|
|
|
|
it("should getProfile", async () => {
|
|
const req = { user: { id: "user-id" } };
|
|
const result = await controller.getProfile(req);
|
|
expect(service.findById).toHaveBeenCalledWith("user-id");
|
|
expect(result?.id).toBe("user-id");
|
|
});
|
|
|
|
it("should updateProfile", async () => {
|
|
const req = { user: { id: "user-id" } };
|
|
const dto = { firstName: "Updated" };
|
|
const result = await controller.updateProfile(req, dto);
|
|
expect(service.update).toHaveBeenCalledWith("user-id", dto);
|
|
expect(result.firstName).toBe("Updated");
|
|
});
|
|
|
|
it("should addAddress", async () => {
|
|
const req = { user: { id: "user-id" } };
|
|
const dto = {
|
|
title: "Home",
|
|
receptorName: "Ali",
|
|
phone: "0912",
|
|
province: "Teh",
|
|
city: "Teh",
|
|
detail: "Det",
|
|
zipCode: "123",
|
|
};
|
|
const result = await controller.addAddress(req, dto);
|
|
expect(service.addAddress).toHaveBeenCalledWith("user-id", dto);
|
|
expect(result.id).toBe("addr-id");
|
|
});
|
|
|
|
it("should updateAddress", async () => {
|
|
const req = { user: { id: "user-id" } };
|
|
const dto = {
|
|
title: "Work",
|
|
receptorName: "Ali",
|
|
phone: "0912",
|
|
province: "Teh",
|
|
city: "Teh",
|
|
detail: "Det",
|
|
zipCode: "123",
|
|
};
|
|
const result = await controller.updateAddress(req, "addr-id", dto);
|
|
expect(service.updateAddress).toHaveBeenCalledWith(
|
|
"user-id",
|
|
"addr-id",
|
|
dto,
|
|
);
|
|
expect(result.title).toBe("Work");
|
|
});
|
|
|
|
it("should deleteAddress", async () => {
|
|
const req = { user: { id: "user-id" } };
|
|
const result = await controller.deleteAddress(req, "addr-id");
|
|
expect(service.deleteAddress).toHaveBeenCalledWith("user-id", "addr-id");
|
|
expect(result).toBeDefined();
|
|
});
|
|
|
|
it("should setDefaultAddress", async () => {
|
|
const req = { user: { id: "user-id" } };
|
|
const result = await controller.setDefaultAddress(req, "addr-id");
|
|
expect(service.setDefaultAddress).toHaveBeenCalledWith(
|
|
"user-id",
|
|
"addr-id",
|
|
);
|
|
expect(result).toBeDefined();
|
|
});
|
|
});
|