feat: Add Jest configuration and tests for project and user handlers

This commit is contained in:
2024-11-26 11:34:42 +02:00
parent b7771ac827
commit 646ef129d4
17 changed files with 4575 additions and 4 deletions

View File

@@ -0,0 +1,13 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"setupFiles": ["<rootDir>/jest.env.js"],
"moduleFileExtensions": ["ts", "js"],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testMatch": ["**/tests/**/*.test.ts"],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
}
}

View File

@@ -0,0 +1,6 @@
process.env.SLS_REGION = "mock-region";
process.env.TASKER_TASK_TABLE_NAME = "mock-task-table";
process.env.TASKER_PROJECT_TABLE_NAME = "mock-project-table";
process.env.TASKER_USER_TABLE_NAME = "mock-user-table";
process.env.TASKER_TASK_EXTRA_TABLE_NAME = "mock-task-extra-table";
process.env.TASKER_TEAM_TABLE_NAME = "mock-team-table";

File diff suppressed because it is too large Load Diff

View File

@@ -9,7 +9,9 @@
"infra:destroy": "cd terraform && AWS_PROFILE=default terraform destroy",
"sls:package": "AWS_PROFILE=default sls package",
"sls:deploy": "AWS_PROFILE=default sls deploy",
"sls:remove": "AWS_PROFILE=default sls remove"
"sls:remove": "AWS_PROFILE=default sls remove",
"test": "jes --coverage",
"test:ci": "jest --no-coverage --ci"
},
"author": "",
"license": "ISC",
@@ -22,10 +24,13 @@
"uuid": "^11.0.3"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.9.1",
"eslint": "^9.15.0",
"jest": "^29.7.0",
"prettier": "^3.3.3",
"serverless-prune-plugin": "^2.1.0",
"ts-jest": "^29.2.5",
"typescript": "^5.6.3"
}
}

View File

@@ -10,7 +10,6 @@ provider:
runtime: nodejs20.x
environment:
SLS_REGION: ${self:provider.region}
API_BASE_URL: ${ssm:/tasker/api/base-url}
TASKER_TASK_TABLE_NAME: ${ssm:/tasker/dynamodb/task-table-name}
TASKER_PROJECT_TABLE_NAME: ${ssm:/tasker/dynamodb/project-table-name}
TASKER_USER_TABLE_NAME: ${ssm:/tasker/dynamodb/user-table-name}

View File

