Skip to content

Commit 97b7377

Browse files
committed
Refactor tests (Gemini review)
1 parent c4f6786 commit 97b7377

File tree

2 files changed

+111
-6
lines changed

2 files changed

+111
-6
lines changed

src/__tests__/webhooks/notification.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ describe("Notification Tests", function (): void {
108108

109109
expect(notificationRequestItem.amount).toBeDefined();
110110
expect(notificationRequestItem.additionalData).toBeDefined();
111-
expect(notificationRequestItem.additionalData!.orderId).toEqual("12345");
112-
expect(notificationRequestItem.additionalData!.customerId).toEqual("67890");
111+
expect(notificationRequestItem.additionalData).toHaveProperty("orderId", "12345");
112+
expect(notificationRequestItem.additionalData).toHaveProperty("customerId", "67890");
113113

114114
});
115115

@@ -133,10 +133,12 @@ describe("Notification Tests", function (): void {
133133

134134
expect(notificationRequestItem.amount).toBeDefined();
135135
expect(notificationRequestItem.additionalData).toBeDefined();
136-
expect(notificationRequestItem.additionalData!.orderId).toEqual("12345");
137-
expect(notificationRequestItem.additionalData!.customerId).toEqual("67890");
138-
expect(notificationRequestItem.additionalData!["metadata.myKey"]).toEqual("myValue");
139-
expect(notificationRequestItem.additionalData!["metadata.anotherKey"]).toEqual("anotherValue");
136+
expect(notificationRequestItem.additionalData).toHaveProperty("orderId", "12345");
137+
expect(notificationRequestItem.additionalData).toHaveProperty("customerId", "67890");
138+
expect(notificationRequestItem.additionalData["metadata.myKey"]).toBeDefined();
139+
expect(notificationRequestItem.additionalData["metadata.anotherKey"]).toBeDefined();
140+
expect(notificationRequestItem.additionalData["metadata.myKey"]).toEqual("myValue");
141+
expect(notificationRequestItem.additionalData["metadata.anotherKey"]).toEqual("anotherValue");
140142

141143
});
142144
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* PredefinedContentHelper class to parse and manage predefined content reference IDs.
3+
*/
4+
export class PredefinedContentHelper {
5+
private params: URLSearchParams;
6+
7+
constructor(referenceId: string) {
8+
this.params = new URLSearchParams(referenceId);
9+
}
10+
11+
/**
12+
* Extracts and validates the `event` value from the ReferenceID.
13+
*
14+
* @returns A valid `DisplayNotificationEvent`, otherwise `null`.
15+
*
16+
* @example
17+
* const helper = new PredefinedContentHelper("...&event=PIN_ENTERED");
18+
* const event = helper.getEvent(); // "PIN_ENTERED" or null
19+
*/
20+
getEvent(): DisplayNotificationEvent | null {
21+
const event = this.params.get("event");
22+
23+
switch (event) {
24+
case "TENDER_CREATED":
25+
case "CARD_INSERTED":
26+
case "CARD_PRESENTED":
27+
case "CARD_SWIPED":
28+
case "WAIT_FOR_APP_SELECTION":
29+
case "APPLICATION_SELECTED":
30+
case "ASK_SIGNATURE":
31+
case "CHECK_SIGNATURE":
32+
case "SIGNATURE_CHECKED":
33+
case "WAIT_FOR_PIN":
34+
case "PIN_ENTERED":
35+
case "PRINT_RECEIPT":
36+
case "RECEIPT_PRINTED":
37+
case "CARD_REMOVED":
38+
case "TENDER_FINAL":
39+
case "ASK_DCC":
40+
case "DCC_ACCEPTED":
41+
case "DCC_REJECTED":
42+
case "ASK_GRATUITY":
43+
case "GRATUITY_ENTERED":
44+
case "BALANCE_QUERY_STARTED":
45+
case "BALANCE_QUERY_COMPLETED":
46+
case "LOAD_STARTED":
47+
case "LOAD_COMPLETED":
48+
case "PROVIDE_CARD_DETAILS":
49+
case "CARD_DETAILS_PROVIDED":
50+
return event as DisplayNotificationEvent;
51+
default:
52+
return null;
53+
}
54+
}
55+
getTransactionId(): string | null {
56+
return this.params.get("TransactionID");
57+
}
58+
59+
getTimeStamp(): string | null {
60+
return this.params.get("TimeStamp");
61+
}
62+
63+
get(key: string): string | null {
64+
return this.params.get(key);
65+
}
66+
67+
toObject(): Record<string, string> {
68+
const result: Record<string, string> = {};
69+
for (const [key, value] of this.params.entries()) {
70+
result[key] = value;
71+
}
72+
return result;
73+
}
74+
}
75+
76+
export enum DisplayNotificationEvent {
77+
TENDER_CREATED = "TENDER_CREATED",
78+
CARD_INSERTED = "CARD_INSERTED",
79+
CARD_PRESENTED = "CARD_PRESENTED",
80+
CARD_SWIPED = "CARD_SWIPED",
81+
WAIT_FOR_APP_SELECTION = "WAIT_FOR_APP_SELECTION",
82+
APPLICATION_SELECTED = "APPLICATION_SELECTED",
83+
ASK_SIGNATURE = "ASK_SIGNATURE",
84+
CHECK_SIGNATURE = "CHECK_SIGNATURE",
85+
SIGNATURE_CHECKED = "SIGNATURE_CHECKED",
86+
WAIT_FOR_PIN = "WAIT_FOR_PIN",
87+
PIN_ENTERED = "PIN_ENTERED",
88+
PRINT_RECEIPT = "PRINT_RECEIPT",
89+
RECEIPT_PRINTED = "RECEIPT_PRINTED",
90+
CARD_REMOVED = "CARD_REMOVED",
91+
TENDER_FINAL = "TENDER_FINAL",
92+
ASK_DCC = "ASK_DCC",
93+
DCC_ACCEPTED = "DCC_ACCEPTED",
94+
DCC_REJECTED = "DCC_REJECTED",
95+
ASK_GRATUITY = "ASK_GRATUITY",
96+
GRATUITY_ENTERED = "GRATUITY_ENTERED",
97+
BALANCE_QUERY_STARTED = "BALANCE_QUERY_STARTED",
98+
BALANCE_QUERY_COMPLETED = "BALANCE_QUERY_COMPLETED",
99+
LOAD_STARTED = "LOAD_STARTED",
100+
LOAD_COMPLETED = "LOAD_COMPLETED",
101+
PROVIDE_CARD_DETAILS = "PROVIDE_CARD_DETAILS",
102+
CARD_DETAILS_PROVIDED = "CARD_DETAILS_PROVIDED",
103+
}

0 commit comments

Comments
 (0)