72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
|
import {
|
|
approveRecommendation,
|
|
generateRecommendation,
|
|
getRecommendations,
|
|
} from "../recommendations";
|
|
import { apiClient, withAuth } from "../client";
|
|
|
|
jest.mock("../client", () => ({
|
|
apiClient: {
|
|
get: jest.fn(),
|
|
post: jest.fn(),
|
|
},
|
|
withAuth: jest.fn((token?: string | null) =>
|
|
token ? { headers: { Authorization: `Bearer ${token}` } } : {},
|
|
),
|
|
}));
|
|
|
|
describe("recommendations api", () => {
|
|
const getMock = apiClient.get as any;
|
|
const postMock = apiClient.post as any;
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("returns normalized list from standardized response", async () => {
|
|
getMock.mockResolvedValue({
|
|
data: {
|
|
success: true,
|
|
data: [{ id: "rec_1", status: "pending" }],
|
|
},
|
|
});
|
|
|
|
const result = await getRecommendations("user_1", "token_1");
|
|
|
|
expect(result).toEqual([{ id: "rec_1", status: "pending" }]);
|
|
expect(withAuth).toHaveBeenCalledWith("token_1");
|
|
});
|
|
|
|
it("returns recommendation from standardized response for generate", async () => {
|
|
postMock.mockResolvedValue({
|
|
data: {
|
|
success: true,
|
|
data: { id: "rec_2", status: "pending" },
|
|
},
|
|
});
|
|
|
|
const result = await generateRecommendation({ userId: "user_1" }, null);
|
|
|
|
expect(result).toEqual({ id: "rec_2", status: "pending" });
|
|
});
|
|
|
|
it("sends approval payload without approvedBy", async () => {
|
|
postMock.mockResolvedValue({
|
|
data: {
|
|
success: true,
|
|
data: { id: "rec_3", status: "approved" },
|
|
},
|
|
});
|
|
|
|
const result = await approveRecommendation("rec_3", "token_3");
|
|
|
|
expect(result).toEqual({ id: "rec_3", status: "approved" });
|
|
expect(apiClient.post).toHaveBeenCalledWith(
|
|
"/api/recommendations/approve",
|
|
{ recommendationId: "rec_3", status: "approved" },
|
|
expect.any(Object),
|
|
);
|
|
});
|
|
});
|