@@ -0,0 +1,94 @@
const mockPut = jest.fn();
import { handler } from "../src/handlers/createProject";
import { v4 as uuidv4 } from "uuid";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
put: mockPut,
})),
},
}));
jest.mock("uuid", () => ({
v4: jest.fn(),
}));
describe("handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should create a new project and return 201 response", async () => {
const mockUUID = "mock-uuid";
(uuidv4 as jest.Mock).mockReturnValue(mockUUID);
const event = {
body: JSON.stringify({
name: "Test Project",
description: "This is a test project.",
startDate: "2024-01-01",
endDate: "2024-12-31",
}),
};
mockPut.mockResolvedValue({});
const response = await handler(event);
expect(mockPut).toHaveBeenCalledWith({
TableName: "mock-project-table",
Item: {
category: "projects",
projectId: `project_${mockUUID}`,
name: "Test Project",
description: "This is a test project.",
startDate: "2024-01-01",
endDate: "2024-12-31",
},
});
expect(response).toEqual({
statusCode: 201,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
category: "projects",
projectId: `project_${mockUUID}`,
name: "Test Project",
description: "This is a test project.",
startDate: "2024-01-01",
endDate: "2024-12-31",
}),
});
});
it("should return 500 response on error", async () => {
const event = {
body: JSON.stringify({
name: "Test Project",
description: "This is a test project.",
startDate: "2024-01-01",
endDate: "2024-12-31",
}),
};
const mockError = new Error("DynamoDB error");
mockPut.mockRejectedValue(mockError);
const response = await handler(event);
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error creating project: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,122 @@
const mockPut = jest.fn();
import { handler } from "../src/handlers/createTask";
import { v4 as uuidv4 } from "uuid";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
put: mockPut,
})),
},
}));
jest.mock("uuid", () => ({
v4: jest.fn(),
}));
describe("createTask handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should create a new task and return 201 response", async () => {
const mockUUID = "mock-uuid";
(uuidv4 as jest.Mock).mockReturnValue(mockUUID);
const event = {
body: JSON.stringify({
title: "Test Task",
description: "This is a test task.",
status: "In Progress",
priority: "High",
tags: ["test", "jest"],
startDate: "2024-01-01",
dueDate: "2024-01-15",
points: 5,
projectId: "project_12345",
authorUserId: "user_abc",
assignedUserId: "user_xyz",
}),
};
mockPut.mockResolvedValue({});
const response = await handler(event);
expect(mockPut).toHaveBeenCalledWith({
TableName: "mock-task-table",
Item: {
category: "tasks",
taskId: `task_${mockUUID}`,
title: "Test Task",
description: "This is a test task.",
status: "In Progress",
priority: "High",
tags: ["test", "jest"],
startDate: "2024-01-01",
dueDate: "2024-01-15",
points: 5,
projectId: "project_12345",
authorUserId: "user_abc",
assignedUserId: "user_xyz",
},
});
expect(response).toEqual({
statusCode: 201,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
category: "tasks",
taskId: `task_${mockUUID}`,
title: "Test Task",
description: "This is a test task.",
status: "In Progress",
priority: "High",
tags: ["test", "jest"],
startDate: "2024-01-01",
dueDate: "2024-01-15",
points: 5,
projectId: "project_12345",
authorUserId: "user_abc",
assignedUserId: "user_xyz",
}),
});
});
it("should return 500 response on error", async () => {
const event = {
body: JSON.stringify({
title: "Test Task",
description: "This is a test task.",
status: "In Progress",
priority: "High",
tags: ["test", "jest"],
startDate: "2024-01-01",
dueDate: "2024-01-15",
points: 5,
projectId: "project_12345",
authorUserId: "user_abc",
assignedUserId: "user_xyz",
}),
};
const mockError = new Error("DynamoDB error");
mockPut.mockRejectedValue(mockError);
const response = await handler(event);
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error creating task: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,104 @@
const mockPut = jest.fn();
import { handler } from "../src/handlers/createUser";
import { v4 as uuidv4 } from "uuid";
import { fetchRandomTeamId } from "@/lib/util";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
put: mockPut,
})),
},
}));
jest.mock("uuid", () => ({
v4: jest.fn(),
}));
jest.mock("@/lib/util", () => ({
fetchRandomTeamId: jest.fn(),
}));
describe("createUser handler", () => {
beforeAll(() => {
jest.spyOn(console, "info").mockImplementation(jest.fn());
});
afterAll(() => {
jest.restoreAllMocks();
});
it("should create a new user and log a success message", async () => {
const mockUUID = "mock-uuid";
const mockTeamId = "team-123";
(uuidv4 as jest.Mock).mockReturnValue(mockUUID);
(fetchRandomTeamId as jest.Mock).mockResolvedValue(mockTeamId);
const event = {
request: {
userAttributes: {
preferred_username: "testUser",
sub: "cognito-123",
},
},
userName: "fallbackUser",
};
mockPut.mockResolvedValue({});
const response = await handler(event);
expect(fetchRandomTeamId).toHaveBeenCalled();
expect(mockPut).toHaveBeenCalledWith({
TableName: "mock-user-table",
Item: {
category: "users",
cognitoId: "cognito-123",
userId: `user_${mockUUID}`,
username: "testUser",
profilePictureUrl: "p0.jpeg",
teamId: mockTeamId,
},
});
expect(response).toEqual(event);
});
it("should throw an error if DynamoDB operation fails", async () => {
const mockUUID = "mock-uuid";
const mockTeamId = "team-123";
(uuidv4 as jest.Mock).mockReturnValue(mockUUID);
(fetchRandomTeamId as jest.Mock).mockResolvedValue(mockTeamId);
const event = {
request: {
userAttributes: {
preferred_username: "testUser",
sub: "cognito-123",
},
},
userName: "fallbackUser",
};
const mockError = new Error("DynamoDB error");
mockPut.mockRejectedValue(mockError);
await expect(handler(event)).rejects.toThrow(
"Error creating user: DynamoDB error"
);
expect(fetchRandomTeamId).toHaveBeenCalled();
expect(mockPut).toHaveBeenCalledWith({
TableName: "mock-user-table",
Item: {
category: "users",
cognitoId: "cognito-123",
userId: `user_${mockUUID}`,
username: "testUser",
profilePictureUrl: "p0.jpeg",
teamId: mockTeamId,
},
});
});
});

