Skip to content

Commit 707ade8

Browse files
committed
Post Notification Unit Tests
1 parent cd22abf commit 707ade8

File tree

5 files changed

+794
-5
lines changed

5 files changed

+794
-5
lines changed

lib/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const messages = {
1010
INVALID_NOTIFICATION_TYPES: "Notification Types must contain the following key: 'NotificationTypeKey'.",
1111
DESTINATION_NOT_FOUND: "Failed to get destination: ",
1212
MANDATORY_PARAMETER_NOT_PASSED_FOR_DEFAULT_NOTIFICATION: "Recipients and title are mandatory parameters",
13-
MANDATORY_PARAMETER_NOT_PASSED_FOR_CUSTOM_NOTIFICATION: "Recipients, type are mandatory parameters",
13+
MANDATORY_PARAMETER_NOT_PASSED_FOR_CUSTOM_NOTIFICATION: "Recipients are mandatory parameters",
1414
RECIPIENTS_IS_NOT_ARRAY: "Recipients is not an array or it is empty",
1515
TITLE_IS_NOT_STRING: "Title is not a string",
1616
DESCRIPTION_IS_NOT_STRING: "Description is not a string",
@@ -61,7 +61,7 @@ function validateDefaultNotifyParameters(recipients, priority, title, descriptio
6161
}
6262

6363
function validateCustomNotifyParameters(type, recipients, properties, navigation, priority, payload) {
64-
if (!recipients || !type) {
64+
if (!recipients) {
6565
console.log(messages.MANDATORY_PARAMETER_NOT_PASSED_FOR_CUSTOM_NOTIFICATION);
6666
return false;
6767
}
@@ -180,7 +180,7 @@ function buildCustomNotification(notificationData) {
180180
OriginId: notificationData["payload"] ? notificationData["payload"]["OriginId"] : undefined,
181181
NotificationTypeId: notificationData["payload"] ? notificationData["payload"]["NotificationTypeId"] : undefined,
182182
NotificationTypeKey: getNotificationTypesKeyWithPrefix(notificationData["type"]),
183-
NotificationTypeVersion: notificationData["payload"] ? notificationData["payload"]["NotificationTypeVersion"] : "1",
183+
NotificationTypeVersion: notificationData["payload"] && notificationData["payload"]["NotificationTypeVersion"] ? notificationData["payload"]["NotificationTypeVersion"] : "1",
184184
NavigationTargetAction: notificationData["navigation"] ? notificationData["navigation"]["NavigationTargetAction"] : undefined,
185185
NavigationTargetObject: notificationData["navigation"] ? notificationData["navigation"]["NavigationTargetObject"] : undefined,
186186
Priority: notificationData["priority"] ? notificationData["priority"] : "NEUTRAL",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"lodash": "4.17.21"
1919
},
2020
"devDependencies": {
21-
"jest": "^29.6.4"
21+
"jest": "^29.7.0"
2222
},
2323
"scripts": {
2424
"lint": "npx eslint .",
@@ -46,4 +46,4 @@
4646
}
4747
}
4848
}
49-
}
49+
}

