Skip to content

Instantly share code, notes, and snippets.

@igoralves1
Created August 21, 2024 13:36
Show Gist options
  • Save igoralves1/4cebb553d71541f7c6f3948be1489908 to your computer and use it in GitHub Desktop.
Save igoralves1/4cebb553d71541f7c6f3948be1489908 to your computer and use it in GitHub Desktop.
zz
const { getByDate } = require("../api");
const db = require("../db");
jest.mock("../db", () => ({
send: jest.fn(),
}));
describe("getByDate function", () => {
const mockResponseData = {
Items: [
{
reason: { S: "FLOW_END" },
GSI4PK: { S: "SEARCH_ID#com.twilio.studio.flow" },
GSI3PK: { S: "SEARCH_TO#com.twilio.studio.flow" },
GSI2PK: { S: "SEARCH_FROM#com.twilio.studio.flow" },
fromNumber: { S: "18886602449" },
duration: { N: "0.026" },
executionId: { S: "FN846b958fe0c70b9c6c3e0388a2b004eb" },
GSI1PK: { S: "SEARCH_DATE#com.twilio.studio.flow" },
GSI4SK: { S: "CAf0473b17886439dc380356419cde4818" },
GSI3SK: { S: "15108043852" },
GSI2SK: { S: "18886602449" },
GSI1SK: { S: "2024-07-25T13:24:00.351Z" },
SK: { S: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb" },
flowSid: { S: "FWd7b6ceaf8004f55275f8dc00c16589d2" },
PK: {
S: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
},
time: { S: "2024-07-25T13:24:00.351Z" },
id: { S: "CAf0473b17886439dc380356419cde4818" },
toNumber: { S: "15108043852" },
direction: { S: "inbound" },
status: { S: "Complete" },
},
],
};
beforeEach(() => {
db.send.mockReset();
});
it("should return a successful response with the correct data", async () => {
db.send.mockResolvedValueOnce(mockResponseData);
const event = {
queryStringParameters: {
startDate: "2024-07-25",
endDate: "2024-07-25",
gsi1pk: "SEARCH_DATE#com.twilio.studio.flow",
ended_reason: "FLOW_END",
status: "Complete",
},
};
const response = await getByDate(event);
console.log("Successful Response:", JSON.parse(response.body).data);
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body).data).toEqual([
{
reason: "FLOW_END",
GSI4PK: "SEARCH_ID#com.twilio.studio.flow",
GSI3PK: "SEARCH_TO#com.twilio.studio.flow",
GSI2PK: "SEARCH_FROM#com.twilio.studio.flow",
fromNumber: "18886602449",
duration: 0.026,
executionId: "FN846b958fe0c70b9c6c3e0388a2b004eb",
GSI1PK: "SEARCH_DATE#com.twilio.studio.flow",
GSI4SK: "CAf0473b17886439dc380356419cde4818",
GSI3SK: "15108043852",
GSI2SK: "18886602449",
GSI1SK: "2024-07-25T13:24:00.351Z",
SK: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb",
flowSid: "FWd7b6ceaf8004f55275f8dc00c16589d2",
PK: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
time: "2024-07-25T13:24:00.351Z",
id: "CAf0473b17886439dc380356419cde4818",
toNumber: "15108043852",
direction: "inbound",
status: "Complete",
},
]);
});
it("should return an error if startDate is after endDate", async () => {
const event = {
queryStringParameters: {
startDate: "2024-07-26",
endDate: "2024-07-25",
gsi1pk: "SEARCH_DATE#com.twilio.studio.flow",
},
};
const response = await getByDate(event);
console.log("Error Response:", JSON.parse(response.body));
expect(response.statusCode).toBe(500);
expect(JSON.parse(response.body).message).toBe("Failed to get data.");
expect(JSON.parse(response.body).errorMsg).toBe(
"Start Date Must Be Before or Equal to End Date"
);
});
it("should return an error if gsi1pk is missing", async () => {
const event = {
queryStringParameters: {
startDate: "2024-07-25",
endDate: "2024-07-25",
},
};
const response = await getByDate(event);
console.log("Error Response:", JSON.parse(response.body));
expect(response.statusCode).toBe(500);
expect(JSON.parse(response.body).message).toBe("Failed to get data.");
expect(JSON.parse(response.body).errorMsg).toBe(
"GSI1PK parameter is required"
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment