@capillarytech/creatives-library 8.0.314 → 8.0.316-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,365 +1,307 @@
1
- import React from 'react';
2
- import Bugsnag from '@bugsnag/js';
3
- import { FormattedMessage } from 'react-intl';
4
- import "@testing-library/jest-dom";
1
+ /**
2
+ * Test suite for common utility functions
3
+ */
5
4
  import {
6
- filterTags,
7
- getTreeStructuredTags,
8
- handleInjectedData,
9
- intlKeyGenerator,
10
- bytes2Size,
11
5
  getUserNameById,
6
+ getMergedUserList,
7
+ removeLinksFromHtmlContent,
12
8
  iframePreviewAdjustWidth,
13
- getDecodedFileName,
14
- createQueryString,
15
- getMergedUserList
16
- } from "../common";
17
- import * as mockdata from "./common.mockdata";
18
-
19
- describe("getTreeStructuredTags test", () => {
20
- it("test for getTreeStructuredTags when tagsList is not empty", () => {
21
- expect(getTreeStructuredTags({ tagsList: mockdata.tagsList })).toEqual(mockdata.output1);
22
- });
23
- it("test for getTreeStructuredTags when tagsList is empty", () => {
24
- expect(getTreeStructuredTags({ tagsList: [] })).toEqual([]);
25
- });
9
+ } from '../common';
10
+
11
+ // Mock the Auth module
12
+ jest.mock('@capillarytech/cap-ui-utils', () => ({
13
+ Auth: {
14
+ hasFeatureAccess: jest.fn(),
15
+ },
16
+ }));
17
+
18
+ describe('common Utility Functions', () => {
19
+ describe('getUserNameById', () => {
20
+ it('should return full name when user found', () => {
21
+ const userList = [
22
+ { userId: '1', firstName: 'John', lastName: 'Doe' },
23
+ { userId: '2', firstName: 'Jane', lastName: 'Smith' },
24
+ ];
25
+
26
+ const result = getUserNameById('1', userList);
27
+
28
+ expect(result).toBe('John Doe');
29
+ });
26
30
 
27
- it("test for getTreeStructuredTags when incentive attached is BADGES", () => {
28
- expect(getTreeStructuredTags({ tagsList: mockdata.badgesTags, offerDetails: mockdata.offer })).toEqual(mockdata.expectedOffer);
29
- });
30
- it("test for filterTags", () => {
31
- expect(filterTags(mockdata.tagsToFilter, mockdata.tagsList)).toEqual([mockdata.tagsList[1]]);
32
- });
33
- it("test for filterTags when tagsToFilter and tagsList is empty", () => {
34
- expect(filterTags([], [])).toEqual([]);
35
- });
36
- it("test for getTreeStructuredTags when incentive attached is offer", () => {
37
- expect(getTreeStructuredTags({ tagsList: mockdata.OfferTag, offerDetails: [{ id: '12', couponName: 'mac' }] })).toEqual(mockdata.offerOutput);
38
- });
39
- });
31
+ it('should return empty string when user not found', () => {
32
+ const userList = [
33
+ { userId: '1', firstName: 'John', lastName: 'Doe' },
34
+ ];
40
35
 
41
- describe("getUserNameById test", () => {
42
- it("test for username is available or not", () => {
43
- const data = [
44
- {
45
- userId: 1,
46
- firstName: 'test1',
47
- lastName: 'test2',
48
-
49
- },
50
- {
51
- userId: 2,
52
- firstName: 'test3',
53
- lastName: 'test4',
54
-
55
- },
56
- ];
57
- const expected = 'test1 test2';
58
- expect(getUserNameById(1, data)).toEqual(expected);
59
- });
60
- });
36
+ const result = getUserNameById('999', userList);
61
37
 
62
- describe("handleInjectedData", () => {
63
- it("replaces name with intl key for top level tag", () => {
64
- const data = {
65
- tag1: {
66
- name: "Registration Fields",
67
- },
68
- };
69
-
70
- const result = handleInjectedData(data, "scope");
71
- const intlKey = intlKeyGenerator("scope");
72
- expect(intlKey).toEqual('scope');
73
- expect(result.tag1.name).toEqual(<FormattedMessage defaultMessage="Registration Fields" id="scope.registration_fields" values={{}} />);
74
- });
38
+ expect(result).toBe('');
39
+ });
75
40
 
76
- it("adds tagType for Registration custom fields", () => {
77
- const data = {
78
- tag1: {
79
- name: "Registration custom fields",
80
- },
81
- };
82
-
83
- const result = handleInjectedData(data, "scope");
84
- const intlKey = intlKeyGenerator();
85
- expect(intlKey).toEqual('');
86
- expect(result.tag1.name).toEqual(
87
- <FormattedMessage defaultMessage="Registration custom fields" id="scope.registration_custom_fields_name.CustomTagMessage" values={{}} />
88
- );
89
- });
41
+ it('should handle empty user list', () => {
42
+ const result = getUserNameById('1', []);
90
43
 
91
- it("adds tagType for Customer extended fields fields", () => {
92
- const data = {
93
- tag1: {
94
- name: "Customer extended fields",
95
- },
96
- };
44
+ expect(result).toBe('');
45
+ });
97
46
 
98
- const result = handleInjectedData(data, "scope");
47
+ it('should handle multiple users with same ID (returns last match)', () => {
48
+ const userList = [
49
+ { userId: '1', firstName: 'John', lastName: 'Doe' },
50
+ { userId: '1', firstName: 'Jane', lastName: 'Smith' },
51
+ ];
99
52
 
100
- expect(result.tag1.name).toEqual(
101
- <FormattedMessage defaultMessage="Customer extended fields" id="scope.customer_extended_fields_name.ExtendedTagMessage" values={{}} />
102
- );
103
- });
104
- it("adds tagType for Customer extended fields fields", () => {
105
- const data = {
106
- tag1: {
107
- },
108
- };
53
+ const result = getUserNameById('1', userList);
109
54
 
110
- const result = handleInjectedData(data, "scope");
55
+ // Should return the last match due to forEach iteration
56
+ expect(result).toBe('Jane Smith');
57
+ });
111
58
 
112
- expect(result.tag1.name).toEqual(undefined);
113
- });
59
+ it('should handle users with empty names', () => {
60
+ const userList = [
61
+ { userId: '1', firstName: '', lastName: '' },
62
+ ];
114
63
 
115
- it("adds tagType for Customer with extended fields fields", () => {
116
- const data = {
117
- tag1: {
118
- name: "mahaRaja",
119
- subtag1: {
120
- name: "First Name",
121
- },
122
- },
123
- };
64
+ const result = getUserNameById('1', userList);
124
65
 
125
- const result = handleInjectedData(data, "scope");
66
+ expect(result).toBe(' ');
67
+ });
126
68
 
127
- expect(result.tag1.subtag1.name).toEqual("First Name");
128
- });
129
- it("replaces subtag name with intl key", () => {
130
- const data = {
131
- tag1: {
132
- subtags: {
133
- subtag1: {
134
- name: "First Name",
135
- hasOwnProperty: jest.fn(),
136
- },
137
- hasOwnProperty: jest.fn(),
138
- },
139
- },
140
- };
141
-
142
- const result = handleInjectedData(data, "scope");
143
-
144
- expect(result.tag1.subtags.subtag1.name).toEqual("First Name");
145
- });
69
+ it('should handle users with only first name', () => {
70
+ const userList = [
71
+ { userId: '1', firstName: 'John', lastName: '' },
72
+ ];
146
73
 
147
- it("replaces subtag name with intl key test", () => {
148
- const data = {
149
- tag1: {
150
- subtags: {
151
- subtag1: {
152
- name: "First Name",
153
- hasOwnProperty: jest.fn(),
154
- },
155
- },
156
- },
157
- hasOwnProperty: jest.fn(),
158
- };
159
-
160
- const result = handleInjectedData(data, "scope");
161
-
162
- expect(result.tag1.subtags.subtag1.name).toEqual("First Name");
163
- });
74
+ const result = getUserNameById('1', userList);
164
75
 
165
- it("replaces subtag desc with intl key", () => {
166
- const data = {
167
- tag1: {
168
- subtags: {
169
- subtag1: {
170
- desc: "Enter your first name",
171
- },
172
- },
173
- },
174
- };
175
-
176
- const result = handleInjectedData(data, "scope");
177
-
178
- expect(result.tag1.subtags.subtag1.desc).toEqual(
179
- <FormattedMessage defaultMessage="Enter your first name" id="scope.enter_your_first_name" values={{}} />
180
- );
181
- });
182
- it("replaces subtag name desc with intl key", () => {
183
- const data = {
184
- tag1: {
185
- subtags: {
186
- subtag1: {
187
- name: "mahaRaja",
188
- desc: "Enter your first name",
189
- },
190
- },
191
- },
192
- };
193
-
194
- const result = handleInjectedData(data, "scope");
195
-
196
- expect(result.tag1.subtags.subtag1.desc).toEqual(
197
- <FormattedMessage defaultMessage="Enter your first name" id="scope.enter_your_first_name" values={{}} />
198
- );
76
+ expect(result).toContain('John');
77
+ });
199
78
  });
200
- });
201
79
 
202
- describe('bytes2Size', () => {
203
- it('should convert bytes to the correct size string', () => {
204
- // Test cases with different byte values
205
- expect(bytes2Size(0)).toBe('0 Bytes');
206
- expect(bytes2Size(1023)).toBe('1023 Bytes');
207
- expect(bytes2Size(1024)).toBe('1 KB');
208
- expect(bytes2Size(1048576)).toBe('1 MB');
209
- expect(bytes2Size(1099511627776)).toBe('1 TB');
210
- expect(bytes2Size(1125899906842624)).toBe('1 PB');
211
- expect(bytes2Size(1152921504606846976)).toBe('1 EB');
212
- expect(bytes2Size(1180591620717411303424)).toBe('1 ZB');
213
- expect(bytes2Size(Number.MAX_SAFE_INTEGER)).toBe('8 PB'); // Test with the maximum safe integer value
214
- });
80
+ describe('getMergedUserList', () => {
81
+ it('should merge all user lists when isCapUser is true', () => {
82
+ const userList = {
83
+ capUsers: [
84
+ { userId: '1', name: 'CAP User' },
85
+ ],
86
+ otherUsers: [
87
+ { userId: '2', name: 'Other User' },
88
+ ],
89
+ };
215
90
 
216
- it('should handle negative decimals', () => {
217
- // Test with negative decimal values
218
- expect(bytes2Size(1024, -1)).toBe('1 KB'); // Decimals < 0 should round to 0
219
- expect(bytes2Size(1024, -2)).toBe('1 KB');
220
- expect(bytes2Size(1024, -3)).toBe('1 KB');
221
- });
222
- });
91
+ const result = getMergedUserList(userList, true);
223
92
 
224
- describe('iframeAdjustWidth', () => {
225
- it("should Add the width in iframe image", () => {
226
- const data = [
227
- <head>
228
- <meta httpEquiv="Content-Type" content="text/html; charset=utf-8">
229
- <meta name="viewport" content="width=device-width">
230
- </meta>
231
- </meta>
232
- </head>,
233
-
234
- ];
235
- expect(iframePreviewAdjustWidth(data)).toBe("<html><head></head><body>[object Object]</body></html>");
236
- });
237
- });
93
+ expect(result).toHaveLength(2);
94
+ expect(result[0].name).toBe('CAP User');
95
+ expect(result[1].name).toBe('Other User');
96
+ });
238
97
 
239
- describe('getDecodedFileName function', () => {
240
- it('Returns an empty string when templateName is undefined', () => {
241
- expect(getDecodedFileName()).toEqual('');
242
- });
98
+ it('should exclude capUsers when isCapUser is false', () => {
99
+ const userList = {
100
+ capUsers: [
101
+ { userId: '1', name: 'CAP User' },
102
+ ],
103
+ otherUsers: [
104
+ { userId: '2', name: 'Other User' },
105
+ ],
106
+ };
243
107
 
244
- it('Returns the decoded filename from encoded filename', () => {
245
- expect(getDecodedFileName('%24Test.jpeg')).toEqual('$Test.jpeg');
246
- expect(getDecodedFileName('cap%24.logo.%23.jpg')).toEqual('cap$.logo.#.jpg');
247
- expect(getDecodedFileName("%24L_i%40n%26e__%60AL%60L%2BI'ma_g__e%2Bco_pyAI____Copy____Copy_wek1SeS0.jpg")).toEqual("$L_i@n&e__`AL`L+I'ma_g__e+co_pyAI____Copy____Copy_wek1SeS0.jpg");
248
- expect(getDecodedFileName('test.jpeg')).toEqual('test.jpeg');
249
- });
108
+ const result = getMergedUserList(userList, false);
250
109
 
251
- it('Returns the original filename when decoding fails due to URIError', () => {
252
- const fileName = '@Test%ZZ.jpeg';
253
- expect(getDecodedFileName(fileName)).toEqual(fileName);
254
- });
110
+ expect(result).toHaveLength(1);
111
+ expect(result[0].name).toBe('Other User');
112
+ });
113
+
114
+ it('should handle empty user lists', () => {
115
+ const userList = {};
255
116
 
256
- it('Returns the original filename and notifies Bugsnag for other decoding errors', () => {
257
- // Mocking Bugsnag methods for testing
258
- const originalLeaveBreadcrumb = Bugsnag.leaveBreadcrumb;
259
- const originalNotify = Bugsnag.notify;
260
- Bugsnag.leaveBreadcrumb = jest.fn();
261
- Bugsnag.notify = jest.fn((error, callback) => {
262
- const event = {};
263
- callback(event);
264
- expect(event.severity).toEqual("error");
117
+ const result = getMergedUserList(userList, true);
118
+
119
+ expect(result).toHaveLength(0);
265
120
  });
266
- const fileName = '@Test%20ZZ.jpg';
267
- const error = new Error('Some other decoding error');
268
- // Mocking decodeURIComponent to throw the error
269
- global.decodeURIComponent = jest.fn(() => { throw error; });
270
-
271
- expect(getDecodedFileName(fileName)).toEqual(fileName);
272
- expect(Bugsnag.notify).toHaveBeenCalled();
273
- // Restoring original Bugsnag methods
274
- Bugsnag.leaveBreadcrumb = originalLeaveBreadcrumb;
275
- Bugsnag.notify = originalNotify;
276
- });
277
- });
278
121
 
279
- describe('createQueryString', () => {
280
- it('should return a query string for a valid object with multiple key-value pairs', () => {
281
- const input = { name: 'John', age: 30, city: 'New York' };
282
- const result = createQueryString(input);
283
- expect(result).toBe('?name=John&age=30&city=New%20York');
284
- });
122
+ it('should handle empty nested arrays', () => {
123
+ const userList = {
124
+ capUsers: [],
125
+ otherUsers: [],
126
+ };
285
127
 
286
- it('should return an empty string for an empty object', () => {
287
- const input = {};
288
- const result = createQueryString(input);
289
- expect(result).toBe('');
290
- });
128
+ const result = getMergedUserList(userList, true);
291
129
 
292
- it('should return an empty string for null or undefined input', () => {
293
- expect(createQueryString(null)).toBe('');
294
- expect(createQueryString(undefined)).toBe('');
295
- });
130
+ expect(result).toHaveLength(0);
131
+ });
296
132
 
297
- it('should return an empty string for non-object input', () => {
298
- expect(createQueryString("string")).toBe('');
299
- expect(createQueryString(123)).toBe('');
300
- expect(createQueryString(true)).toBe('');
301
- });
133
+ it('should handle single user list', () => {
134
+ const userList = {
135
+ users: [
136
+ { userId: '1', name: 'User 1' },
137
+ { userId: '2', name: 'User 2' },
138
+ ],
139
+ };
302
140
 
303
- it('should encode special characters in keys and values', () => {
304
- const input = { 'key with spaces': 'value with spaces', 'special&characters': 'value@#' };
305
- const result = createQueryString(input);
306
- expect(result).toBe('?key%20with%20spaces=value%20with%20spaces&special%26characters=value%40%23');
307
- });
141
+ const result = getMergedUserList(userList, true);
308
142
 
309
- it('should handle null or undefined values in query parameters', () => {
310
- const input = { name: 'John', age: null, city: undefined };
311
- const result = createQueryString(input);
312
- expect(result).toBe('?name=John&age=null&city=undefined');
313
- });
143
+ expect(result).toHaveLength(2);
144
+ });
314
145
 
315
- it('should return query string for a single key-value pair', () => {
316
- const input = { name: 'John' };
317
- const result = createQueryString(input);
318
- expect(result).toBe('?name=John');
319
- });
320
- });
321
- describe('getMergedUserList', () => {
322
- it('should merge user list when isCapUser is true', () => {
323
- const userList = {
324
- capUsers: [{ id: 1, name: 'Cap User 1' }],
325
- otherUsers: [{ id: 2, name: 'Other User 1' }],
326
- };
327
-
328
- const result = getMergedUserList(userList, true);
329
-
330
- expect(result).toEqual([
331
- { id: 1, name: 'Cap User 1' },
332
- { id: 2, name: 'Other User 1' },
333
- ]);
334
- });
146
+ it('should default isCapUser to true', () => {
147
+ const userList = {
148
+ capUsers: [{ userId: '1', name: 'CAP User' }],
149
+ otherUsers: [{ userId: '2', name: 'Other User' }],
150
+ };
335
151
 
336
- it('should merge user list excluding capUsers when isCapUser is false', () => {
337
- const userList = {
338
- capUsers: [{ id: 1, name: 'Cap User 1' }],
339
- otherUsers: [{ id: 2, name: 'Other User 1' }],
340
- };
341
-
342
- const result = getMergedUserList(userList, false);
152
+ const result = getMergedUserList(userList);
343
153
 
344
- expect(result).toEqual([{ id: 2, name: 'Other User 1' }]);
154
+ expect(result).toHaveLength(2);
155
+ });
345
156
  });
346
157
 
347
- it('should handle empty userList', () => {
348
- const userList = {};
349
-
350
- const result = getMergedUserList(userList, true);
158
+ describe('removeLinksFromHtmlContent', () => {
159
+ it('should remove href attributes from anchor tags', () => {
160
+ const content = '<a href="https://example.com">Click here</a>';
161
+
162
+ const result = removeLinksFromHtmlContent(content);
163
+
164
+ expect(result).toContain('<a>');
165
+ expect(result).not.toContain('href="https://example.com"');
166
+ });
167
+
168
+ it('should preserve anchor tag content', () => {
169
+ const content = '<a href="https://example.com">Click here</a>';
170
+
171
+ const result = removeLinksFromHtmlContent(content);
351
172
 
352
- expect(result).toEqual([]);
173
+ expect(result).toContain('Click here');
174
+ });
175
+
176
+ it('should handle multiple links', () => {
177
+ const content = '<a href="url1">Link 1</a><a href="url2">Link 2</a>';
178
+
179
+ const result = removeLinksFromHtmlContent(content);
180
+
181
+ expect(result).toContain('<a>');
182
+ expect(result).toContain('Link 1');
183
+ expect(result).toContain('Link 2');
184
+ });
185
+
186
+ it('should handle complex HTML with nested content', () => {
187
+ const content = '<div><p><a href="url">Link</a></p></div>';
188
+
189
+ const result = removeLinksFromHtmlContent(content);
190
+
191
+ expect(result).toContain('<a>');
192
+ expect(result).toContain('<p>');
193
+ expect(result).toContain('Link');
194
+ });
195
+
196
+ it('should handle content without links', () => {
197
+ const content = '<p>No links here</p>';
198
+
199
+ const result = removeLinksFromHtmlContent(content);
200
+
201
+ expect(result).toContain('No links here');
202
+ expect(result).toContain('<p>');
203
+ });
204
+
205
+ it('should handle anchor tags with multiple attributes', () => {
206
+ const content = '<a href="url" title="Title" class="link">Link</a>';
207
+
208
+ const result = removeLinksFromHtmlContent(content);
209
+
210
+ expect(result).toContain('<a>');
211
+ expect(result).not.toContain('href="url"');
212
+ });
213
+
214
+ it('should handle empty anchor tags', () => {
215
+ const content = '<a href="url"></a>';
216
+
217
+ const result = removeLinksFromHtmlContent(content);
218
+
219
+ expect(result).toContain('<a>');
220
+ });
353
221
  });
354
222
 
355
- it('should handle userList with empty arrays', () => {
356
- const userList = {
357
- capUsers: [],
358
- otherUsers: [],
359
- };
360
-
361
- const result = getMergedUserList(userList);
223
+ describe('iframePreviewAdjustWidth', () => {
224
+ it('should add width attribute to img tags', () => {
225
+ const content = '<img src="image.jpg" />';
226
+
227
+ const result = iframePreviewAdjustWidth(content);
228
+
229
+ expect(result).toContain('width="310"');
230
+ expect(result).toContain('src="image.jpg"');
231
+ });
232
+
233
+ it('should set width to 310 pixels', () => {
234
+ const content = '<img src="image.jpg" />';
235
+
236
+ const result = iframePreviewAdjustWidth(content);
237
+
238
+ expect(result).toContain('width="310"');
239
+ });
240
+
241
+ it('should handle multiple img tags', () => {
242
+ const content = '<img src="image1.jpg" /><img src="image2.jpg" />';
243
+
244
+ const result = iframePreviewAdjustWidth(content);
245
+
246
+ const widthCount = (result.match(/width="310"/g) || []).length;
247
+ expect(widthCount).toBe(2);
248
+ });
249
+
250
+ it('should preserve existing img attributes', () => {
251
+ const content = '<img src="image.jpg" alt="Alt text" />';
252
+
253
+ const result = iframePreviewAdjustWidth(content);
254
+
255
+ expect(result).toContain('src="image.jpg"');
256
+ expect(result).toContain('alt="Alt text"');
257
+ expect(result).toContain('width="310"');
258
+ });
259
+
260
+ it('should handle img tags with other attributes', () => {
261
+ const content = '<img src="image.jpg" height="200" class="responsive" />';
262
+
263
+ const result = iframePreviewAdjustWidth(content);
264
+
265
+ expect(result).toContain('width="310"');
266
+ expect(result).toContain('height="200"');
267
+ expect(result).toContain('class="responsive"');
268
+ });
269
+
270
+ it('should handle content without img tags', () => {
271
+ const content = '<p>No images here</p>';
362
272
 
363
- expect(result).toEqual([]);
273
+ const result = iframePreviewAdjustWidth(content);
274
+
275
+ expect(result).toContain('<p>No images here</p>');
276
+ expect(result).not.toContain('width="310"');
277
+ });
278
+
279
+ it('should handle complex HTML structure', () => {
280
+ const content = '<div><img src="image.jpg" /><p>Text</p><img src="image2.jpg" /></div>';
281
+
282
+ const result = iframePreviewAdjustWidth(content);
283
+
284
+ const widthCount = (result.match(/width="310"/g) || []).length;
285
+ expect(widthCount).toBe(2);
286
+ expect(result).toContain('<p>Text</p>');
287
+ });
288
+
289
+ it('should handle self-closing and non-self-closing img tags', () => {
290
+ const content1 = '<img src="image.jpg" />';
291
+ const content2 = '<img src="image.jpg">';
292
+
293
+ const result1 = iframePreviewAdjustWidth(content1);
294
+ const result2 = iframePreviewAdjustWidth(content2);
295
+
296
+ expect(result1).toContain('width="310"');
297
+ expect(result2).toContain('width="310"');
298
+ });
299
+
300
+ it('should handle empty content', () => {
301
+ const result = iframePreviewAdjustWidth('');
302
+ // When empty string is parsed as HTML, the DOM parser adds html/head/body tags
303
+ expect(result).toContain('<html>');
304
+ expect(result).toContain('<body>');
305
+ });
364
306
  });
365
- });
307
+ });