View File

@@ -0,0 +1,73 @@
const mockQuery = jest.fn();
import { handler } from "../src/handlers/getProjects";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
describe("getProjects handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve projects and return a 200 response", async () => {
const mockProjects = [
{ projectId: "project_1", name: "Project 1" },
{ projectId: "project_2", name: "Project 2" },
];
mockQuery.mockResolvedValue({
Items: mockProjects,
});
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-project-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "projects",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockProjects),
});
});
it("should return a 500 response if the query fails", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-project-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "projects",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving projects: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,131 @@
const mockQuery = jest.fn();
const mockFetchUserWithUserId = jest.fn();
const mockFetchComments = jest.fn();
const mockFetchAttachments = jest.fn();
import { handler } from "../src/handlers/getTasks";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
jest.mock("@/lib/util", () => ({
fetchUserWithUserId: mockFetchUserWithUserId,
fetchComments: mockFetchComments,
fetchAttachments: mockFetchAttachments,
}));
describe("getTasks handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve tasks with details and return a 200 response", async () => {
const mockTasks = [
{
taskId: "task_1",
title: "Task 1",
authorUserId: "user_1",
assignedUserId: "user_2",
},
{
taskId: "task_2",
title: "Task 2",
},
];
const mockAuthor = { userId: "user_1", username: "AuthorUser" };
const mockAssignee = { userId: "user_2", username: "AssigneeUser" };
const mockComments = [{ commentId: "comment_1", content: "Nice work!" }];
const mockAttachments = [
{ attachmentId: "attachment_1", name: "file.pdf" },
];
mockQuery.mockResolvedValue({ Items: mockTasks });
mockFetchUserWithUserId.mockResolvedValueOnce(mockAuthor);
mockFetchUserWithUserId.mockResolvedValueOnce(mockAssignee);
mockFetchComments.mockResolvedValue(mockComments);
mockFetchAttachments.mockResolvedValue(mockAttachments);
const event = {
queryStringParameters: { projectId: "project_123" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-task-table",
KeyConditionExpression: "category = :category AND projectId = :projectId",
IndexName: "GSI-project-id",
ExpressionAttributeValues: {
":category": "tasks",
":projectId": "project_123",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify([
{
taskId: "task_1",
title: "Task 1",
authorUserId: "user_1",
assignedUserId: "user_2",
author: mockAuthor,
assignee: mockAssignee,
comments: mockComments,
attachments: mockAttachments,
},
{
taskId: "task_2",
title: "Task 2",
author: null,
assignee: null,
comments: mockComments,
attachments: mockAttachments,
},
]),
});
});
it("should return a 500 response if there is an error", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const event = {
queryStringParameters: { projectId: "project_123" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-task-table",
KeyConditionExpression: "category = :category AND projectId = :projectId",
IndexName: "GSI-project-id",
ExpressionAttributeValues: {
":category": "tasks",
":projectId": "project_123",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving tasks: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,113 @@
const mockQuery = jest.fn();
const mockFetchUserWithUserId = jest.fn();
import { handler } from "../src/handlers/getTeams";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
jest.mock("@/lib/util", () => ({
fetchUserWithUserId: mockFetchUserWithUserId,
}));
describe("getTeams handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve teams with usernames and return a 200 response", async () => {
const mockTeams = [
{
teamId: "team_1",
name: "Team A",
productOwnerUserId: "user_1",
projectManagerUserId: "user_2",
},
{
teamId: "team_2",
name: "Team B",
productOwnerUserId: "user_3",
},
];
const mockProductOwner1 = { userId: "user_1", username: "POUser1" };
const mockProjectManager1 = { userId: "user_2", username: "PMUser2" };
const mockProductOwner2 = { userId: "user_3", username: "POUser3" };
mockQuery.mockResolvedValue({ Items: mockTeams });
mockFetchUserWithUserId.mockImplementation((userId) => {
if (userId === "user_1") return Promise.resolve(mockProductOwner1);
if (userId === "user_2") return Promise.resolve(mockProjectManager1);
if (userId === "user_3") return Promise.resolve(mockProductOwner2);
return Promise.resolve(null);
});
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-team-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "teams",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify([
{
teamId: "team_1",
name: "Team A",
productOwnerUserId: "user_1",
projectManagerUserId: "user_2",
productOwnerUsername: "POUser1",
projectManagerUsername: "PMUser2",
},
{
teamId: "team_2",
name: "Team B",
productOwnerUserId: "user_3",
projectManagerUserId: undefined,
productOwnerUsername: "POUser3",
projectManagerUsername: null,
},
]),
});
});
it("should return a 500 response if there is an error", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-team-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "teams",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving teams: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,115 @@
const mockQuery = jest.fn();
import { handler } from "../src/handlers/getUser";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
describe("getUser handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve a user and return a 200 response", async () => {
const mockUser = {
cognitoId: "cognito_123",
userId: "user_1",
username: "TestUser",
category: "users",
};
mockQuery.mockResolvedValue({
Items: [mockUser],
});
const event = {
pathParameters: { cognitoId: "cognito_123" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category AND cognitoId = :cognitoId",
ExpressionAttributeValues: {
":category": "users",
":cognitoId": "cognito_123",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockUser),
});
});
it("should return an empty object if no user is found", async () => {
mockQuery.mockResolvedValue({
Items: [],
});
const event = {
pathParameters: { cognitoId: "cognito_456" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category AND cognitoId = :cognitoId",
ExpressionAttributeValues: {
":category": "users",
":cognitoId": "cognito_456",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({}),
});
});
it("should return a 500 response if there is an error", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const event = {
pathParameters: { cognitoId: "cognito_123" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category AND cognitoId = :cognitoId",
ExpressionAttributeValues: {
":category": "users",
":cognitoId": "cognito_123",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving user: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,120 @@
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([]),
});
});
});

View File

@@ -0,0 +1,98 @@
const mockQuery = jest.fn();
import { handler } from "../src/handlers/getUsers";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
describe("getUsers handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve all users and return a 200 response", async () => {
const mockUsers = [
{ userId: "user_1", username: "User1", category: "users" },
{ userId: "user_2", username: "User2", category: "users" },
];
mockQuery.mockResolvedValue({
Items: mockUsers,
});
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "users",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockUsers),
});
});
it("should return an empty array if no users are found", async () => {
mockQuery.mockResolvedValue({
Items: [],
});
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "users",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify([]),
});
});
it("should return a 500 response if there is an error", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "users",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving users: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,120 @@
const mockUpdate = jest.fn();
import { handler } from "../src/handlers/updateTaskStatus"; // Adjust the path as needed
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
update: mockUpdate,
})),
},
}));
describe("updateTaskStatus handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should update the task status and return the updated task", async () => {
const mockUpdatedTask = {
category: "tasks",
taskId: "task_123",
status: "Completed",
};
mockUpdate.mockResolvedValue({
Attributes: mockUpdatedTask,
});
const event = {
pathParameters: { taskId: "task_123" },
body: JSON.stringify({ status: "Completed" }),
};
const response = await handler(event);
expect(mockUpdate).toHaveBeenCalledWith({
TableName: "mock-task-table",
Key: {
category: "tasks",
taskId: "task_123",
},
UpdateExpression: "set #status = :status",
ExpressionAttributeNames: {
"#status": "status",
},
ExpressionAttributeValues: {
":status": "Completed",
},
ReturnValues: "ALL_NEW",
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockUpdatedTask),
});
});
it("should return a 500 response if the update fails", async () => {
const mockError = new Error("DynamoDB update error");
mockUpdate.mockRejectedValue(mockError);
const event = {
pathParameters: { taskId: "task_123" },
body: JSON.stringify({ status: "Completed" }),
};
const response = await handler(event);
expect(mockUpdate).toHaveBeenCalledWith({
TableName: "mock-task-table",
Key: {
category: "tasks",
taskId: "task_123",
},
UpdateExpression: "set #status = :status",
ExpressionAttributeNames: {
"#status": "status",
},
ExpressionAttributeValues: {
":status": "Completed",
},
ReturnValues: "ALL_NEW",
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error updating task: ${mockError.message}`,
}),
});
});
it("should handle invalid input gracefully", async () => {
const event = {
pathParameters: { taskId: "task_123" },
body: JSON.stringify({}),
};
const response = await handler(event);
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error updating task: DynamoDB update error`,
}),
});
});
});

View File

@@ -22,6 +22,6 @@
"@/*": ["./src/*"]
}
},
"include": ["**/*.ts", "**/*.tsx"],
"include": ["**/*.ts", "**/*.tsx", "jest.config.js"],
"exclude": ["node_modules"]
}