-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathIterableInApp.test.ts
More file actions
324 lines (283 loc) · 10.3 KB
/
IterableInApp.test.ts
File metadata and controls
324 lines (283 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { NativeEventEmitter } from 'react-native';
import { MockRNIterableAPI } from '../__mocks__/MockRNIterableAPI';
import {
Iterable,
IterableConfig,
IterableEventName,
IterableInAppCloseSource,
IterableInAppDeleteSource,
IterableInAppLocation,
IterableInAppMessage,
IterableInAppShowResponse,
IterableInAppTrigger,
IterableInAppTriggerType,
IterableInboxMetadata,
} from '..';
describe('Iterable In App', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('trackInAppOpen_params_methodCalledWithParams', () => {
// GIVEN an in-app message and a location
const msg: IterableInAppMessage = new IterableInAppMessage(
'someMessageId',
123,
new IterableInAppTrigger(IterableInAppTriggerType.event),
new Date(1234),
new Date(123123),
true,
new IterableInboxMetadata('title', 'subtitle', 'iconURL'),
{ CustomPayloadKey: 'CustomPayloadValue' },
false,
300.5
);
const location: IterableInAppLocation = IterableInAppLocation.inApp;
// WHEN Iterable.trackInAppOpen is called
Iterable.trackInAppOpen(msg, location);
// THEN corresponding method is called on MockIterableAPI with appropriate parameters
expect(MockRNIterableAPI.trackInAppOpen).toBeCalledWith(
msg.messageId,
location
);
});
test('trackInAppClick_params_methodCalledWithParams', () => {
// GIVEN an in-app message, a location, and a url
const msg: IterableInAppMessage = new IterableInAppMessage(
'someMessageId',
123,
new IterableInAppTrigger(IterableInAppTriggerType.event),
new Date(1234),
new Date(123123),
true,
new IterableInboxMetadata('title', 'subtitle', 'iconURL'),
{ CustomPayloadKey: 'CustomPayloadValue' },
false,
300.5
);
const location: IterableInAppLocation = IterableInAppLocation.inApp;
const url: string = 'URLClicked';
// WHEN Iterable.trackInAppClick is called
Iterable.trackInAppClick(msg, location, url);
// THEN corresponding method is called on MockIterableAPI with appropriate parameters
expect(MockRNIterableAPI.trackInAppClick).toBeCalledWith(
msg.messageId,
location,
url
);
});
test('trackInAppClose_params_methodCalledWithParams', () => {
// GIVEN an in-app messsage, a location, a close source, and a url
const msg: IterableInAppMessage = new IterableInAppMessage(
'someMessageId',
123,
new IterableInAppTrigger(IterableInAppTriggerType.event),
new Date(1234),
new Date(123123),
true,
new IterableInboxMetadata('title', 'subtitle', 'iconURL'),
{ CustomPayloadKey: 'CustomPayloadValue' },
false,
300.5
);
const location: IterableInAppLocation = IterableInAppLocation.inbox;
const source: IterableInAppCloseSource = IterableInAppCloseSource.link;
const url: string = 'ClickedURL';
// WHEN Iterable.trackInAppClose is called
Iterable.trackInAppClose(msg, location, source, url);
// THEN corresponding method is called on MockIterableAPI with appropriate parameters
expect(MockRNIterableAPI.trackInAppClose).toBeCalledWith(
msg.messageId,
location,
source,
url
);
});
test('inAppConsume_params_methodCalledWithParams', () => {
// GIVEN an in-app messsage, a location, and a delete source
const msg = new IterableInAppMessage(
'asdf',
1234,
new IterableInAppTrigger(IterableInAppTriggerType.never),
undefined,
undefined,
false,
undefined,
undefined,
false,
300.5
);
const location: IterableInAppLocation = IterableInAppLocation.inApp;
const source: IterableInAppDeleteSource = IterableInAppDeleteSource.unknown;
// WHEN Iterable.inAppConsume is called
Iterable.inAppConsume(msg, location, source);
// THEN corresponding method is called on MockIterableAPI with appropriate parameters
expect(MockRNIterableAPI.inAppConsume).toBeCalledWith(
msg.messageId,
location,
source
);
});
test('inAppHandler_messageAndEventEmitted_methodCalledWithMessage', () => {
// sets up event emitter
const nativeEmitter = new NativeEventEmitter();
nativeEmitter.removeAllListeners(IterableEventName.handleInAppCalled);
// sets up config file and inAppHandler function
const config = new IterableConfig();
config.inAppHandler = jest.fn((_message: IterableInAppMessage) => {
return IterableInAppShowResponse.show;
});
// initialize Iterable object
Iterable.initialize('apiKey', config);
// GIVEN an in-app message
const messageDict = {
messageId: 'message1',
campaignId: 1234,
trigger: { type: IterableInAppTriggerType.immediate },
priorityLevel: 300.5,
};
const expectedMessage = new IterableInAppMessage(
'message1',
1234,
new IterableInAppTrigger(IterableInAppTriggerType.immediate),
undefined,
undefined,
false,
undefined,
undefined,
false,
300.5
);
// WHEN handleInAppCalled event is emitted
nativeEmitter.emit(IterableEventName.handleInAppCalled, messageDict);
// THEN inAppHandler and MockRNIterableAPI.setInAppShowResponse is called with message
expect(config.inAppHandler).toBeCalledWith(expectedMessage);
expect(MockRNIterableAPI.setInAppShowResponse).toBeCalledWith(
IterableInAppShowResponse.show
);
});
test('fromDict_withCreatedAtAndExpiresAt_returnsDateObjects', () => {
// GIVEN a message dict with numeric timestamps from the native bridge
const createdAtMs = 1609459200000; // 2021-01-01T00:00:00.000Z
const expiresAtMs = 1609545600000; // 2021-01-02T00:00:00.000Z
const messageDict = {
messageId: 'message1',
campaignId: 1234,
trigger: { type: IterableInAppTriggerType.immediate },
createdAt: createdAtMs,
expiresAt: expiresAtMs,
priorityLevel: 300.5,
};
// WHEN fromDict is called
const message = IterableInAppMessage.fromDict(messageDict);
// THEN createdAt and expiresAt are Date objects with the correct values
expect(message.createdAt).toBeInstanceOf(Date);
expect(message.expiresAt).toBeInstanceOf(Date);
expect(message.createdAt?.getTime()).toBe(createdAtMs);
expect(message.expiresAt?.getTime()).toBe(expiresAtMs);
});
test('fromDict_withoutCreatedAtAndExpiresAt_returnsUndefined', () => {
// GIVEN a message dict without timestamps
const messageDict = {
messageId: 'message1',
campaignId: 1234,
trigger: { type: IterableInAppTriggerType.immediate },
priorityLevel: 300.5,
};
// WHEN fromDict is called
const message = IterableInAppMessage.fromDict(messageDict);
// THEN createdAt and expiresAt are undefined
expect(message.createdAt).toBeUndefined();
expect(message.expiresAt).toBeUndefined();
});
test('getMessages_noParams_returnsMessages', async () => {
// GIVEN a list of in-app messages representing the local queue
const messageDicts = [
{
messageId: 'message1',
campaignId: 1234,
trigger: { type: IterableInAppTriggerType.immediate },
},
{
messageId: 'message2',
campaignId: 2345,
trigger: { type: IterableInAppTriggerType.never },
},
];
const messages = messageDicts.map((message) =>
IterableInAppMessage.fromDict(message)
);
// WHEN the simulated local queue is set to the in-app messages
MockRNIterableAPI.setMessages(messages);
// THEN Iterable.inAppManager.getMessages returns the list of in-app messages
return await Iterable.inAppManager
?.getMessages()
.then((messagesObtained) => {
expect(messagesObtained).toEqual(messages);
});
});
test('showMessage_messageAndConsume_returnsClickedUrl', async () => {
// GIVEN an in-app message and a clicked url
const messageDict = {
messageId: 'message1',
campaignId: 1234,
trigger: { type: IterableInAppTriggerType.immediate },
};
const message: IterableInAppMessage =
IterableInAppMessage.fromDict(messageDict);
const consume: boolean = true;
const clickedUrl: string = 'testUrl';
// WHEN the simulated clicked url is set to the clicked url
MockRNIterableAPI.setClickedUrl(clickedUrl);
// THEN Iterable,inAppManager.showMessage returns the simulated clicked url
return await Iterable.inAppManager
?.showMessage(message, consume)
.then((url) => {
expect(url).toEqual(clickedUrl);
});
});
test('removeMessage_params_methodCalledWithParams', () => {
// GIVEN an in-app message
const messageDict = {
messageId: 'message1',
campaignId: 1234,
trigger: { type: IterableInAppTriggerType.immediate },
};
const message = IterableInAppMessage.fromDict(messageDict);
const location: IterableInAppLocation = IterableInAppLocation.inApp;
const source: IterableInAppDeleteSource =
IterableInAppDeleteSource.deleteButton;
// WHEN Iterable.inAppManager.removeMessage is called
Iterable.inAppManager?.removeMessage(message, location, source);
// THEN corresponding method is called on MockIterableAPI with appropriate parameters
expect(MockRNIterableAPI.removeMessage).toBeCalledWith(
message.messageId,
location,
source
);
});
test('setReadForMessage_params_methodCalledWithParams', () => {
// GIVEN an in-app message
const messageDict = {
messageId: 'message1',
campaignId: 1234,
trigger: { type: IterableInAppTriggerType.immediate },
};
const message = IterableInAppMessage.fromDict(messageDict);
const read: boolean = true;
// WHEN Iterable.inAppManager.setReadForMessage is called
Iterable.inAppManager?.setReadForMessage(message, read);
// THEN corresponding method is called on MockRNIterableAPI with appropriate parameters
expect(MockRNIterableAPI.setReadForMessage).toBeCalledWith(
message.messageId,
read
);
});
test('setAutoDisplayPaused_params_methodCalledWithParams', () => {
// GIVEN paused flag
const paused: boolean = true;
// WHEN Iterable.inAppManager.setAutoDisplayPaused is called
Iterable.inAppManager?.setAutoDisplayPaused(paused);
// THEN corresponding method is called on MockRNIterableAPI with appropriate parameters
expect(MockRNIterableAPI.setAutoDisplayPaused).toBeCalledWith(paused);
});
});