This repository has been archived on 2025-12-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
tasker/tasker-server/tests/getUserTasks.test.ts
Andrew Trieu e293ab3c2c Config ci cd (#5)
* feat: Add GitHub Actions workflow for Amplify deployment and configuration

* feat: Update Amplify deployment workflow and add Serverless deployment workflow

* feat: Set environment variables for Amplify monorepo deployment

* feat: Refactor Amplify deployment configuration for monorepo structure

* feat: Update environment variable naming and refactor image URL handling across the application

* feat: Add Jest configuration and tests for project and user handlers
2024-11-27 11:33:41 +02:00

121 lines
3.0 KiB
TypeScript

const mockQueryTasks = jest.fn();
import { handler } from "../src/handlers/getUserTasks";
import { queryTasks } from "@/lib/util";
jest.mock("@/lib/util", () => ({
queryTasks: mockQueryTasks,
}));
describe("getUserTasks handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve tasks authored and assigned to the user and return a 200 response", async () => {
const mockAuthorTasks = [
{ taskId: "task_1", title: "Authored Task 1" },
{ taskId: "task_2", title: "Authored Task 2" },
];
const mockAssigneeTasks = [
{ taskId: "task_2", title: "Authored Task 2" }, // Duplicate task
{ taskId: "task_3", title: "Assigned Task 3" },
];
const mockUniqueTasks = [
{ taskId: "task_1", title: "Authored Task 1" },
{ taskId: "task_2", title: "Authored Task 2" },
{ taskId: "task_3", title: "Assigned Task 3" },
];
mockQueryTasks
.mockResolvedValueOnce(mockAuthorTasks)
.mockResolvedValueOnce(mockAssigneeTasks);
const event = {
pathParameters: { userId: "user_123" },
};
const response = await handler(event);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-author-user-id",
"authorUserId"
);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-assigned-user-id",
"assignedUserId"
);
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockUniqueTasks),
});
});
it("should return a 500 response if queryTasks fails", async () => {
const mockError = new Error("Query failed");
mockQueryTasks.mockRejectedValueOnce(mockError);
const event = {
pathParameters: { userId: "user_123" },
};
const response = await handler(event);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-author-user-id",
"authorUserId"
);
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving tasks for user: ${mockError.message}`,
}),
});
});
it("should handle empty author and assignee tasks gracefully", async () => {
mockQueryTasks.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const event = {
pathParameters: { userId: "user_123" },
};
const response = await handler(event);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-author-user-id",
"authorUserId"
);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-assigned-user-id",
"assignedUserId"
);
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify([]),
});
});
});