83 lines
2.4 KiB
TypeScript
83 lines
2.4 KiB
TypeScript
jest.mock("uuid", () => ({
|
|
v4: jest.fn(() => "mocked-uuid"),
|
|
}));
|
|
|
|
jest.mock("@noble/post-quantum/ml-kem.js", () => ({
|
|
ml_kem768: {
|
|
keygen: jest.fn(),
|
|
encapsulate: jest.fn(),
|
|
decapsulate: jest.fn(),
|
|
},
|
|
}));
|
|
|
|
jest.mock("jose", () => ({
|
|
SignJWT: jest.fn().mockReturnValue({
|
|
setProtectedHeader: jest.fn().mockReturnThis(),
|
|
setIssuedAt: jest.fn().mockReturnThis(),
|
|
setExpirationTime: jest.fn().mockReturnThis(),
|
|
sign: jest.fn().mockResolvedValue("mocked-jwt"),
|
|
}),
|
|
jwtVerify: jest.fn(),
|
|
}));
|
|
|
|
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { AuthGuard } from "../auth/guards/auth.guard";
|
|
import { AuthenticatedRequest } from "../common/interfaces/request.interface";
|
|
import { FavoritesController } from "./favorites.controller";
|
|
import { FavoritesService } from "./favorites.service";
|
|
|
|
describe("FavoritesController", () => {
|
|
let controller: FavoritesController;
|
|
let service: FavoritesService;
|
|
|
|
const mockFavoritesService = {
|
|
addFavorite: jest.fn(),
|
|
removeFavorite: jest.fn(),
|
|
getUserFavorites: jest.fn(),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [FavoritesController],
|
|
providers: [{ provide: FavoritesService, useValue: mockFavoritesService }],
|
|
})
|
|
.overrideGuard(AuthGuard)
|
|
.useValue({ canActivate: () => true })
|
|
.compile();
|
|
|
|
controller = module.get<FavoritesController>(FavoritesController);
|
|
service = module.get<FavoritesService>(FavoritesService);
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(controller).toBeDefined();
|
|
});
|
|
|
|
describe("add", () => {
|
|
it("should call service.addFavorite", async () => {
|
|
const req = { user: { sub: "user-uuid" } } as AuthenticatedRequest;
|
|
await controller.add(req, "content-1");
|
|
expect(service.addFavorite).toHaveBeenCalledWith("user-uuid", "content-1");
|
|
});
|
|
});
|
|
|
|
describe("remove", () => {
|
|
it("should call service.removeFavorite", async () => {
|
|
const req = { user: { sub: "user-uuid" } } as AuthenticatedRequest;
|
|
await controller.remove(req, "content-1");
|
|
expect(service.removeFavorite).toHaveBeenCalledWith(
|
|
"user-uuid",
|
|
"content-1",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("list", () => {
|
|
it("should call service.getUserFavorites", async () => {
|
|
const req = { user: { sub: "user-uuid" } } as AuthenticatedRequest;
|
|
await controller.list(req, 10, 0);
|
|
expect(service.getUserFavorites).toHaveBeenCalledWith("user-uuid", 10, 0);
|
|
});
|
|
});
|
|
});
|