-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathentry-editable.test.ts
More file actions
717 lines (587 loc) · 41.8 KB
/
entry-editable.test.ts
File metadata and controls
717 lines (587 loc) · 41.8 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
import { addTags, getTag } from '../src/entry-editable'
import { entry_global_field, entry_global_field_multiple, entry_modular_block, entry_reference, entry_with_text, entry_with_applied_variants, entry_with_parent_path_variants } from './mock/entry-editable-mock'
describe('Entry editable test', () => {
it('Entry with text test', done => {
addTags(entry_with_text, 'entry_asset', false)
expect((entry_with_text as any)['$']['rich_text_editor']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.rich_text_editor')
expect((entry_with_text as any)['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.rich_text_editor_multiple')
done()
})
it('Entry with text should return object test', done => {
addTags(entry_with_text, 'entry_asset', true)
expect((entry_with_text as any)['$']['rich_text_editor']).toEqual({'data-cslp': 'entry_asset.entry_uid_1.en-us.rich_text_editor'})
expect((entry_with_text as any)['$']['rich_text_editor_multiple']).toEqual({'data-cslp': 'entry_asset.entry_uid_1.en-us.rich_text_editor_multiple' })
done()
})
it('Entry with Modular blocks test', done => {
addTags(entry_modular_block, 'entry_multiple_content', false)
expect((entry_modular_block as any)['modular_blocks'][0]['rich_text_inmodular']['$']['rich_text_editor']).toEqual('data-cslp=entry_multiple_content.entry_uid_1.en-us.modular_blocks.0.rich_text_inmodular.rich_text_editor')
expect((entry_modular_block as any)['modular_blocks'][0]['rich_text_inmodular']['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_multiple_content.entry_uid_1.en-us.modular_blocks.0.rich_text_inmodular.rich_text_editor_multiple')
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['$']['rich_text_editor']).toEqual('data-cslp=entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.rich_text_editor')
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.rich_text_editor_multiple')
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['group']['$']['rich_text_editor']).toEqual('data-cslp=entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.group.rich_text_editor')
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['group']['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.group.rich_text_editor_multiple')
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor']).toEqual('data-cslp=entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.modular_blocks.0.rich_in_modular.rich_text_editor')
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.modular_blocks.0.rich_in_modular.rich_text_editor_multiple')
done()
})
it('Entry with Modular blocks test return object', done => {
addTags(entry_modular_block, 'entry_multiple_content', true)
expect((entry_modular_block as any)['modular_blocks'][0]['rich_text_inmodular']['$']['rich_text_editor']).toEqual({'data-cslp':'entry_multiple_content.entry_uid_1.en-us.modular_blocks.0.rich_text_inmodular.rich_text_editor'})
expect((entry_modular_block as any)['modular_blocks'][0]['rich_text_inmodular']['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_multiple_content.entry_uid_1.en-us.modular_blocks.0.rich_text_inmodular.rich_text_editor_multiple'})
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['$']['rich_text_editor']).toEqual({'data-cslp':'entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.rich_text_editor'})
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.rich_text_editor_multiple'})
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['group']['$']['rich_text_editor']).toEqual({'data-cslp':'entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.group.rich_text_editor'})
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['group']['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.group.rich_text_editor_multiple'})
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor']).toEqual({'data-cslp':'entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.modular_blocks.0.rich_in_modular.rich_text_editor'})
expect((entry_modular_block as any)['modular_blocks'][1]['global_modular']['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_multiple_content.entry_uid_1.en-us.modular_blocks.1.global_modular.modular_blocks.0.rich_in_modular.rich_text_editor_multiple'})
done()
})
it('Entry with reference test', done => {
addTags(entry_reference, 'entry_asset', false)
expect((entry_reference as any)['reference'][0]['$']['rich_text_editor']).toEqual('data-cslp=embed_entry.entry_uid_11.en-us.rich_text_editor')
expect((entry_reference as any)['reference'][0]['$']['rich_text_editor_multiple']).toEqual('data-cslp=embed_entry.entry_uid_11.en-us.rich_text_editor_multiple')
done()
})
it('Entry with reference should return object test', done => {
addTags(entry_reference, 'entry_asset', true)
expect((entry_reference as any)['reference'][0]['$']['rich_text_editor']).toEqual({'data-cslp':'embed_entry.entry_uid_11.en-us.rich_text_editor'})
expect((entry_reference as any)['reference'][0]['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'embed_entry.entry_uid_11.en-us.rich_text_editor_multiple'})
done()
})
it('Entry with global fields test', done => {
addTags(entry_global_field, 'entry_asset', false)
expect((entry_global_field as any)['global_rich']['$']['rich_text_editor']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich.rich_text_editor')
expect((entry_global_field as any)['global_rich']['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich.rich_text_editor_multiple')
expect((entry_global_field as any)['global_rich']['group']['$']['rich_text_editor']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich.group.rich_text_editor')
expect((entry_global_field as any)['global_rich']['group']['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich.group.rich_text_editor_multiple')
expect((entry_global_field as any)['global_rich']['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich.modular_blocks.0.rich_in_modular.rich_text_editor')
expect((entry_global_field as any)['global_rich']['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich.modular_blocks.0.rich_in_modular.rich_text_editor_multiple')
done()
})
it('Entry with reference should return object test', done => {
addTags(entry_global_field, 'entry_asset', true)
expect((entry_global_field as any)['global_rich']['$']['rich_text_editor']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich.rich_text_editor'})
expect((entry_global_field as any)['global_rich']['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich.rich_text_editor_multiple'})
expect((entry_global_field as any)['global_rich']['group']['$']['rich_text_editor']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich.group.rich_text_editor'})
expect((entry_global_field as any)['global_rich']['group']['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich.group.rich_text_editor_multiple'})
expect((entry_global_field as any)['global_rich']['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich.modular_blocks.0.rich_in_modular.rich_text_editor'})
expect((entry_global_field as any)['global_rich']['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich.modular_blocks.0.rich_in_modular.rich_text_editor_multiple'})
done()
})
it('Entry with reference test', done => {
addTags(entry_global_field_multiple, 'entry_asset', false)
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['$']['rich_text_editor']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich_multiple.0.rich_text_editor')
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich_multiple.0.rich_text_editor_multiple')
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['group']['$']['rich_text_editor']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich_multiple.0.group.rich_text_editor')
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['group']['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich_multiple.0.group.rich_text_editor_multiple')
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich_multiple.0.modular_blocks.0.rich_in_modular.rich_text_editor')
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.global_rich_multiple.0.modular_blocks.0.rich_in_modular.rich_text_editor_multiple')
done()
})
it('Entry with reference should return object test', done => {
addTags(entry_global_field_multiple, 'entry_asset', true)
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['$']['rich_text_editor']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich_multiple.0.rich_text_editor'})
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich_multiple.0.rich_text_editor_multiple'})
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['group']['$']['rich_text_editor']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich_multiple.0.group.rich_text_editor'})
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['group']['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich_multiple.0.group.rich_text_editor_multiple'})
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich_multiple.0.modular_blocks.0.rich_in_modular.rich_text_editor'})
expect((entry_global_field_multiple as any)['global_rich_multiple'][0]['modular_blocks'][0]['rich_in_modular']['$']['rich_text_editor_multiple']).toEqual({'data-cslp':'entry_asset.entry_uid_1.en-us.global_rich_multiple.0.modular_blocks.0.rich_in_modular.rich_text_editor_multiple'})
done()
})
// Tests for applied variants functionality
describe('Applied Variants Tests', () => {
it('Entry with applied variants should generate v2 tags with variant suffix', done => {
addTags(entry_with_applied_variants, 'entry_asset', false)
// Field with direct variant match should get v2 prefix and variant suffix
expect((entry_with_applied_variants as any)['$']['rich_text_editor']).toEqual('data-cslp=v2:entry_asset.entry_uid_1_variant_1.en-us.rich_text_editor')
// Nested field with direct variant match
expect((entry_with_applied_variants as any)['nested']['$']['field']).toEqual('data-cslp=v2:entry_asset.entry_uid_1_variant_2.en-us.nested.field')
// Field without variant should not have v2 prefix
expect((entry_with_applied_variants as any)['nested']['$']['other_field']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.nested.other_field')
expect((entry_with_applied_variants as any)['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.rich_text_editor_multiple')
done()
})
it('Entry with applied variants should return v2 objects when tagsAsObject is true', done => {
addTags(entry_with_applied_variants, 'entry_asset', true)
// Field with direct variant match should get v2 prefix and variant suffix as object
expect((entry_with_applied_variants as any)['$']['rich_text_editor']).toEqual({'data-cslp': 'v2:entry_asset.entry_uid_1_variant_1.en-us.rich_text_editor'})
// Nested field with direct variant match
expect((entry_with_applied_variants as any)['nested']['$']['field']).toEqual({'data-cslp': 'v2:entry_asset.entry_uid_1_variant_2.en-us.nested.field'})
// Field without variant should not have v2 prefix
expect((entry_with_applied_variants as any)['nested']['$']['other_field']).toEqual({'data-cslp': 'entry_asset.entry_uid_1.en-us.nested.other_field'})
expect((entry_with_applied_variants as any)['$']['rich_text_editor_multiple']).toEqual({'data-cslp': 'entry_asset.entry_uid_1.en-us.rich_text_editor_multiple'})
done()
})
it('Entry with parent path variants should find correct variant', done => {
addTags(entry_with_parent_path_variants, 'entry_asset', false)
// Group field should get parent variant
expect((entry_with_parent_path_variants as any)['$']['group']).toEqual('data-cslp=v2:entry_asset.entry_uid_3_parent_variant.en-us.group')
// Field under 'group' parent should get parent variant
expect((entry_with_parent_path_variants as any)['group']['$']['other']).toEqual('data-cslp=v2:entry_asset.entry_uid_3_parent_variant.en-us.group.other')
// Field under 'group.nested' should get parent variant (group is longer match)
expect((entry_with_parent_path_variants as any)['group']['nested']['$']['field']).toEqual('data-cslp=v2:entry_asset.entry_uid_3_parent_variant.en-us.group.nested.field')
// Field with exact deep path match should get deep variant
expect((entry_with_parent_path_variants as any)['group']['nested']['deep']['$']['field']).toEqual('data-cslp=v2:entry_asset.entry_uid_3_deep_variant.en-us.group.nested.deep.field')
// Field with the same starting path should not get parent variant
expect((entry_with_parent_path_variants as any)['$']['group_multiple']).toEqual('data-cslp=entry_asset.entry_uid_3.en-us.group_multiple')
// Modular block content with variant should get v2 prefix and variant suffix
expect((entry_with_parent_path_variants as any)['modular_blocks'][0]['$']['content']).toEqual('data-cslp=v2:entry_asset.entry_uid_3_parent_variant.en-us.modular_blocks.0.content')
// Modular block field inside a variantised parent should get v2 prefix and variant suffix
expect((entry_with_parent_path_variants as any)['modular_blocks'][0]['content']['$']['title']).toEqual('data-cslp=v2:entry_asset.entry_uid_3_parent_variant.en-us.modular_blocks.0.content.title')
// Modular block content without variant should not have v2 prefix and variant suffix
expect((entry_with_parent_path_variants as any)['modular_blocks'][1]['$']['content']).toEqual('data-cslp=entry_asset.entry_uid_3.en-us.modular_blocks.1.content')
// Modular block field inside a non variantised parent should not get v2 prefix and variant suffix
expect((entry_with_parent_path_variants as any)['modular_blocks'][1]['content']['$']['title']).toEqual('data-cslp=entry_asset.entry_uid_3.en-us.modular_blocks.1.content.title')
done()
})
it('Entry with modular block variants should apply variants correctly', done => {
addTags(entry_with_applied_variants, 'entry_asset', false)
// Modular block content with variant should get v2 prefix and variant suffix
expect((entry_with_applied_variants as any)['modular_blocks'][1]['$']['content_from_variant']).toEqual('data-cslp=v2:entry_asset.entry_uid_1_variant_3.en-us.modular_blocks.1.content_from_variant')
// Modular block field inside a variantised parent should get v2 prefix and variant suffix
expect((entry_with_applied_variants as any)['modular_blocks'][1]['content_from_variant']['$']['title']).toEqual('data-cslp=v2:entry_asset.entry_uid_1_variant_3.en-us.modular_blocks.1.content_from_variant.title')
// Field inside a variantised parent with a different variant should get v2 prefix and variant suffix of that variant
expect((entry_with_applied_variants as any)['modular_blocks'][1]['content_from_variant']['$']['different_from_parent_variant']).toEqual('data-cslp=v2:entry_asset.entry_uid_1_variant_4.en-us.modular_blocks.1.content_from_variant.different_from_parent_variant')
// Modular block content without variant should get v2 prefix and variant suffix
expect((entry_with_applied_variants as any)['modular_blocks'][0]['$']['content']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.modular_blocks.0.content')
// Modular block field without variant should not have v2 prefix and variant suffix
expect((entry_with_applied_variants as any)['modular_blocks'][0]['content']['$']['title']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.modular_blocks.0.content.title')
done()
})
it('Entry without applied variants should work normally', done => {
addTags(entry_with_text, 'entry_asset', false)
// Should not have v2 prefix when no variants are applied
expect((entry_with_text as any)['$']['rich_text_editor']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.rich_text_editor')
expect((entry_with_text as any)['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.rich_text_editor_multiple')
done()
})
it('Entry with empty applied variants should work normally', done => {
const entryWithEmptyVariants = {
...entry_with_text,
_applied_variants: {}
}
addTags(entryWithEmptyVariants, 'entry_asset', false)
// Should not have v2 prefix when variants object is empty
expect((entryWithEmptyVariants as any)['$']['rich_text_editor']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.rich_text_editor')
expect((entryWithEmptyVariants as any)['$']['rich_text_editor_multiple']).toEqual('data-cslp=entry_asset.entry_uid_1.en-us.rich_text_editor_multiple')
done()
})
it('Entry with array containing null values should skip null elements', done => {
const entryWithNullInArray = {
"locale": "en-us",
"uid": "uid",
"items": [
null,
{ "title": "valid item" },
null
]
}
expect(() => addTags(entryWithNullInArray, 'content_type', false)).not.toThrow()
expect((entryWithNullInArray as any)['items'][1]['$']['title']).toEqual('data-cslp=content_type.uid.en-us.items.1.title')
done()
})
it('Entry with array containing undefined values should skip undefined elements', done => {
const entryWithUndefinedInArray = {
"locale": "en-us",
"uid": "uid",
"items": [
undefined,
{ "title": "valid item" },
undefined
]
}
expect(() => addTags(entryWithUndefinedInArray, 'content_type', false)).not.toThrow()
expect((entryWithUndefinedInArray as any)['items'][1]['$']['title']).toEqual('data-cslp=content_type.uid.en-us.items.1.title')
done()
})
it('Entry with array containing mixed null and undefined values should skip both', done => {
const entryWithMixedNullUndefined = {
"locale": "en-us",
"uid": "uid",
"items": [
null,
undefined,
{ "title": "valid item 1" },
null,
{ "title": "valid item 2" },
undefined
]
}
expect(() => addTags(entryWithMixedNullUndefined, 'content_type', true)).not.toThrow()
expect((entryWithMixedNullUndefined as any)['items'][2]['$']['title']).toEqual({'data-cslp': 'content_type.uid.en-us.items.2.title'})
expect((entryWithMixedNullUndefined as any)['items'][4]['$']['title']).toEqual({'data-cslp': 'content_type.uid.en-us.items.4.title'})
done()
})
it('Entry with _embedded_items containing null values should handle gracefully', done => {
const entryWithEmbeddedNull: any = {
"locale": "en-us",
"uid": "uid",
"blocks": [
{
"items": [
{ "heading": "Item heading" }
]
}
],
"_embedded_items": {
"blocks.items.description": [null]
}
}
expect(() => addTags(entryWithEmbeddedNull, 'content_type', false)).not.toThrow()
expect((entryWithEmbeddedNull as any)['blocks'][0]['items'][0]['$']['heading']).toEqual('data-cslp=content_type.uid.en-us.blocks.0.items.0.heading')
expect((entryWithEmbeddedNull as any)['_embedded_items']['blocks.items.description'][0]).toBeNull()
done()
})
it('Entry with nested arrays containing nulls in complex structure should work', done => {
const entryWithComplexNulls: any = {
"locale": "en-us",
"uid": "uid",
"blocks": [
{
"hero": {
"title": "Hero title",
"items": [null, { "name": "Item name" }, undefined]
}
},
null,
{
"content": "Content text"
}
]
}
expect(() => addTags(entryWithComplexNulls, 'content_type', true)).not.toThrow()
expect((entryWithComplexNulls as any)['blocks'][0]['hero']['$']['title']).toEqual({'data-cslp': 'content_type.uid.en-us.blocks.0.hero.title'})
expect((entryWithComplexNulls as any)['blocks'][0]['hero']['items'][1]['$']['name']).toEqual({'data-cslp': 'content_type.uid.en-us.blocks.0.hero.items.1.name'})
expect((entryWithComplexNulls as any)['blocks'][2]['$']['content']).toEqual({'data-cslp': 'content_type.uid.en-us.blocks.2.content'})
done()
})
it('Variant path sorting should work correctly for nested paths', done => {
const entryWithComplexVariants = {
"_version": 10,
"locale": "en-us",
"uid": "entry_uid_test",
"ACL": {},
"_applied_variants": {
"a": "variant_a",
"a.b": "variant_ab",
"a.b.c": "variant_abc",
"a.b.c.d": "variant_abcd"
},
"a": {
"b": {
"c": {
"d": {
"field": "deep field"
},
"field": "c field"
},
"field": "b field"
},
"field": "a field"
}
}
addTags(entryWithComplexVariants, 'entry_asset', false)
// Should use the longest matching path variant
expect((entryWithComplexVariants as any)['a']['b']['c']['d']['$']['field']).toEqual('data-cslp=v2:entry_asset.entry_uid_test_variant_abcd.en-us.a.b.c.d.field')
expect((entryWithComplexVariants as any)['a']['b']['c']['$']['field']).toEqual('data-cslp=v2:entry_asset.entry_uid_test_variant_abc.en-us.a.b.c.field')
expect((entryWithComplexVariants as any)['a']['b']['$']['field']).toEqual('data-cslp=v2:entry_asset.entry_uid_test_variant_ab.en-us.a.b.field')
expect((entryWithComplexVariants as any)['a']['$']['field']).toEqual('data-cslp=v2:entry_asset.entry_uid_test_variant_a.en-us.a.field')
done()
})
it('Reference fields should not inherit parent variants when they have no applied_variants', done => {
const entryWithReferenceAndVariants = {
"title": "home",
"url": "/data/all_test/first",
"single_line": "ssd",
"tags": ["hi"],
"locale": "en-us",
"uid": "blt827e0ad3608248be",
"created_by": "bltf0d59057590e9b09",
"updated_by": "bltf0d59057590e9b09",
"created_at": "2025-08-25T09:43:49.935Z",
"updated_at": "2025-10-09T11:45:19.967Z",
"ACL": [] as any[],
"_version": 40,
"_in_progress": false,
"json_rte": "<p>hisdassf</p>",
"select": "1",
"group": {
"single_line": ""
},
"non_single_line_textbox": "",
"reference": [
{
"title": "base variant",
"single_line": "bases",
"tags": [] as any[],
"locale": "en-us",
"uid": "blt07a6c7258ddba844",
"created_by": "bltf0d59057590e9b09",
"updated_by": "bltf0d59057590e9b09",
"created_at": "2025-10-01T03:10:10.701Z",
"updated_at": "2025-10-09T11:44:44.981Z",
"_content_type_uid": "all_test_3",
"ACL": [] as any[],
"_version": 3,
"_in_progress": false,
"multi_line_reference": "hii\n"
// Note: This reference object has NO _applied_variants
}
],
"taxonomies": [] as any[],
"multi_line": "woek",
"_applied_variants": {
"single_line": "csfff653e89df54e8c",
"tags": "csfff653e89df54e8c",
"multi_line": "csfff653e89df54e8c"
}
}
addTags(entryWithReferenceAndVariants, 'all_test', false)
// Parent entry fields with variants should get v2 prefix
expect((entryWithReferenceAndVariants as any)['$']['single_line']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.single_line')
expect((entryWithReferenceAndVariants as any)['$']['tags']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.tags')
expect((entryWithReferenceAndVariants as any)['$']['multi_line']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.multi_line')
// Reference fields should NOT get v2 prefix since they have no _applied_variants
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['title']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.title')
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['single_line']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.single_line')
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['tags']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.tags')
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['multi_line_reference']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.multi_line_reference')
done()
})
it('Reference fields with their own applied_variants should use their variants', done => {
const entryWithReferenceHavingVariants = {
"title": "home",
"locale": "en-us",
"uid": "blt827e0ad3608248be",
"ACL": [] as any[],
"_version": 40,
"_in_progress": false,
"single_line": "parent field",
"reference": [
{
"title": "base variant",
"single_line": "reference field",
"locale": "en-us",
"uid": "blt07a6c7258ddba844",
"_content_type_uid": "all_test_3",
"ACL": [] as any[],
"_version": 3,
"_in_progress": false,
"_applied_variants": {
"single_line": "ref_variant_123"
}
}
],
"_applied_variants": {
"single_line": "parent_variant_456"
}
}
addTags(entryWithReferenceHavingVariants, 'all_test', false)
// Parent entry field should get parent variant
expect((entryWithReferenceHavingVariants as any)['$']['single_line']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_parent_variant_456.en-us.single_line')
// Reference field should get its own variant, not parent variant
expect((entryWithReferenceHavingVariants as any)['reference'][0]['$']['title']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.title')
expect((entryWithReferenceHavingVariants as any)['reference'][0]['$']['single_line']).toEqual('data-cslp=v2:all_test_3.blt07a6c7258ddba844_ref_variant_123.en-us.single_line')
done()
})
it('Reference fields should work correctly with tagsAsObject=true', done => {
const entryWithReferenceAndVariants = {
"title": "home",
"single_line": "ssd",
"locale": "en-us",
"uid": "blt827e0ad3608248be",
"ACL": [] as any[],
"_version": 40,
"_in_progress": false,
"reference": [
{
"title": "base variant",
"single_line": "bases",
"locale": "en-us",
"uid": "blt07a6c7258ddba844",
"_content_type_uid": "all_test_3",
"ACL": [] as any[],
"_version": 3,
"_in_progress": false
// No _applied_variants
}
],
"_applied_variants": {
"single_line": "csfff653e89df54e8c"
}
}
addTags(entryWithReferenceAndVariants, 'all_test', true)
// Parent entry field with variant should get v2 prefix as object
expect((entryWithReferenceAndVariants as any)['$']['single_line']).toEqual({'data-cslp': 'v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.single_line'})
// Reference fields should NOT get v2 prefix as objects
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['title']).toEqual({'data-cslp': 'all_test_3.blt07a6c7258ddba844.en-us.title'})
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['single_line']).toEqual({'data-cslp': 'all_test_3.blt07a6c7258ddba844.en-us.single_line'})
done()
})
})
describe('Null and undefined content handling (Issue #193)', () => {
it('should not throw when entry has a null field value', done => {
const entryWithNullField: any = {
"uid": "entry_uid_null",
"locale": "en-us",
"title": "Valid title",
"description": null
}
expect(() => addTags(entryWithNullField, 'content_type', false)).not.toThrow()
expect((entryWithNullField as any)['$']['title']).toEqual('data-cslp=content_type.entry_uid_null.en-us.title')
done()
})
it('should not throw when entry has an undefined field value', done => {
const entryWithUndefinedField: any = {
"uid": "entry_uid_undef",
"locale": "en-us",
"title": "Valid title",
"description": undefined
}
expect(() => addTags(entryWithUndefinedField, 'content_type', false)).not.toThrow()
expect((entryWithUndefinedField as any)['$']['title']).toEqual('data-cslp=content_type.entry_uid_undef.en-us.title')
done()
})
it('should return empty tags for a null entry', done => {
const nullEntry: any = null
expect(() => addTags(nullEntry, 'content_type', false)).not.toThrow()
done()
})
it('should return empty tags for an undefined entry', done => {
const undefinedEntry: any = undefined
expect(() => addTags(undefinedEntry, 'content_type', false)).not.toThrow()
done()
})
it('should handle entry with multiple null field values', done => {
const entryWithMultipleNulls: any = {
"uid": "entry_uid_multi_null",
"locale": "en-us",
"title": "Valid title",
"field_a": null,
"field_b": null,
"field_c": "valid"
}
expect(() => addTags(entryWithMultipleNulls, 'content_type', true)).not.toThrow()
expect((entryWithMultipleNulls as any)['$']['title']).toEqual({'data-cslp': 'content_type.entry_uid_multi_null.en-us.title'})
expect((entryWithMultipleNulls as any)['$']['field_c']).toEqual({'data-cslp': 'content_type.entry_uid_multi_null.en-us.field_c'})
done()
})
it('should handle entry with nested null object', done => {
const entryWithNestedNull: any = {
"uid": "entry_uid_nested_null",
"locale": "en-us",
"title": "Valid title",
"group": null
}
expect(() => addTags(entryWithNestedNull, 'content_type', false)).not.toThrow()
expect((entryWithNestedNull as any)['$']['title']).toEqual('data-cslp=content_type.entry_uid_nested_null.en-us.title')
done()
})
it('getTag with null content returns empty object (covers null guard)', done => {
const appliedVariants = { _applied_variants: {}, shouldApplyVariant: false, metaKey: '' }
const result = getTag(null as any, 'some.prefix', false, 'en-us', appliedVariants)
expect(result).toEqual({})
done()
})
it('getTag with undefined content returns empty object (covers null guard)', done => {
const appliedVariants = { _applied_variants: {}, shouldApplyVariant: false, metaKey: '' }
const result = getTag(undefined as any, 'some.prefix', true, 'en-us', appliedVariants)
expect(result).toEqual({})
done()
})
})
describe('useLowerCaseLocale option', () => {
it('should preserve locale casing when useLowerCaseLocale is false', done => {
const entry = {
"uid": "test_uid",
"locale": "en-US",
"title": "Test Entry"
}
addTags(entry, 'test_content_type', false, 'en-US', { useLowerCaseLocale: false })
expect((entry as any)['$']['title']).toEqual('data-cslp=test_content_type.test_uid.en-US.title')
done()
})
it('should lowercase locale when useLowerCaseLocale is explicitly true', done => {
const entry = {
"uid": "test_uid",
"locale": "en-US",
"title": "Test Entry"
}
addTags(entry, 'test_content_type', false, 'en-US', { useLowerCaseLocale: true })
expect((entry as any)['$']['title']).toEqual('data-cslp=test_content_type.test_uid.en-us.title')
done()
})
it('should lowercase locale by default when options is not provided', done => {
const entry = {
"uid": "test_uid",
"locale": "en-US",
"title": "Test Entry"
}
addTags(entry, 'test_content_type', false, 'en-US')
expect((entry as any)['$']['title']).toEqual('data-cslp=test_content_type.test_uid.en-us.title')
done()
})
})
describe('Negative and corner cases', () => {
it('addTags with empty entry object should not throw and should set $ with empty-like tags', done => {
const emptyEntry = { uid: 'e1', locale: 'en-us' }
expect(() => addTags(emptyEntry, 'ct', false)).not.toThrow()
expect((emptyEntry as any).$).toBeDefined()
expect(typeof (emptyEntry as any).$).toBe('object')
done()
})
it('addTags with empty string contentTypeUid should normalize to lowercase', done => {
const entry = { uid: 'u1', locale: 'en-us', title: 't' }
addTags(entry, '', false)
expect((entry as any)['$']['title']).toContain('.title')
done()
})
it('addTags with options as undefined should not throw', done => {
const entry = { uid: 'u1', locale: 'en-us', title: 't' }
expect(() => addTags(entry, 'ct', false, 'en-us', undefined)).not.toThrow()
expect((entry as any)['$']['title']).toBeDefined()
done()
})
it('getTag with empty object content should return empty tags object', done => {
const appliedVariants = { _applied_variants: {}, shouldApplyVariant: false, metaKey: '' }
const result = getTag({}, 'prefix', false, 'en-us', appliedVariants)
expect(result).toEqual({})
done()
})
it('getTag with content that has only $ key should skip $ and return empty', done => {
const appliedVariants = { _applied_variants: {}, shouldApplyVariant: false, metaKey: '' }
const result = getTag({ $: 'ignored' }, 'prefix', false, 'en-us', appliedVariants)
expect(result).toEqual({})
done()
})
it('getTag with appliedVariants._applied_variants null should not throw (getParentVariantisedPath safety)', done => {
const content = { field: 'value' }
const appliedVariants = { _applied_variants: null as any, shouldApplyVariant: true, metaKey: 'field' }
expect(() => getTag(content, 'prefix', false, 'en-us', appliedVariants)).not.toThrow()
const result = getTag(content, 'prefix', false, 'en-us', appliedVariants)
expect(result).toHaveProperty('field')
done()
})
it('getTag with appliedVariants.metaKey empty and shouldApplyVariant true should still produce tags', done => {
const content = { title: 'hello' }
const appliedVariants = { _applied_variants: {}, shouldApplyVariant: true, metaKey: '' }
const result = getTag(content, 'ct.uid.en-us', false, 'en-us', appliedVariants)
expect(result).toHaveProperty('title')
expect((result as any).title).toContain('title')
done()
})
it('addTags with entry that has empty array field should not throw', done => {
const entry: any = { uid: 'u1', locale: 'en-us', items: [] }
expect(() => addTags(entry, 'ct', false)).not.toThrow()
expect((entry as any)['$']['items']).toBeDefined()
done()
})
it('addTags with entry that has nested null object value should not throw', done => {
const entry: any = { uid: 'u1', locale: 'en-us', title: 't', nested: null }
expect(() => addTags(entry, 'ct', true)).not.toThrow()
expect((entry as any)['$']['title']).toEqual({ 'data-cslp': expect.stringContaining('title') })
done()
})
})
})