-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch.test.ts
50 lines (40 loc) · 1.76 KB
/
search.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { APIGatewayEvent, APIGatewayProxyEventPathParameters } from "aws-lambda";
import { getRequestContext, makoStateSubmitter } from "mocks";
import { describe, expect, it } from "vitest";
import { handler } from "./search";
describe("getSearchData Handler", () => {
it("should call validateEnvVariable and return 400 if index path parameter is missing", async () => {
const event = { pathParameters: {} } as APIGatewayEvent;
const res = await handler(event);
expect(res).toBeTruthy();
expect(res.statusCode).toEqual(400);
expect(res.body).toEqual(JSON.stringify({ message: "Index path parameter required" }));
});
it("should return 200 with search results", async () => {
const event = {
body: JSON.stringify({ query: { match_all: {} } }),
pathParameters: { index: "main" } as APIGatewayProxyEventPathParameters,
requestContext: getRequestContext(makoStateSubmitter),
} as APIGatewayEvent;
const res = await handler(event);
expect(res).toBeTruthy();
expect(res.statusCode).toEqual(200);
const body = JSON.parse(res.body);
expect(body).toBeTruthy();
expect(body?.hits?.hits).toBeTruthy();
expect(body?.hits?.hits?.length).toEqual(13);
});
it("should handle errors during processing", async () => {
const event = {
body: JSON.stringify({ query: { match_all: { id: "throw-error" } } }),
pathParameters: { index: "main" } as APIGatewayProxyEventPathParameters,
requestContext: getRequestContext(makoStateSubmitter),
} as APIGatewayEvent;
const res = await handler(event);
expect(res).toBeTruthy();
expect(res.statusCode).toEqual(500);
expect(res.body).toEqual(
JSON.stringify({ error: "Internal server error", message: "Response Error" }),
);
});
});