Skip to content

Commit

Permalink
Refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-szlapa committed Jul 5, 2024
1 parent ecaf529 commit a71b73e
Showing 1 changed file with 101 additions and 105 deletions.
206 changes: 101 additions & 105 deletions packages/gsul/tests/testHander.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ describe("test handler", () => {
jest.clearAllMocks()
})

it("respond with error when schema version is 2", async () => {
const response = await handler(
{
const testCases = [
{
description: "responds with error when schema version is 2",
event: {
schemaVersion: 2,
prescriptions: [
{
Expand All @@ -40,53 +41,32 @@ describe("test handler", () => {
}
]
},
dummyContext
)
expect(response).toMatchObject({
schemaVersion: 1,
isSuccess: false,
prescriptions: []
})
})

it("respond with success for empty request", async () => {
const mockReply = {
Count: 0,
Items: []
}
jest.spyOn(DynamoDBDocumentClient.prototype, "send").mockResolvedValue(mockReply as never)

const response = await handler(
{
mockReply: null,
expectedResponse: {
schemaVersion: 1,
isSuccess: false,
prescriptions: []
}
},
{
description: "responds with success for empty request",
event: {
schemaVersion: 1,
prescriptions: []
},
dummyContext
)
expect(response).toMatchObject({
schemaVersion: 1,
isSuccess: true,
prescriptions: []
})
})

it("respond with success when data passed in with a terminal status 'completed'", async () => {
const mockReply = {
Count: 1,
Items: [
{
PrescriptionID: "abc",
LineItemID: "item_1",
Status: "latest_status",
TerminalStatus: "completed",
LastModified: "1970-01-01T00:00:00Z"
}
]
}
jest.spyOn(DynamoDBDocumentClient.prototype, "send").mockResolvedValue(mockReply as never)

const response = await handler(
{
mockReply: {
Count: 0,
Items: []
},
expectedResponse: {
schemaVersion: 1,
isSuccess: true,
prescriptions: []
}
},
{
description: "responds with success when data passed in with a terminal status 'completed'",
event: {
schemaVersion: 1,
prescriptions: [
{
Expand All @@ -95,45 +75,40 @@ describe("test handler", () => {
}
]
},
dummyContext
)
expect(response).toMatchObject({
schemaVersion: 1,
isSuccess: true,
prescriptions: [
{
prescriptionID: "abc",
onboarded: true,
items: [
{
itemId: "item_1",
latestStatus: "latest_status",
isTerminalState: true,
lastUpdateDateTime: "1970-01-01T00:00:00Z"
}
]
}
]
})
})

it("respond with success when data passed in with a terminal status 'in-progress'", async () => {
const mockReply = {
Count: 1,
Items: [
{
PrescriptionID: "abc",
LineItemID: "item_1",
Status: "latest_status",
TerminalStatus: "in-progress",
LastModified: "1970-01-01T00:00:00Z"
}
]
}
jest.spyOn(DynamoDBDocumentClient.prototype, "send").mockResolvedValue(mockReply as never)

const response = await handler(
{
mockReply: {
Count: 1,
Items: [
{
PrescriptionID: "abc",
LineItemID: "item_1",
Status: "latest_status",
TerminalStatus: "completed",
LastModified: "1970-01-01T00:00:00Z"
}
]
},
expectedResponse: {
schemaVersion: 1,
isSuccess: true,
prescriptions: [
{
prescriptionID: "abc",
onboarded: true,
items: [
{
itemId: "item_1",
latestStatus: "latest_status",
isTerminalState: true,
lastUpdateDateTime: "1970-01-01T00:00:00Z"
}
]
}
]
}
},
{
description: "responds with success when data passed in with a terminal status 'in-progress'",
event: {
schemaVersion: 1,
prescriptions: [
{
Expand All @@ -142,25 +117,46 @@ describe("test handler", () => {
}
]
},
dummyContext
)
expect(response).toMatchObject({
schemaVersion: 1,
isSuccess: true,
prescriptions: [
{
prescriptionID: "abc",
onboarded: true,
items: [
{
itemId: "item_1",
latestStatus: "latest_status",
isTerminalState: false,
lastUpdateDateTime: "1970-01-01T00:00:00Z"
}
]
}
]
mockReply: {
Count: 1,
Items: [
{
PrescriptionID: "abc",
LineItemID: "item_1",
Status: "latest_status",
TerminalStatus: "in-progress",
LastModified: "1970-01-01T00:00:00Z"
}
]
},
expectedResponse: {
schemaVersion: 1,
isSuccess: true,
prescriptions: [
{
prescriptionID: "abc",
onboarded: true,
items: [
{
itemId: "item_1",
latestStatus: "latest_status",
isTerminalState: false,
lastUpdateDateTime: "1970-01-01T00:00:00Z"
}
]
}
]
}
}
]

testCases.forEach(({description, event, mockReply, expectedResponse}) => {
it(description, async () => {
if (mockReply) {
jest.spyOn(DynamoDBDocumentClient.prototype, "send").mockResolvedValue(mockReply as never)
}
const response = await handler(event, dummyContext)
expect(response).toMatchObject(expectedResponse)
})
})
})

0 comments on commit a71b73e

Please sign in to comment.