Skip to content

Commit 09c013c

Browse files
committed
Rewrite tests in Typescript
1 parent 0c5531b commit 09c013c

File tree

1 file changed

+42
-66
lines changed

1 file changed

+42
-66
lines changed

src/__tests__/notification.spec.ts

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -312,72 +312,48 @@ describe("Notification Test", function (): void {
312312
expect(negativeBalanceCompensationWarningNotificationRequest.data.creationDate?.toISOString()).toBe(new Date("2024-07-02T02:01:08+02:00").toISOString());
313313
});
314314

315-
it("should correctly deserialize notificationItems and handle additionalData.metadata", () => {
316-
// Scenario 1: Valid metadata with string values
317-
const validNotification = {
318-
live: "true",
319-
notificationItems: [
320-
{
321-
NotificationRequestItem: {
322-
pspReference: "123456789",
323-
eventCode: "AUTHORISATION",
324-
eventDate: "2021-01-01T00:00:00+00:00",
325-
merchantAccountCode: "TestAccount",
326-
merchantReference: "TestRef",
327-
success: "true",
328-
amount: { currency: "EUR", value: 100 },
329-
additionalData: {
330-
metadata: {
331-
key1: "value1",
332-
key2: "value2"
333-
}
334-
}
335-
}
336-
}
337-
]
338-
};
339-
340-
const validRequest = new NotificationRequest(validNotification as any);
341-
const validItem = validRequest.notificationItems![0];
342-
const validMetadata = validItem.additionalData?.metadata as unknown as Record<string, string>;
343-
expect(validMetadata).toEqual({
344-
key1: "value1",
345-
key2: "value2"
346-
});
347-
348-
// Scenario 2: Metadata containing a nested object (which is not a plain string)
349-
const invalidNotification = {
350-
live: "true",
351-
notificationItems: [
352-
{
353-
NotificationRequestItem: {
354-
pspReference: "123456789",
355-
eventCode: "AUTHORISATION",
356-
eventDate: "2021-01-01T00:00:00+00:00",
357-
merchantAccountCode: "TestAccount",
358-
merchantReference: "TestRef",
359-
success: "true",
360-
amount: { currency: "EUR", value: 100 },
361-
additionalData: {
362-
metadata: {
363-
key1: { nestedKey: "nestedValue" }
364-
}
365-
}
366-
}
367-
}
368-
]
369-
};
370-
371-
const invalidRequest = new NotificationRequest(invalidNotification as any);
372-
// type expects string, check that the value is not a string, accepts nested?
373-
// Get the first notification item from the invalidRequest.
374-
const invalidItem = invalidRequest.notificationItems![0];
375-
const additionalData = invalidItem.additionalData;
376-
const metadata = additionalData ? additionalData.metadata : undefined;
377-
const key1Value = metadata ? metadata["key1"] : undefined;
378-
const key1ValueType = typeof key1Value;
379-
// Assert that the type of key1Value is not "string".
380-
expect(key1ValueType).not.toBe("string");
315+
// test additionalData without metadata
316+
it("should correctly store additionalData as a key-value strings", () => {
317+
const notification = new NotificationRequestItem();
318+
notification.amount = { currency: "EUR", value: 1000 };
319+
notification.pspReference = "1234567890123456";
320+
notification.eventCode = NotificationEnum.Authorisation;
321+
notification.eventDate = "2024-03-05T12:00:00Z";
322+
notification.merchantAccountCode = "TestMerchant";
323+
notification.merchantReference = "Order-12345";
324+
notification.success = SuccessEnum.True;
325+
notification.additionalData = {
326+
orderId: "12345",
327+
customerId: "54321",
328+
}
329+
330+
expect(notification.additionalData).toBeDefined();
331+
expect(notification.additionalData?.orderId).toBe("12345");
332+
expect(notification.additionalData?.customerId).toBe("54321");
333+
expect(notification.additionalData.metadata).toBeUndefined;
334+
});
335+
336+
// test additionalData with metadata as set of key-value pairs prefixed with 'metadata.'. For example, 'metadata.myField: myValue'
337+
it("should correctly store additionalData as a key-value object", () => {
338+
const notification = new NotificationRequestItem();
339+
notification.amount = { currency: "EUR", value: 1000 };
340+
notification.pspReference = "1234567890123456";
341+
notification.eventCode = NotificationEnum.Authorisation;
342+
notification.eventDate = "2024-03-05T12:00:00Z";
343+
notification.merchantAccountCode = "TestMerchant";
344+
notification.merchantReference = "Order-12345";
345+
notification.success = SuccessEnum.True;
346+
notification.additionalData = {
347+
orderId: "12345",
348+
customerId: "54321",
349+
"metadata.myField": "myValue",
350+
"metadata.anotherField": "anotherValue"
351+
}
352+
353+
expect(notification.additionalData).toBeDefined();
354+
expect(notification.additionalData?.orderId).toBe("12345");
355+
expect(notification.additionalData?.customerId).toBe("54321");
356+
expect(notification.additionalData["metadata.myField"]).toBe("myValue");
381357
});
382358

383359
});

0 commit comments

Comments
 (0)