Skip to content

Commit 605d28c

Browse files
committed
test: add unit tests for IterableInAppManager
1 parent 553ee16 commit 605d28c

File tree

1 file changed

+396
-0
lines changed

1 file changed

+396
-0
lines changed
Lines changed: 396 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,396 @@
1+
import { IterableInAppDeleteSource, IterableInAppLocation } from '../enums';
2+
import { IterableInAppManager } from './IterableInAppManager';
3+
import { IterableInAppMessage } from './IterableInAppMessage';
4+
5+
describe('IterableInAppManager', () => {
6+
let manager: IterableInAppManager;
7+
8+
beforeEach(() => {
9+
manager = new IterableInAppManager();
10+
});
11+
12+
describe('constructor', () => {
13+
it('should create an instance', () => {
14+
// WHEN creating a new manager
15+
const newManager = new IterableInAppManager();
16+
17+
// THEN it should be an instance of IterableInAppManager
18+
expect(newManager).toBeInstanceOf(IterableInAppManager);
19+
});
20+
});
21+
22+
describe('method signatures', () => {
23+
it('should have getMessages method', () => {
24+
// THEN the manager should have getMessages method
25+
expect(typeof manager.getMessages).toBe('function');
26+
});
27+
28+
it('should have getInboxMessages method', () => {
29+
// THEN the manager should have getInboxMessages method
30+
expect(typeof manager.getInboxMessages).toBe('function');
31+
});
32+
33+
it('should have showMessage method', () => {
34+
// THEN the manager should have showMessage method
35+
expect(typeof manager.showMessage).toBe('function');
36+
});
37+
38+
it('should have removeMessage method', () => {
39+
// THEN the manager should have removeMessage method
40+
expect(typeof manager.removeMessage).toBe('function');
41+
});
42+
43+
it('should have setReadForMessage method', () => {
44+
// THEN the manager should have setReadForMessage method
45+
expect(typeof manager.setReadForMessage).toBe('function');
46+
});
47+
48+
it('should have getHtmlContentForMessage method', () => {
49+
// THEN the manager should have getHtmlContentForMessage method
50+
expect(typeof manager.getHtmlContentForMessage).toBe('function');
51+
});
52+
53+
it('should have setAutoDisplayPaused method', () => {
54+
// THEN the manager should have setAutoDisplayPaused method
55+
expect(typeof manager.setAutoDisplayPaused).toBe('function');
56+
});
57+
});
58+
59+
describe('getInboxMessages', () => {
60+
it('should be a function', () => {
61+
// THEN the method should be a function
62+
expect(typeof manager.getInboxMessages).toBe('function');
63+
});
64+
65+
it('should have the correct method name', () => {
66+
// THEN the method should be named getInboxMessages
67+
expect(manager.getInboxMessages.name).toBe('getInboxMessages');
68+
});
69+
70+
it('should be a different method from getMessages', () => {
71+
// THEN getInboxMessages should be different from getMessages
72+
expect(manager.getInboxMessages).not.toBe(manager.getMessages);
73+
expect(manager.getInboxMessages.name).not.toBe(manager.getMessages.name);
74+
});
75+
76+
it('should return a Promise when called', async () => {
77+
// WHEN calling getInboxMessages
78+
const result = manager.getInboxMessages();
79+
80+
// THEN it should return a Promise
81+
expect(result).toBeInstanceOf(Promise);
82+
});
83+
84+
it('should return empty array when no inbox messages exist', async () => {
85+
// GIVEN no messages are set in the mock
86+
const { MockRNIterableAPI } = await import('../../__mocks__/MockRNIterableAPI');
87+
MockRNIterableAPI.setMessages([]);
88+
89+
// WHEN calling getInboxMessages
90+
const result = await manager.getInboxMessages();
91+
92+
// THEN it should return empty array
93+
expect(result).toEqual([]);
94+
});
95+
96+
it('should return only inbox messages when mixed messages exist', async () => {
97+
// GIVEN mixed messages with some marked for inbox
98+
const { MockRNIterableAPI } = await import('../../__mocks__/MockRNIterableAPI');
99+
const mockMessages = [
100+
{ messageId: 'msg1', campaignId: 1, saveToInbox: true } as IterableInAppMessage,
101+
{ messageId: 'msg2', campaignId: 2, saveToInbox: false } as IterableInAppMessage,
102+
{ messageId: 'msg3', campaignId: 3, saveToInbox: true } as IterableInAppMessage,
103+
];
104+
MockRNIterableAPI.setMessages(mockMessages);
105+
106+
// WHEN calling getInboxMessages
107+
const result = await manager.getInboxMessages();
108+
109+
// THEN it should return only inbox messages
110+
expect(result).toHaveLength(2);
111+
expect(result?.[0]?.messageId).toBe('msg1');
112+
expect(result?.[1]?.messageId).toBe('msg3');
113+
});
114+
115+
it('should return all messages when all are marked for inbox', async () => {
116+
// GIVEN all messages are marked for inbox
117+
const { MockRNIterableAPI } = await import('../../__mocks__/MockRNIterableAPI');
118+
const mockMessages = [
119+
{ messageId: 'msg1', campaignId: 1, saveToInbox: true } as IterableInAppMessage,
120+
{ messageId: 'msg2', campaignId: 2, saveToInbox: true } as IterableInAppMessage,
121+
];
122+
MockRNIterableAPI.setMessages(mockMessages);
123+
124+
// WHEN calling getInboxMessages
125+
const result = await manager.getInboxMessages();
126+
127+
// THEN it should return all messages
128+
expect(result).toHaveLength(2);
129+
expect(result).toEqual(mockMessages);
130+
});
131+
132+
it('should handle undefined messages gracefully', async () => {
133+
// GIVEN messages are undefined
134+
const { MockRNIterableAPI } = await import('../../__mocks__/MockRNIterableAPI');
135+
MockRNIterableAPI.setMessages(undefined as unknown as IterableInAppMessage[]);
136+
137+
// WHEN calling getInboxMessages
138+
const result = await manager.getInboxMessages();
139+
140+
// THEN it should return empty array
141+
expect(result).toEqual([]);
142+
});
143+
});
144+
145+
describe('showMessage parameters', () => {
146+
it('should accept IterableInAppMessage and boolean parameters', () => {
147+
// GIVEN a mock message
148+
const mockMessage = {
149+
messageId: 'test-message-id',
150+
campaignId: 123,
151+
} as IterableInAppMessage;
152+
153+
// WHEN calling showMessage with valid parameters
154+
// THEN it should not throw an error
155+
expect(() => {
156+
manager.showMessage(mockMessage, true);
157+
manager.showMessage(mockMessage, false);
158+
}).not.toThrow();
159+
});
160+
});
161+
162+
describe('removeMessage parameters', () => {
163+
it('should accept IterableInAppMessage, IterableInAppLocation, and IterableInAppDeleteSource parameters', () => {
164+
// GIVEN a mock message
165+
const mockMessage = {
166+
messageId: 'test-message-id',
167+
campaignId: 123,
168+
} as IterableInAppMessage;
169+
170+
// WHEN calling removeMessage with valid parameters
171+
// THEN it should not throw an error
172+
expect(() => {
173+
manager.removeMessage(
174+
mockMessage,
175+
IterableInAppLocation.inApp,
176+
IterableInAppDeleteSource.deleteButton
177+
);
178+
manager.removeMessage(
179+
mockMessage,
180+
IterableInAppLocation.inbox,
181+
IterableInAppDeleteSource.inboxSwipe
182+
);
183+
manager.removeMessage(
184+
mockMessage,
185+
IterableInAppLocation.inApp,
186+
IterableInAppDeleteSource.unknown
187+
);
188+
}).not.toThrow();
189+
});
190+
});
191+
192+
describe('setReadForMessage parameters', () => {
193+
it('should accept IterableInAppMessage and boolean parameters', () => {
194+
// GIVEN a mock message
195+
const mockMessage = {
196+
messageId: 'test-message-id',
197+
campaignId: 123,
198+
} as IterableInAppMessage;
199+
200+
// WHEN calling setReadForMessage with valid parameters
201+
// THEN it should not throw an error
202+
expect(() => {
203+
manager.setReadForMessage(mockMessage, true);
204+
manager.setReadForMessage(mockMessage, false);
205+
}).not.toThrow();
206+
});
207+
});
208+
209+
describe('getHtmlContentForMessage', () => {
210+
it('should be a function', () => {
211+
// THEN the method should be a function
212+
expect(typeof manager.getHtmlContentForMessage).toBe('function');
213+
});
214+
215+
it('should return a Promise when called', async () => {
216+
// GIVEN a mock message
217+
const mockMessage = {
218+
messageId: 'test-message-id',
219+
campaignId: 123,
220+
} as IterableInAppMessage;
221+
222+
// WHEN calling getHtmlContentForMessage
223+
const result = manager.getHtmlContentForMessage(mockMessage);
224+
225+
// THEN it should return a Promise
226+
expect(result).toBeInstanceOf(Promise);
227+
});
228+
229+
it('should return HTML content for a message', async () => {
230+
// GIVEN a mock message
231+
const mockMessage = {
232+
messageId: 'test-message-id',
233+
campaignId: 123,
234+
} as IterableInAppMessage;
235+
236+
// WHEN calling getHtmlContentForMessage
237+
const result = await manager.getHtmlContentForMessage(mockMessage);
238+
239+
// THEN it should return HTML content
240+
expect(result).toEqual({
241+
edgeInsets: { top: 10, left: 20, bottom: 30, right: 40 },
242+
html: '<div>Mock HTML content for message test-message-id</div>',
243+
});
244+
});
245+
246+
it('should handle different message IDs', async () => {
247+
// GIVEN different mock messages
248+
const message1 = { messageId: 'msg1', campaignId: 1 } as IterableInAppMessage;
249+
const message2 = { messageId: 'msg2', campaignId: 2 } as IterableInAppMessage;
250+
251+
// WHEN calling getHtmlContentForMessage with different messages
252+
const result1 = await manager.getHtmlContentForMessage(message1);
253+
const result2 = await manager.getHtmlContentForMessage(message2);
254+
255+
// THEN it should return different HTML content for each message
256+
expect(result1).toEqual({
257+
edgeInsets: { top: 10, left: 20, bottom: 30, right: 40 },
258+
html: '<div>Mock HTML content for message msg1</div>',
259+
});
260+
expect(result2).toEqual({
261+
edgeInsets: { top: 10, left: 20, bottom: 30, right: 40 },
262+
html: '<div>Mock HTML content for message msg2</div>',
263+
});
264+
});
265+
});
266+
267+
describe('setAutoDisplayPaused parameters', () => {
268+
it('should accept boolean parameter', () => {
269+
// WHEN calling setAutoDisplayPaused with valid parameters
270+
// THEN it should not throw an error
271+
expect(() => {
272+
manager.setAutoDisplayPaused(true);
273+
manager.setAutoDisplayPaused(false);
274+
}).not.toThrow();
275+
});
276+
});
277+
278+
describe('enum values', () => {
279+
it('should have correct IterableInAppLocation enum values', () => {
280+
// THEN the enum values should be correct
281+
expect(IterableInAppLocation.inApp).toBe(0);
282+
expect(IterableInAppLocation.inbox).toBe(1);
283+
});
284+
285+
it('should have correct IterableInAppDeleteSource enum values', () => {
286+
// THEN the enum values should be correct
287+
expect(IterableInAppDeleteSource.inboxSwipe).toBe(0);
288+
expect(IterableInAppDeleteSource.deleteButton).toBe(1);
289+
expect(IterableInAppDeleteSource.unknown).toBe(100);
290+
});
291+
});
292+
293+
describe('method return types', () => {
294+
it('should return Promise for async methods', () => {
295+
// GIVEN a mock message
296+
const mockMessage = {
297+
messageId: 'test-message-id',
298+
campaignId: 123,
299+
} as IterableInAppMessage;
300+
301+
// WHEN calling async methods that don't require native modules
302+
const showMessagePromise = manager.showMessage(mockMessage, true);
303+
304+
// THEN they should return Promises
305+
expect(showMessagePromise).toBeInstanceOf(Promise);
306+
});
307+
308+
it('should return void for sync methods', () => {
309+
// GIVEN a mock message
310+
const mockMessage = {
311+
messageId: 'test-message-id',
312+
campaignId: 123,
313+
} as IterableInAppMessage;
314+
315+
// WHEN calling sync methods
316+
const removeMessageResult = manager.removeMessage(
317+
mockMessage,
318+
IterableInAppLocation.inApp,
319+
IterableInAppDeleteSource.deleteButton
320+
);
321+
const setReadResult = manager.setReadForMessage(mockMessage, true);
322+
const setAutoDisplayResult = manager.setAutoDisplayPaused(true);
323+
324+
// THEN they should return undefined (void)
325+
expect(removeMessageResult).toBeUndefined();
326+
expect(setReadResult).toBeUndefined();
327+
expect(setAutoDisplayResult).toBeUndefined();
328+
});
329+
});
330+
331+
describe('error handling', () => {
332+
it('should handle null message parameters', () => {
333+
// WHEN calling methods with null message
334+
// THEN it should throw appropriate errors
335+
expect(() => {
336+
manager.removeMessage(null as unknown as IterableInAppMessage, IterableInAppLocation.inApp, IterableInAppDeleteSource.unknown);
337+
}).toThrow();
338+
339+
expect(() => {
340+
manager.setReadForMessage(null as unknown as IterableInAppMessage, true);
341+
}).toThrow();
342+
343+
expect(() => {
344+
manager.getHtmlContentForMessage(null as unknown as IterableInAppMessage);
345+
}).toThrow();
346+
});
347+
348+
it('should handle undefined message parameters', () => {
349+
// WHEN calling methods with undefined message
350+
// THEN it should throw appropriate errors
351+
expect(() => {
352+
manager.removeMessage(undefined as unknown as IterableInAppMessage, IterableInAppLocation.inApp, IterableInAppDeleteSource.unknown);
353+
}).toThrow();
354+
355+
expect(() => {
356+
manager.setReadForMessage(undefined as unknown as IterableInAppMessage, true);
357+
}).toThrow();
358+
359+
expect(() => {
360+
manager.getHtmlContentForMessage(undefined as unknown as IterableInAppMessage);
361+
}).toThrow();
362+
});
363+
});
364+
365+
describe('parameter validation', () => {
366+
it('should handle invalid enum values gracefully', () => {
367+
// GIVEN a mock message
368+
const mockMessage = {
369+
messageId: 'test-message-id',
370+
campaignId: 123,
371+
} as IterableInAppMessage;
372+
373+
// WHEN calling removeMessage with invalid enum values
374+
// THEN it should not throw an error (values are passed through)
375+
expect(() => {
376+
manager.removeMessage(mockMessage, 999 as IterableInAppLocation, 888 as IterableInAppDeleteSource);
377+
}).not.toThrow();
378+
});
379+
380+
it('should handle invalid boolean parameters', () => {
381+
// GIVEN a mock message
382+
const mockMessage = {
383+
messageId: 'test-message-id',
384+
campaignId: 123,
385+
} as IterableInAppMessage;
386+
387+
// WHEN calling methods with invalid boolean parameters
388+
// THEN it should not throw an error (values are passed through)
389+
expect(() => {
390+
manager.showMessage(mockMessage, 'true' as unknown as boolean);
391+
manager.setReadForMessage(mockMessage, 'false' as unknown as boolean);
392+
manager.setAutoDisplayPaused('true' as unknown as boolean);
393+
}).not.toThrow();
394+
});
395+
});
396+
});

0 commit comments

Comments
 (0)