test/lib/notifications.test.js

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
const {getNotificationDestination, buildNotification, executeRequest} = require("./../../lib/utils");
2+
const {buildHeadersForDestination} = require("@sap-cloud-sdk/connectivity");
3+
const {postNotification} = require("./../../lib/notifications");
4+
5+
jest.mock("./../../lib/utils");
6+
jest.mock("@sap-cloud-sdk/connectivity");
7+
8+
const expectedDefaultNotificationWithoutDescription = {
9+
NotificationTypeKey: "Default",
10+
NotificationTypeVersion: "1",
11+
Priority: "HIGH",
12+
Properties: [
13+
{
14+
Key:"title",
15+
IsSensitive:false,
16+
Language :"en",
17+
Value :"Some Test Title",
18+
Type :"String"
19+
},
20+
{
21+
Key:"description",
22+
IsSensitive:false,
23+
Language :"en",
24+
Value :"",
25+
Type :"String"
26+
}
27+
],
28+
Recipients: [
29+
{
30+
RecipientId: "[email protected]"
31+
}
32+
]
33+
};
34+
35+
const expectedDefaultNotificationWithDescription = {
36+
NotificationTypeKey: "Default",
37+
NotificationTypeVersion: "1",
38+
Priority: "HIGH",
39+
Properties: [
40+
{
41+
Key:"title",
42+
IsSensitive:false,
43+
Language :"en",
44+
Value :"Some Test Title",
45+
Type :"String"
46+
},
47+
{
48+
Key:"description",
49+
IsSensitive:false,
50+
Language :"en",
51+
Value :"Some Test Description",
52+
Type :"String"
53+
}
54+
],
55+
Recipients: [
56+
{
57+
RecipientId: "[email protected]"
58+
}
59+
]
60+
};
61+
62+
describe ("Test post notification", () => {
63+
test("When passed arguments are incorrect to postNotification", () => {
64+
getNotificationDestination.mockReturnValue(undefined);
65+
buildHeadersForDestination.mockReturnValue(undefined);
66+
buildNotification.mockReturnValue(undefined);
67+
68+
expect(postNotification({})).toMatchObject({});
69+
})
70+
71+
test("When passed whole notification object to postNotification", () => {
72+
const expectedCustomNotification = {
73+
NotificationTypeKey: "Custom",
74+
NotificationTypeVersion: "1",
75+
Priority: "HIGH",
76+
Properties: [
77+
{
78+
Key:"title",
79+
IsSensitive:false,
80+
Language :"en",
81+
Value :"Some Text Title",
82+
Type :"String"
83+
},
84+
{
85+
Key:"description",
86+
IsSensitive:false,
87+
Language :"en",
88+
Value :"Some Text Description",
89+
Type :"String"
90+
}
91+
],
92+
Recipients: [
93+
{
94+
RecipientId: "[email protected]"
95+
}
96+
]
97+
};
98+
99+
getNotificationDestination.mockReturnValue(undefined);
100+
buildHeadersForDestination.mockReturnValue(undefined);
101+
buildNotification.mockReturnValue(expectedCustomNotification);
102+
executeRequest.mockReturnValue(Promise.resolve(expectedCustomNotification));
103+
104+
expect(postNotification(expectedCustomNotification)).toMatchObject(Promise.resolve(expectedCustomNotification));
105+
})
106+
107+
test("When passed recipients, priority, title to postNotification", () => {
108+
getNotificationDestination.mockReturnValue(undefined);
109+
buildHeadersForDestination.mockReturnValue(undefined);
110+
buildNotification.mockReturnValue(expectedDefaultNotificationWithoutDescription);
111+
executeRequest.mockReturnValue(Promise.resolve(expectedDefaultNotificationWithoutDescription));
112+
113+
expect(postNotification(
114+
{
115+
recipients: ["[email protected]"],
116+
title: "Some Test Title",
117+
priority: "HIGH"
118+
})
119+
).toMatchObject(Promise.resolve(expectedDefaultNotificationWithoutDescription));
120+
})
121+
122+
test("When passed recipients, priority, title, description to postNotification", () => {
123+
getNotificationDestination.mockReturnValue(undefined);
124+
buildHeadersForDestination.mockReturnValue(undefined);
125+
buildNotification.mockReturnValue(expectedDefaultNotificationWithoutDescription);
126+
executeRequest.mockReturnValue(Promise.resolve(expectedDefaultNotificationWithoutDescription));
127+
128+
expect(postNotification(
129+
{
130+
recipients: ["[email protected]"],
131+
title: "Some Test Title",
132+
priority: "HIGH",
133+
description: "Some Test Description"
134+
})
135+
).toMatchObject(Promise.resolve(expectedDefaultNotificationWithDescription));
136+
})
137+
138+
test("When passed recipients, priority, title, description to postNotification", () => {
139+
getNotificationDestination.mockReturnValue(undefined);
140+
buildHeadersForDestination.mockReturnValue(undefined);
141+
buildNotification.mockReturnValue(expectedDefaultNotificationWithoutDescription);
142+
executeRequest.mockReturnValue(Promise.resolve(expectedDefaultNotificationWithoutDescription));
143+
144+
expect(postNotification(
145+
{
146+
recipients: ["[email protected]"],
147+
title: "Some Test Title",
148+
priority: "HIGH",
149+
description: "Some Test Description"
150+
})
151+
).toMatchObject(Promise.resolve(expectedDefaultNotificationWithDescription));
152+
})
153+
154+
test("When passed recipients, priority, title, description to postNotification", () => {
155+
const expectedNotification = {
156+
NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef",
157+
NotificationTypeKey: "alert-notification/TestNotificationType",
158+
NotificationTypeVersion: "1",
159+
NavigationTargetAction: "TestTargetAction",
160+
NavigationTargetObject: "TestTargetObject",
161+
Priority: "HIGH",
162+
ProviderId: "SAMPLEPROVIDER",
163+
ActorId: "BACKENDACTORID",
164+
ActorDisplayText: "ActorName",
165+
ActorImageURL: "https://some-url",
166+
NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z",
167+
Properties: [
168+
{
169+
Key:"title",
170+
IsSensitive:false,
171+
Language :"en",
172+
Value :"Some Test Title",
173+
Type :"String"
174+
}
175+
],
176+
Recipients: [
177+
{
178+
RecipientId: "[email protected]"
179+
}
180+
],
181+
TargetParameters: [
182+
{
183+
"Key": "string",
184+
"Value": "string"
185+
}
186+
]
187+
};
188+
189+
getNotificationDestination.mockReturnValue(undefined);
190+
buildHeadersForDestination.mockReturnValue(undefined);
191+
buildNotification.mockReturnValue(expectedNotification);
192+
executeRequest.mockReturnValue(Promise.resolve(expectedNotification));
193+
194+
expect(postNotification(
195+
{
196+
recipients: ["[email protected]"],
197+
type: "TestNotificationType",
198+
properties: [{
199+
Key:"title",
200+
IsSensitive:false,
201+
Language :"en",
202+
Value :"Some Test Title",
203+
Type :"String"
204+
}],
205+
navigation: {
206+
NavigationTargetAction: "TestTargetAction",
207+
NavigationTargetObject: "TestTargetObject"
208+
},
209+
priority: "HIGH",
210+
payload: {
211+
NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef",
212+
ProviderId: "SAMPLEPROVIDER",
213+
ActorId: "BACKENDACTORID",
214+
ActorDisplayText: "ActorName",
215+
ActorImageURL: "https://some-url",
216+
NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z",
217+
TargetParameters: [
218+
{
219+
"Key": "string",
220+
"Value": "string"
221+
}
222+
]
223+
}
224+
})
225+
).toMatchObject(Promise.resolve(expectedNotification));
226+
})
227+
})

0 commit comments

Comments
 (0)