-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathIterableEmbeddedCard.test.tsx
More file actions
375 lines (333 loc) · 12 KB
/
Copy pathIterableEmbeddedCard.test.tsx
File metadata and controls
375 lines (333 loc) · 12 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* eslint-disable @typescript-eslint/no-explicit-any */
import { fireEvent, render } from '@testing-library/react-native';
import { IterableEmbeddedViewType } from '../../enums/IterableEmbeddedViewType';
import { useEmbeddedView } from '../../hooks/useEmbeddedView';
import type { IterableEmbeddedMessage } from '../../types/IterableEmbeddedMessage';
import type { IterableEmbeddedMessageElementsButton } from '../../types/IterableEmbeddedMessageElementsButton';
import { IterableLogoGrey } from '../../../core/assets';
import {
IMAGE_HEIGHT,
PLACEHOLDER_IMAGE_HEIGHT,
PLACEHOLDER_IMAGE_WIDTH,
} from './IterableEmbeddedCard.styles';
import { IterableEmbeddedCard } from './IterableEmbeddedCard';
const mockHandleButtonClick = jest.fn();
const mockHandleMessageClick = jest.fn();
jest.mock('../../hooks/useEmbeddedView', () => ({
useEmbeddedView: jest.fn(),
}));
jest.mock('../../hooks/useWarnIfOutsideEmbeddedSession', () => ({
useWarnIfOutsideEmbeddedSession: jest.fn(),
}));
const mockUseEmbeddedView = useEmbeddedView as jest.MockedFunction<
typeof useEmbeddedView
>;
const defaultParsedStyles = {
backgroundColor: '#ffffff',
borderColor: '#E0DEDF',
borderCornerRadius: 8,
borderWidth: 1,
primaryBtnBackgroundColor: '#6A266D',
primaryBtnTextColor: '#ffffff',
secondaryBtnBackgroundColor: 'transparent',
secondaryBtnTextColor: '#79347F',
titleTextColor: '#3D3A3B',
bodyTextColor: '#787174',
};
function mockUseEmbeddedViewReturn(
overrides: Partial<ReturnType<typeof useEmbeddedView>> = {}
) {
mockUseEmbeddedView.mockReturnValue({
parsedStyles: defaultParsedStyles,
handleButtonClick: mockHandleButtonClick,
handleMessageClick: mockHandleMessageClick,
media: { url: null, caption: null, shouldShow: false },
...overrides,
});
}
describe('IterableEmbeddedCard', () => {
const baseMessage: IterableEmbeddedMessage = {
metadata: {
messageId: 'msg-1',
campaignId: 1,
placementId: 1,
},
elements: {
title: 'Card Title',
body: 'Card body text.',
},
};
beforeEach(() => {
jest.clearAllMocks();
mockUseEmbeddedViewReturn();
});
describe('Rendering', () => {
it('should render without crashing', () => {
const { getByText } = render(<IterableEmbeddedCard message={baseMessage} />);
expect(getByText('Card Title')).toBeTruthy();
expect(getByText('Card body text.')).toBeTruthy();
});
it('should render title and body from message.elements', () => {
const message: IterableEmbeddedMessage = {
...baseMessage,
elements: {
title: 'Custom Card Title',
body: 'Custom card body.',
},
};
const { getByText } = render(<IterableEmbeddedCard message={message} />);
expect(getByText('Custom Card Title')).toBeTruthy();
expect(getByText('Custom card body.')).toBeTruthy();
});
it('should apply parsedStyles to container and text', () => {
const customStyles = {
...defaultParsedStyles,
backgroundColor: '#000000',
titleTextColor: '#ff0000',
bodyTextColor: '#00ff00',
};
mockUseEmbeddedViewReturn({ parsedStyles: customStyles });
const { getByText, UNSAFE_getAllByType } = render(
<IterableEmbeddedCard message={baseMessage} />
);
const views = UNSAFE_getAllByType('View' as any);
const styleArray = (s: any) => (Array.isArray(s) ? s : [s]);
const container = views.find(
(v: any) =>
v.props.style &&
styleArray(v.props.style).some(
(sty: any) => sty && sty.backgroundColor === '#000000'
)
);
expect(container).toBeTruthy();
expect(styleArray(container!.props.style)).toEqual(
expect.arrayContaining([
expect.any(Object),
expect.objectContaining({
backgroundColor: '#000000',
borderColor: customStyles.borderColor,
borderRadius: customStyles.borderCornerRadius,
borderWidth: customStyles.borderWidth,
}),
])
);
const title = getByText('Card Title');
const body = getByText('Card body text.');
expect(title.props.style).toEqual(
expect.arrayContaining([
expect.any(Object),
expect.objectContaining({ color: '#ff0000' }),
])
);
expect(body.props.style).toEqual(
expect.arrayContaining([
expect.any(Object),
expect.objectContaining({ color: '#00ff00' }),
])
);
});
it('should not render button container when message has no buttons', () => {
const message: IterableEmbeddedMessage = {
...baseMessage,
elements: { ...baseMessage.elements, buttons: undefined },
};
const { queryByText } = render(<IterableEmbeddedCard message={message} />);
expect(queryByText('Primary')).toBeNull();
});
it('should not render button container when buttons array is empty', () => {
const message: IterableEmbeddedMessage = {
...baseMessage,
elements: { ...baseMessage.elements, buttons: [] },
};
const { queryByText } = render(<IterableEmbeddedCard message={message} />);
expect(queryByText('Primary')).toBeNull();
});
});
describe('Buttons', () => {
const primaryButton: IterableEmbeddedMessageElementsButton = {
id: 'btn-primary',
title: 'Primary',
action: { type: 'openUrl', data: 'https://example.com' },
};
const secondaryButton: IterableEmbeddedMessageElementsButton = {
id: 'btn-secondary',
title: 'Secondary',
};
it('should render buttons when message has buttons', () => {
const message: IterableEmbeddedMessage = {
...baseMessage,
elements: {
...baseMessage.elements,
buttons: [primaryButton, secondaryButton],
},
};
const { getByText } = render(<IterableEmbeddedCard message={message} />);
expect(getByText('Primary')).toBeTruthy();
expect(getByText('Secondary')).toBeTruthy();
});
it('should apply primary and secondary button text colors from parsedStyles', () => {
const message: IterableEmbeddedMessage = {
...baseMessage,
elements: {
...baseMessage.elements,
buttons: [primaryButton, secondaryButton],
},
};
const { getByText } = render(<IterableEmbeddedCard message={message} />);
const primaryText = getByText('Primary');
const secondaryText = getByText('Secondary');
expect(primaryText.props.style).toEqual(
expect.arrayContaining([
expect.any(Object),
expect.objectContaining({
color: defaultParsedStyles.primaryBtnTextColor,
}),
])
);
expect(secondaryText.props.style).toEqual(
expect.arrayContaining([
expect.any(Object),
expect.objectContaining({
color: defaultParsedStyles.secondaryBtnTextColor,
}),
])
);
});
it('should call handleButtonClick with correct button when button is pressed', () => {
const message: IterableEmbeddedMessage = {
...baseMessage,
elements: {
...baseMessage.elements,
buttons: [primaryButton, secondaryButton],
},
};
const { getByText } = render(<IterableEmbeddedCard message={message} />);
fireEvent.press(getByText('Primary'));
expect(mockHandleButtonClick).toHaveBeenCalledTimes(1);
expect(mockHandleButtonClick).toHaveBeenCalledWith(primaryButton);
fireEvent.press(getByText('Secondary'));
expect(mockHandleButtonClick).toHaveBeenCalledTimes(2);
expect(mockHandleButtonClick).toHaveBeenLastCalledWith(secondaryButton);
});
});
describe('Media', () => {
it('should render placeholder image when media.shouldShow is false', () => {
const { UNSAFE_getAllByType } = render(
<IterableEmbeddedCard message={baseMessage} />
);
const images = UNSAFE_getAllByType('Image' as any);
expect(images.length).toBeGreaterThan(0);
const image = images[0] as any;
const styleArray = (s: any) => (Array.isArray(s) ? s : [s]);
expect(styleArray(image.props.style)).toEqual(
expect.arrayContaining([
expect.objectContaining({
height: PLACEHOLDER_IMAGE_HEIGHT,
width: PLACEHOLDER_IMAGE_WIDTH,
}),
])
);
expect(image.props.source).toBe(IterableLogoGrey);
});
it('should render media image when media.shouldShow is true', () => {
const media = {
url: 'https://example.com/image.png',
caption: 'Card image',
shouldShow: true,
};
mockUseEmbeddedViewReturn({ media });
const { UNSAFE_getAllByType } = render(
<IterableEmbeddedCard message={baseMessage} />
);
const images = UNSAFE_getAllByType('Image' as any);
expect(images.length).toBeGreaterThan(0);
const image = images[0] as any;
expect(image.props.source.uri).toBe(media.url);
const styleArray = (s: any) => (Array.isArray(s) ? s : [s]);
expect(styleArray(image.props.style)).toEqual(
expect.arrayContaining([
expect.objectContaining({
height: IMAGE_HEIGHT,
}),
])
);
});
});
describe('Message click', () => {
it('should call handleMessageClick when card is pressed', () => {
const { getByText } = render(<IterableEmbeddedCard message={baseMessage} />);
fireEvent.press(getByText('Card Title'));
expect(mockHandleMessageClick).toHaveBeenCalledTimes(1);
});
});
describe('useEmbeddedView integration', () => {
it('should call useEmbeddedView with Card viewType and props', () => {
const config = { backgroundColor: '#abc' } as any;
const onButtonClick = jest.fn();
const onMessageClick = jest.fn();
render(
<IterableEmbeddedCard
message={baseMessage}
config={config}
onButtonClick={onButtonClick}
onMessageClick={onMessageClick}
/>
);
expect(mockUseEmbeddedView).toHaveBeenCalledTimes(1);
expect(mockUseEmbeddedView).toHaveBeenCalledWith(
IterableEmbeddedViewType.Card,
{
message: baseMessage,
config,
onButtonClick,
onMessageClick,
}
);
});
});
describe('Edge cases', () => {
it('should handle message with missing elements', () => {
const message: IterableEmbeddedMessage = {
metadata: baseMessage.metadata,
elements: undefined,
};
const { queryByText } = render(<IterableEmbeddedCard message={message} />);
expect(queryByText('Card Title')).toBeNull();
expect(queryByText('Card body text.')).toBeNull();
});
it('should handle message with empty title and body without throwing', () => {
const message: IterableEmbeddedMessage = {
...baseMessage,
elements: { title: '', body: '' },
};
const { getAllByText } = render(
<IterableEmbeddedCard message={message} />
);
const emptyTextNodes = getAllByText('');
expect(emptyTextNodes.length).toBeGreaterThanOrEqual(1);
});
it('should render multiple buttons and call handleButtonClick with correct button for each', () => {
const message: IterableEmbeddedMessage = {
...baseMessage,
elements: {
...baseMessage.elements,
buttons: [
{ id: 'unique-id-1', title: 'First' },
{ id: 'unique-id-2', title: 'Second' },
],
},
};
const { getByText } = render(<IterableEmbeddedCard message={message} />);
expect(getByText('First')).toBeTruthy();
expect(getByText('Second')).toBeTruthy();
fireEvent.press(getByText('First'));
expect(mockHandleButtonClick).toHaveBeenCalledWith(
expect.objectContaining({ id: 'unique-id-1', title: 'First' })
);
fireEvent.press(getByText('Second'));
expect(mockHandleButtonClick).toHaveBeenLastCalledWith(
expect.objectContaining({ id: 'unique-id-2', title: 'Second' })
);
});
});
});