|
| 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 | + |
| 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 | + |
| 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 | + |
| 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 | + |
| 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