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/getUser.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

116 lines
2.8 KiB
TypeScript

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}`,
}),
});
});
});