slice-machine-ui 2.17.0 → 2.17.1-alpha.jp-cr-ui-nested-ct-remove-section.2

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,3 +1,4 @@
1
+ import { CustomType } from "@prismicio/types-internal/lib/customtypes";
1
2
  import { describe, expect, it } from "vitest";
2
3
 
3
4
  import {
@@ -6,154 +7,697 @@ import {
6
7
  } from "../ContentRelationshipFieldPicker";
7
8
 
8
9
  describe("ContentRelationshipFieldPicker", () => {
9
- it("should count picked fields with a custom type as string", () => {
10
- const customtypes = ["ct1"];
10
+ describe("countPickedFields", () => {
11
+ const allCustomTypes: CustomType[] = [
12
+ {
13
+ id: "ct1",
14
+ label: "CT 1",
15
+ repeatable: false,
16
+ status: true,
17
+ json: {
18
+ Main: {
19
+ f1: {
20
+ type: "Link",
21
+ config: {
22
+ select: "document",
23
+ customtypes: ["ct2"],
24
+ },
25
+ },
26
+ f2: {
27
+ type: "Link",
28
+ config: {
29
+ select: "document",
30
+ customtypes: ["ct2"],
31
+ },
32
+ },
33
+ g1: {
34
+ type: "Group",
35
+ config: {
36
+ fields: {
37
+ f1: {
38
+ type: "Boolean",
39
+ },
40
+ f2: {
41
+ type: "Link",
42
+ config: {
43
+ select: "document",
44
+ customtypes: ["ct2"],
45
+ },
46
+ },
47
+ },
48
+ },
49
+ },
50
+ },
51
+ },
52
+ },
53
+ {
54
+ id: "ct2",
55
+ label: "CT 2",
56
+ repeatable: false,
57
+ status: true,
58
+ json: {
59
+ Main: {
60
+ f1: {
61
+ type: "Boolean",
62
+ },
63
+ g2: {
64
+ type: "Group",
65
+ config: {
66
+ fields: {
67
+ f1: {
68
+ type: "Boolean",
69
+ },
70
+ f2: {
71
+ type: "Boolean",
72
+ },
73
+ },
74
+ },
75
+ },
76
+ },
77
+ },
78
+ },
79
+ ];
80
+
81
+ it("should count picked fields with a custom type as string", () => {
82
+ const customtypes = ["ct1"];
11
83
 
12
- const result = countPickedFields(
13
- convertLinkCustomtypesToFieldCheckMap(customtypes),
14
- );
84
+ const result = countPickedFields(
85
+ convertLinkCustomtypesToFieldCheckMap({
86
+ linkCustomtypes: customtypes,
87
+ allCustomTypes,
88
+ }),
89
+ );
15
90
 
16
- expect(result).toEqual({
17
- pickedFields: 0,
18
- nestedPickedFields: 0,
91
+ expect(result).toEqual({
92
+ pickedFields: 0,
93
+ nestedPickedFields: 0,
94
+ });
19
95
  });
20
- });
21
96
 
22
- it("should count picked fields with multiple custom types as string", () => {
23
- const customtypes = ["ct1", "ct2"];
97
+ it("should count picked fields with multiple custom types as string", () => {
98
+ const customtypes = ["ct1", "ct2"];
24
99
 
25
- const result = countPickedFields(
26
- convertLinkCustomtypesToFieldCheckMap(customtypes),
27
- );
100
+ const result = countPickedFields(
101
+ convertLinkCustomtypesToFieldCheckMap({
102
+ linkCustomtypes: customtypes,
103
+ allCustomTypes,
104
+ }),
105
+ );
28
106
 
29
- expect(result).toEqual({
30
- pickedFields: 0,
31
- nestedPickedFields: 0,
107
+ expect(result).toEqual({
108
+ pickedFields: 0,
109
+ nestedPickedFields: 0,
110
+ });
32
111
  });
33
- });
34
112
 
35
- it("should count picked fields with custom type as object and one field", () => {
36
- const customtypes = [
37
- {
38
- id: "ct1",
39
- fields: ["f1"],
40
- },
41
- ];
113
+ it("should count picked fields with custom type as object and one field", () => {
114
+ const customtypes = [
115
+ {
116
+ id: "ct1",
117
+ fields: ["f1"],
118
+ },
119
+ ];
120
+
121
+ const result = countPickedFields(
122
+ convertLinkCustomtypesToFieldCheckMap({
123
+ linkCustomtypes: customtypes,
124
+ allCustomTypes,
125
+ }),
126
+ );
42
127
 
43
- const result = countPickedFields(
44
- convertLinkCustomtypesToFieldCheckMap(customtypes),
45
- );
128
+ expect(result).toEqual({
129
+ pickedFields: 1,
130
+ nestedPickedFields: 0,
131
+ });
132
+ });
133
+
134
+ it("should count picked fields with custom type as object and one nested field", () => {
135
+ const customtypes = [
136
+ {
137
+ id: "ct1",
138
+ fields: [
139
+ {
140
+ id: "f1",
141
+ customtypes: [
142
+ {
143
+ id: "ct2",
144
+ fields: ["f1"],
145
+ },
146
+ ],
147
+ },
148
+ ],
149
+ },
150
+ ];
151
+
152
+ const result = countPickedFields(
153
+ convertLinkCustomtypesToFieldCheckMap({
154
+ linkCustomtypes: customtypes,
155
+ allCustomTypes,
156
+ }),
157
+ );
46
158
 
47
- expect(result).toEqual({
48
- pickedFields: 1,
49
- nestedPickedFields: 0,
159
+ expect(result).toEqual({
160
+ pickedFields: 1,
161
+ nestedPickedFields: 1,
162
+ });
163
+ });
164
+
165
+ it("should count picked fields with custom type as object with group field", () => {
166
+ const customtypes = [
167
+ {
168
+ id: "ct1",
169
+ fields: [
170
+ {
171
+ id: "g1",
172
+ fields: ["f1", "f2"],
173
+ },
174
+ ],
175
+ },
176
+ ];
177
+
178
+ const test = convertLinkCustomtypesToFieldCheckMap({
179
+ linkCustomtypes: customtypes,
180
+ allCustomTypes,
181
+ });
182
+ const result = countPickedFields(test);
183
+
184
+ expect(result).toEqual({
185
+ pickedFields: 2,
186
+ nestedPickedFields: 0,
187
+ });
188
+ });
189
+
190
+ it("should count picked fields with custom type as object with group field and nested custom type", () => {
191
+ const customtypes = [
192
+ {
193
+ id: "ct1",
194
+ fields: [
195
+ {
196
+ id: "g1",
197
+ fields: [
198
+ "f1",
199
+ {
200
+ id: "f2",
201
+ customtypes: [
202
+ {
203
+ id: "ct2",
204
+ fields: ["f1"],
205
+ },
206
+ ],
207
+ },
208
+ ],
209
+ },
210
+ ],
211
+ },
212
+ ];
213
+
214
+ const result = countPickedFields(
215
+ convertLinkCustomtypesToFieldCheckMap({
216
+ linkCustomtypes: customtypes,
217
+ allCustomTypes,
218
+ }),
219
+ );
220
+
221
+ expect(result).toEqual({
222
+ pickedFields: 2,
223
+ nestedPickedFields: 1,
224
+ });
225
+ });
226
+
227
+ it("should count picked fields with custom type as object with group field, nested custom type and nested group field", () => {
228
+ const customtypes = [
229
+ {
230
+ id: "ct1",
231
+ fields: [
232
+ {
233
+ id: "g1",
234
+ fields: [
235
+ "f1",
236
+ {
237
+ id: "f2",
238
+ customtypes: [
239
+ {
240
+ id: "ct2",
241
+ fields: [
242
+ "f1",
243
+ {
244
+ id: "g2",
245
+ fields: ["f1", "f2"],
246
+ },
247
+ ],
248
+ },
249
+ ],
250
+ },
251
+ ],
252
+ },
253
+ ],
254
+ },
255
+ ];
256
+
257
+ const result = countPickedFields(
258
+ convertLinkCustomtypesToFieldCheckMap({
259
+ linkCustomtypes: customtypes,
260
+ allCustomTypes,
261
+ }),
262
+ );
263
+
264
+ expect(result).toEqual({
265
+ pickedFields: 4,
266
+ nestedPickedFields: 3,
267
+ });
268
+ });
269
+
270
+ it("should count picked fields with custom type as object with nested custom type and nested group field", () => {
271
+ const customtypes = [
272
+ {
273
+ id: "ct1",
274
+ fields: [
275
+ "f1",
276
+ {
277
+ id: "f2",
278
+ customtypes: [
279
+ {
280
+ id: "ct2",
281
+ fields: [
282
+ "f1",
283
+ {
284
+ id: "g2",
285
+ fields: ["f1", "f2"],
286
+ },
287
+ ],
288
+ },
289
+ ],
290
+ },
291
+ ],
292
+ },
293
+ ];
294
+
295
+ const result = countPickedFields(
296
+ convertLinkCustomtypesToFieldCheckMap({
297
+ linkCustomtypes: customtypes,
298
+ allCustomTypes,
299
+ }),
300
+ );
301
+
302
+ expect(result).toEqual({
303
+ pickedFields: 4,
304
+ nestedPickedFields: 3,
305
+ });
306
+ });
307
+
308
+ it("should count picked fields with custom type as object with nested custom type without fields", () => {
309
+ const customtypes = [
310
+ {
311
+ id: "ct1",
312
+ fields: [
313
+ "f1",
314
+ {
315
+ id: "f2",
316
+ customtypes: [
317
+ {
318
+ id: "ct2",
319
+ fields: [],
320
+ },
321
+ ],
322
+ },
323
+ ],
324
+ },
325
+ ];
326
+
327
+ const result = countPickedFields(
328
+ convertLinkCustomtypesToFieldCheckMap({
329
+ linkCustomtypes: customtypes,
330
+ allCustomTypes,
331
+ }),
332
+ );
333
+
334
+ expect(result).toEqual({
335
+ pickedFields: 1,
336
+ nestedPickedFields: 0,
337
+ });
338
+ });
339
+
340
+ it("should count picked fields with custom type as object with group field without fields", () => {
341
+ const customtypes = [
342
+ {
343
+ id: "ct1",
344
+ fields: [
345
+ "f1",
346
+ {
347
+ id: "g1",
348
+ fields: [],
349
+ },
350
+ ],
351
+ },
352
+ ];
353
+
354
+ const result = countPickedFields(
355
+ convertLinkCustomtypesToFieldCheckMap({
356
+ linkCustomtypes: customtypes,
357
+ allCustomTypes,
358
+ }),
359
+ );
360
+
361
+ expect(result).toEqual({
362
+ pickedFields: 1,
363
+ nestedPickedFields: 0,
364
+ });
50
365
  });
51
366
  });
52
367
 
53
- it("should count picked fields with custom type as object and one nested field", () => {
54
- const customtypes = [
55
- {
56
- id: "ct1",
57
- fields: [
368
+ describe("createContentRelationshipFieldCheckMap", () => {
369
+ it("should include existing/valid referenced regular root fields", () => {
370
+ const customType: CustomType = {
371
+ id: "customType",
372
+ label: "Custom Type",
373
+ repeatable: false,
374
+ status: true,
375
+ json: {
376
+ Main: {
377
+ textField: {
378
+ type: "Text",
379
+ },
380
+ booleanField: {
381
+ type: "Boolean",
382
+ },
383
+ structuredTextField: {
384
+ type: "StructuredText",
385
+ },
386
+ imageField: {
387
+ type: "Image",
388
+ },
389
+ linkField: {
390
+ type: "Link",
391
+ },
392
+ colorField: {
393
+ type: "Color",
394
+ },
395
+ dateField: {
396
+ type: "Date",
397
+ },
398
+ geoPointField: {
399
+ type: "GeoPoint",
400
+ },
401
+ numberField: {
402
+ type: "Number",
403
+ },
404
+ rangeField: {
405
+ type: "Range",
406
+ },
407
+ selectField: {
408
+ type: "Select",
409
+ },
410
+ timestampField: {
411
+ type: "Timestamp",
412
+ },
413
+ separatorField: {
414
+ type: "Separator",
415
+ },
416
+ tableField: {
417
+ type: "Table",
418
+ },
419
+ integrationFieldsField: {
420
+ type: "IntegrationFields",
421
+ },
422
+ },
423
+ },
424
+ };
425
+
426
+ const result = convertLinkCustomtypesToFieldCheckMap({
427
+ linkCustomtypes: [
58
428
  {
59
- id: "f1",
60
- customtypes: [
61
- {
62
- id: "ct2",
63
- fields: ["f1"],
64
- },
429
+ id: "customType",
430
+ fields: [
431
+ "textField",
432
+ "booleanField",
433
+ "structuredTextField",
434
+ "imageField",
435
+ "linkField",
436
+ "colorField",
437
+ "dateField",
438
+ "geoPointField",
439
+ "numberField",
440
+ "rangeField",
441
+ "selectField",
442
+ "timestampField",
443
+ "separatorField",
444
+ "tableField",
445
+ "integrationFieldsField",
65
446
  ],
66
447
  },
67
448
  ],
68
- },
69
- ];
449
+ allCustomTypes: [customType],
450
+ });
70
451
 
71
- const result = countPickedFields(
72
- convertLinkCustomtypesToFieldCheckMap(customtypes),
73
- );
74
-
75
- expect(result).toEqual({
76
- pickedFields: 1,
77
- nestedPickedFields: 1,
452
+ expect(result).toEqual({
453
+ customType: {
454
+ textField: {
455
+ type: "checkbox",
456
+ value: true,
457
+ },
458
+ booleanField: {
459
+ type: "checkbox",
460
+ value: true,
461
+ },
462
+ structuredTextField: {
463
+ type: "checkbox",
464
+ value: true,
465
+ },
466
+ imageField: {
467
+ type: "checkbox",
468
+ value: true,
469
+ },
470
+ linkField: {
471
+ type: "checkbox",
472
+ value: true,
473
+ },
474
+ colorField: {
475
+ type: "checkbox",
476
+ value: true,
477
+ },
478
+ dateField: {
479
+ type: "checkbox",
480
+ value: true,
481
+ },
482
+ geoPointField: {
483
+ type: "checkbox",
484
+ value: true,
485
+ },
486
+ numberField: {
487
+ type: "checkbox",
488
+ value: true,
489
+ },
490
+ rangeField: {
491
+ type: "checkbox",
492
+ value: true,
493
+ },
494
+ selectField: {
495
+ type: "checkbox",
496
+ value: true,
497
+ },
498
+ timestampField: {
499
+ type: "checkbox",
500
+ value: true,
501
+ },
502
+ separatorField: {
503
+ type: "checkbox",
504
+ value: true,
505
+ },
506
+ tableField: {
507
+ type: "checkbox",
508
+ value: true,
509
+ },
510
+ integrationFieldsField: {
511
+ type: "checkbox",
512
+ value: true,
513
+ },
514
+ },
515
+ });
78
516
  });
79
- });
80
517
 
81
- it("should count picked fields with custom type as object with group field", () => {
82
- const customtypes = [
83
- {
84
- id: "ct1",
85
- fields: [
518
+ it("should include a correctly referenced group field", () => {
519
+ const customType: CustomType = {
520
+ id: "customType",
521
+ label: "Custom Type",
522
+ repeatable: false,
523
+ status: true,
524
+ json: {
525
+ Main: {
526
+ groupField: {
527
+ type: "Group",
528
+ config: {
529
+ label: "Group Field",
530
+ fields: {
531
+ booleanField: {
532
+ type: "Boolean",
533
+ config: {
534
+ label: "Boolean Field",
535
+ },
536
+ },
537
+ },
538
+ },
539
+ },
540
+ },
541
+ },
542
+ };
543
+
544
+ const result = convertLinkCustomtypesToFieldCheckMap({
545
+ linkCustomtypes: [
86
546
  {
87
- id: "g1",
88
- fields: ["f1", "f2"],
547
+ id: "customType",
548
+ fields: [
549
+ {
550
+ id: "groupField",
551
+ fields: ["booleanField"],
552
+ },
553
+ ],
89
554
  },
90
555
  ],
91
- },
92
- ];
93
-
94
- const result = countPickedFields(
95
- convertLinkCustomtypesToFieldCheckMap(customtypes),
96
- );
556
+ allCustomTypes: [customType],
557
+ });
97
558
 
98
- expect(result).toEqual({
99
- pickedFields: 2,
100
- nestedPickedFields: 0,
559
+ expect(result).toEqual({
560
+ customType: {
561
+ groupField: {
562
+ type: "group",
563
+ value: {
564
+ booleanField: {
565
+ type: "checkbox",
566
+ value: true,
567
+ },
568
+ },
569
+ },
570
+ },
571
+ });
101
572
  });
102
- });
103
573
 
104
- it("should count picked fields with custom type as object with group field and nested custom type", () => {
105
- const customtypes = [
106
- {
107
- id: "ct1",
108
- fields: [
574
+ it("should include a correctly referenced content relationship field", () => {
575
+ const customType: CustomType = {
576
+ id: "customType",
577
+ label: "Custom Type",
578
+ repeatable: false,
579
+ status: true,
580
+ json: {
581
+ Main: {
582
+ booleanField: {
583
+ type: "Boolean",
584
+ },
585
+ },
586
+ },
587
+ };
588
+ const customTypeWithContentRelationship: CustomType = {
589
+ id: "customTypeWithContentRelationship",
590
+ label: "Custom Type With Content Relationship",
591
+ repeatable: false,
592
+ status: true,
593
+ json: {
594
+ Main: {
595
+ contentRelationshipField: {
596
+ type: "Link",
597
+ config: {
598
+ select: "document",
599
+ customtypes: ["customType"],
600
+ },
601
+ },
602
+ },
603
+ },
604
+ };
605
+
606
+ const result = convertLinkCustomtypesToFieldCheckMap({
607
+ linkCustomtypes: [
109
608
  {
110
- id: "g1",
609
+ id: "customTypeWithContentRelationship",
111
610
  fields: [
112
- "f1",
113
611
  {
114
- id: "f2",
612
+ id: "contentRelationshipField",
115
613
  customtypes: [
116
614
  {
117
- id: "ct2",
118
- fields: ["f1"],
615
+ id: "customType",
616
+ fields: ["booleanField"],
119
617
  },
120
618
  ],
121
619
  },
122
620
  ],
123
621
  },
124
622
  ],
125
- },
126
- ];
623
+ allCustomTypes: [customType, customTypeWithContentRelationship],
624
+ });
127
625
 
128
- const result = countPickedFields(
129
- convertLinkCustomtypesToFieldCheckMap(customtypes),
130
- );
131
-
132
- expect(result).toEqual({
133
- pickedFields: 2,
134
- nestedPickedFields: 1,
626
+ expect(result).toEqual({
627
+ customTypeWithContentRelationship: {
628
+ contentRelationshipField: {
629
+ type: "contentRelationship",
630
+ value: {
631
+ customType: {
632
+ booleanField: {
633
+ type: "checkbox",
634
+ value: true,
635
+ },
636
+ },
637
+ },
638
+ },
639
+ },
640
+ });
135
641
  });
136
- });
137
642
 
138
- it("should count picked fields with custom type as object with group field, nested custom type and nested group field", () => {
139
- const customtypes = [
140
- {
141
- id: "ct1",
142
- fields: [
643
+ it("should include a field correctly referenced as a group inside a nested custom type", () => {
644
+ const customType: CustomType = {
645
+ id: "customType",
646
+ label: "Custom Type",
647
+ repeatable: false,
648
+ status: true,
649
+ json: {
650
+ Main: {
651
+ groupField: {
652
+ type: "Group",
653
+ config: {
654
+ label: "Group Field",
655
+ fields: {
656
+ booleanField: {
657
+ type: "Boolean",
658
+ config: {
659
+ label: "Boolean Field",
660
+ },
661
+ },
662
+ },
663
+ },
664
+ },
665
+ },
666
+ },
667
+ };
668
+
669
+ const customTypeWithContentRelationship: CustomType = {
670
+ id: "customTypeWithContentRelationship",
671
+ label: "Custom Type With Content Relationship",
672
+ repeatable: false,
673
+ status: true,
674
+ json: {
675
+ Main: {
676
+ contentRelationshipField: {
677
+ type: "Link",
678
+ config: {
679
+ select: "document",
680
+ customtypes: ["customType"],
681
+ },
682
+ },
683
+ },
684
+ },
685
+ };
686
+
687
+ const result = convertLinkCustomtypesToFieldCheckMap({
688
+ linkCustomtypes: [
143
689
  {
144
- id: "g1",
690
+ id: "customTypeWithContentRelationship",
145
691
  fields: [
146
- "f1",
147
692
  {
148
- id: "f2",
693
+ id: "contentRelationshipField",
149
694
  customtypes: [
150
695
  {
151
- id: "ct2",
696
+ id: "customType",
152
697
  fields: [
153
- "f1",
154
698
  {
155
- id: "g2",
156
- fields: ["f1", "f2"],
699
+ id: "groupField",
700
+ fields: ["booleanField"],
157
701
  },
158
702
  ],
159
703
  },
@@ -162,104 +706,618 @@ describe("ContentRelationshipFieldPicker", () => {
162
706
  ],
163
707
  },
164
708
  ],
165
- },
166
- ];
709
+ allCustomTypes: [customType, customTypeWithContentRelationship],
710
+ });
711
+
712
+ expect(result).toEqual({
713
+ customTypeWithContentRelationship: {
714
+ contentRelationshipField: {
715
+ type: "contentRelationship",
716
+ value: {
717
+ customType: {
718
+ groupField: {
719
+ type: "group",
720
+ value: {
721
+ booleanField: {
722
+ type: "checkbox",
723
+ value: true,
724
+ },
725
+ },
726
+ },
727
+ },
728
+ },
729
+ },
730
+ },
731
+ });
732
+ });
167
733
 
168
- const result = countPickedFields(
169
- convertLinkCustomtypesToFieldCheckMap(customtypes),
170
- );
734
+ it("should discard non-existing/invalid referenced fields", () => {
735
+ const customType2: CustomType = {
736
+ id: "customType2",
737
+ label: "Custom Type 2",
738
+ repeatable: false,
739
+ status: true,
740
+ json: {
741
+ Main: {
742
+ booleanField: {
743
+ type: "Boolean",
744
+ },
745
+ },
746
+ },
747
+ };
171
748
 
172
- expect(result).toEqual({
173
- pickedFields: 4,
174
- nestedPickedFields: 3,
749
+ const customType: CustomType = {
750
+ id: "customType",
751
+ label: "Custom Type",
752
+ repeatable: false,
753
+ status: true,
754
+ json: {
755
+ Main: {
756
+ mdField: {
757
+ type: "StructuredText",
758
+ },
759
+ crField: {
760
+ type: "Link",
761
+ config: {
762
+ select: "document",
763
+ customtypes: ["customType2"],
764
+ },
765
+ },
766
+ groupField: {
767
+ type: "Group",
768
+ config: {
769
+ fields: {
770
+ groupFieldA: {
771
+ type: "Boolean",
772
+ },
773
+ },
774
+ },
775
+ },
776
+ },
777
+ },
778
+ };
779
+
780
+ const result = convertLinkCustomtypesToFieldCheckMap({
781
+ linkCustomtypes: [
782
+ {
783
+ id: "customType",
784
+ fields: [
785
+ "mdField",
786
+ "nonExistingField",
787
+ {
788
+ id: "nonExistingGroup",
789
+ fields: ["groupFieldA"],
790
+ },
791
+ {
792
+ id: "crField",
793
+ customtypes: [
794
+ {
795
+ id: "customType2",
796
+ fields: ["nonExistingNestedField"],
797
+ },
798
+ ],
799
+ },
800
+ {
801
+ id: "groupField",
802
+ fields: ["nonExistingGroupField"],
803
+ },
804
+ ],
805
+ },
806
+ ],
807
+ allCustomTypes: [customType, customType2],
808
+ });
809
+
810
+ expect(result).toEqual({
811
+ customType: {
812
+ mdField: {
813
+ type: "checkbox",
814
+ value: true,
815
+ },
816
+ },
817
+ });
175
818
  });
176
- });
177
819
 
178
- it("should count picked fields with custom type as object with nested custom type and nested group field", () => {
179
- const customtypes = [
180
- {
181
- id: "ct1",
182
- fields: [
183
- "f1",
820
+ it("should discard invalid fields (uid, slices & choice)", () => {
821
+ const customType: CustomType = {
822
+ id: "customType",
823
+ label: "Custom Type",
824
+ repeatable: false,
825
+ status: true,
826
+ json: {
827
+ Main: {
828
+ booleanField: {
829
+ type: "Boolean",
830
+ },
831
+ typeUid: {
832
+ type: "UID",
833
+ },
834
+ uid: {
835
+ type: "Boolean",
836
+ },
837
+ choice: {
838
+ type: "Choice",
839
+ config: {
840
+ choices: {
841
+ choice1: {
842
+ type: "Boolean",
843
+ },
844
+ },
845
+ },
846
+ },
847
+ slices: {
848
+ type: "Slices",
849
+ config: {
850
+ choices: {
851
+ slice1: {
852
+ type: "SharedSlice",
853
+ },
854
+ },
855
+ },
856
+ },
857
+ },
858
+ },
859
+ };
860
+
861
+ const result = convertLinkCustomtypesToFieldCheckMap({
862
+ linkCustomtypes: [
184
863
  {
185
- id: "f2",
186
- customtypes: [
864
+ id: "customType",
865
+ fields: ["booleanField", "typeUid", "uid", "choice", "slices"],
866
+ },
867
+ ],
868
+ allCustomTypes: [customType],
869
+ });
870
+
871
+ expect(result).toEqual({
872
+ customType: {
873
+ booleanField: {
874
+ type: "checkbox",
875
+ value: true,
876
+ },
877
+ },
878
+ });
879
+ });
880
+
881
+ it("should discard a regular field referenced as a group field", () => {
882
+ const customType: CustomType = {
883
+ id: "customType",
884
+ label: "Custom Type",
885
+ repeatable: false,
886
+ status: true,
887
+ json: {
888
+ Main: {
889
+ booleanField: {
890
+ type: "Boolean",
891
+ },
892
+ },
893
+ },
894
+ };
895
+
896
+ const result = convertLinkCustomtypesToFieldCheckMap({
897
+ linkCustomtypes: [
898
+ {
899
+ id: "customType",
900
+ fields: [
187
901
  {
188
- id: "ct2",
189
- fields: [
190
- "f1",
902
+ id: "booleanField",
903
+ fields: ["something"],
904
+ },
905
+ ],
906
+ },
907
+ ],
908
+ allCustomTypes: [customType],
909
+ });
910
+
911
+ expect(result).toEqual({});
912
+ });
913
+
914
+ it("should discard a regular field referenced as a content relationship field", () => {
915
+ const customType: CustomType = {
916
+ id: "customType",
917
+ label: "Custom Type",
918
+ repeatable: false,
919
+ status: true,
920
+ json: {
921
+ Main: {
922
+ booleanField: {
923
+ type: "Boolean",
924
+ },
925
+ },
926
+ },
927
+ };
928
+
929
+ const result = convertLinkCustomtypesToFieldCheckMap({
930
+ linkCustomtypes: [
931
+ {
932
+ id: "customType",
933
+ fields: [
934
+ {
935
+ id: "booleanField",
936
+ customtypes: [],
937
+ },
938
+ ],
939
+ },
940
+ ],
941
+ allCustomTypes: [customType],
942
+ });
943
+
944
+ expect(result).toEqual({});
945
+ });
946
+
947
+ it("should discard a group field referenced as a regular field (string/no picked fields)", () => {
948
+ const customType: CustomType = {
949
+ id: "customType",
950
+ label: "Custom Type",
951
+ repeatable: false,
952
+ status: true,
953
+ json: {
954
+ Main: {
955
+ groupField: {
956
+ type: "Group",
957
+ config: {
958
+ fields: {
959
+ booleanField: {
960
+ type: "Boolean",
961
+ },
962
+ },
963
+ },
964
+ },
965
+ },
966
+ },
967
+ };
968
+ const customTypeWithContentRelationship: CustomType = {
969
+ id: "customTypeWithContentRelationship",
970
+ label: "Custom Type With Content Relationship",
971
+ repeatable: false,
972
+ status: true,
973
+ json: {
974
+ Main: {
975
+ groupField: {
976
+ type: "Group",
977
+ config: {
978
+ fields: {
979
+ booleanField: {
980
+ type: "Boolean",
981
+ },
982
+ },
983
+ },
984
+ },
985
+ contentRelationshipField: {
986
+ type: "Link",
987
+ config: {
988
+ select: "document",
989
+ customtypes: ["customType"],
990
+ },
991
+ },
992
+ },
993
+ },
994
+ };
995
+
996
+ const result = convertLinkCustomtypesToFieldCheckMap({
997
+ linkCustomtypes: [
998
+ {
999
+ id: "customTypeWithContentRelationship",
1000
+ fields: [
1001
+ "groupField",
1002
+ {
1003
+ id: "contentRelationshipField",
1004
+ customtypes: [
191
1005
  {
192
- id: "g2",
193
- fields: ["f1", "f2"],
1006
+ id: "customType",
1007
+ fields: ["groupField"],
194
1008
  },
195
1009
  ],
196
1010
  },
197
1011
  ],
198
1012
  },
199
1013
  ],
200
- },
201
- ];
1014
+ allCustomTypes: [customType, customTypeWithContentRelationship],
1015
+ });
1016
+
1017
+ expect(result).toEqual({});
1018
+ });
202
1019
 
203
- const result = countPickedFields(
204
- convertLinkCustomtypesToFieldCheckMap(customtypes),
205
- );
1020
+ it("should discard a content relationship field reference to a regular field", () => {
1021
+ const customTypeA: CustomType = {
1022
+ id: "customTypeA",
1023
+ label: "Custom Type A",
1024
+ repeatable: false,
1025
+ status: true,
1026
+ json: {
1027
+ Main: {
1028
+ booleanField: {
1029
+ type: "Boolean",
1030
+ },
1031
+ },
1032
+ },
1033
+ };
1034
+ const customTypeB: CustomType = {
1035
+ id: "customTypeB",
1036
+ label: "Custom Type B",
1037
+ repeatable: false,
1038
+ status: true,
1039
+ json: {
1040
+ Main: {
1041
+ colorField: {
1042
+ type: "Color",
1043
+ },
1044
+ },
1045
+ },
1046
+ };
206
1047
 
207
- expect(result).toEqual({
208
- pickedFields: 4,
209
- nestedPickedFields: 3,
1048
+ const result = convertLinkCustomtypesToFieldCheckMap({
1049
+ linkCustomtypes: [
1050
+ {
1051
+ id: "customTypeA",
1052
+ fields: [
1053
+ {
1054
+ id: "booleanField",
1055
+ customtypes: [
1056
+ {
1057
+ id: "customTypeB",
1058
+ fields: ["colorField"],
1059
+ },
1060
+ ],
1061
+ },
1062
+ ],
1063
+ },
1064
+ ],
1065
+ allCustomTypes: [customTypeB, customTypeA],
1066
+ });
1067
+
1068
+ expect(result).toEqual({});
210
1069
  });
211
- });
212
1070
 
213
- it("should count picked fields with custom type as object with nested custom type without fields", () => {
214
- const customtypes = [
215
- {
216
- id: "ct1",
217
- fields: [
218
- "f1",
1071
+ it("should discard a content relationship field if it references a non existing custom type", () => {
1072
+ const customTypeWithField: CustomType = {
1073
+ id: "customTypeWithField",
1074
+ label: "Custom Type With Field",
1075
+ repeatable: false,
1076
+ status: true,
1077
+ json: {
1078
+ Main: {
1079
+ colorField: {
1080
+ type: "Color",
1081
+ config: {
1082
+ label: "Color Field",
1083
+ },
1084
+ },
1085
+ },
1086
+ },
1087
+ };
1088
+ const customType: CustomType = {
1089
+ id: "customType",
1090
+ label: "Custom Type",
1091
+ repeatable: false,
1092
+ status: true,
1093
+ json: {
1094
+ Main: {
1095
+ contentRelationshipField: {
1096
+ type: "Link",
1097
+ config: {
1098
+ select: "document",
1099
+ customtypes: ["customTypeWithField"],
1100
+ },
1101
+ },
1102
+ groupField: {
1103
+ type: "Group",
1104
+ config: {
1105
+ fields: {
1106
+ contentRelationshipFieldInsideGroup: {
1107
+ type: "Link",
1108
+ config: {
1109
+ select: "document",
1110
+ customtypes: ["customTypeWithField"],
1111
+ },
1112
+ },
1113
+ },
1114
+ },
1115
+ },
1116
+ },
1117
+ },
1118
+ };
1119
+
1120
+ const result = convertLinkCustomtypesToFieldCheckMap({
1121
+ linkCustomtypes: [
219
1122
  {
220
- id: "f2",
221
- customtypes: [
1123
+ id: "customType",
1124
+ fields: [
222
1125
  {
223
- id: "ct2",
224
- fields: [],
1126
+ id: "contentRelationshipField",
1127
+ customtypes: [
1128
+ {
1129
+ id: "nonExistingCustomType",
1130
+ fields: ["colorField"],
1131
+ },
1132
+ ],
1133
+ },
1134
+ {
1135
+ id: "groupField",
1136
+ fields: [
1137
+ {
1138
+ id: "contentRelationshipFieldInsideGroup",
1139
+ customtypes: [
1140
+ {
1141
+ id: "nonExistingCustomType",
1142
+ fields: ["colorField"],
1143
+ },
1144
+ ],
1145
+ },
1146
+ ],
225
1147
  },
226
1148
  ],
227
1149
  },
228
1150
  ],
229
- },
230
- ];
1151
+ allCustomTypes: [customType, customTypeWithField],
1152
+ });
1153
+
1154
+ expect(result).toEqual({});
1155
+ });
231
1156
 
232
- const result = countPickedFields(
233
- convertLinkCustomtypesToFieldCheckMap(customtypes),
234
- );
1157
+ it("should discard content relationship fields referenced at a depth above 2", () => {
1158
+ const thirdCustomType: CustomType = {
1159
+ id: "thirdCustomType",
1160
+ label: "Third Custom Type",
1161
+ repeatable: false,
1162
+ status: true,
1163
+ json: {
1164
+ Main: {
1165
+ booleanField: {
1166
+ type: "Boolean",
1167
+ },
1168
+ },
1169
+ },
1170
+ };
1171
+ const secondCustomType: CustomType = {
1172
+ id: "secondCustomType",
1173
+ label: "Second Custom Type",
1174
+ repeatable: false,
1175
+ status: true,
1176
+ json: {
1177
+ Main: {
1178
+ secondCr: {
1179
+ type: "Link",
1180
+ config: {
1181
+ select: "document",
1182
+ customtypes: ["customType2"],
1183
+ },
1184
+ },
1185
+ },
1186
+ },
1187
+ };
1188
+ const firstCustomType: CustomType = {
1189
+ id: "firstCustomType",
1190
+ label: "First Custom Type",
1191
+ repeatable: false,
1192
+ status: true,
1193
+ json: {
1194
+ Main: {
1195
+ firstCr: {
1196
+ type: "Link",
1197
+ config: {
1198
+ select: "document",
1199
+ customtypes: ["customType"],
1200
+ },
1201
+ },
1202
+ },
1203
+ },
1204
+ };
1205
+
1206
+ const result = convertLinkCustomtypesToFieldCheckMap({
1207
+ linkCustomtypes: [
1208
+ {
1209
+ id: "firstCustomType",
1210
+ fields: [
1211
+ {
1212
+ id: "firstCr",
1213
+ customtypes: [
1214
+ {
1215
+ id: "secondCustomType",
1216
+ fields: [
1217
+ {
1218
+ id: "secondCr",
1219
+ // @ts-expect-error - this is a test
1220
+ customtypes: [
1221
+ {
1222
+ id: "thirdCustomType",
1223
+ },
1224
+ ],
1225
+ },
1226
+ ],
1227
+ },
1228
+ ],
1229
+ },
1230
+ ],
1231
+ },
1232
+ ],
1233
+ allCustomTypes: [secondCustomType, thirdCustomType, firstCustomType],
1234
+ });
235
1235
 
236
- expect(result).toEqual({
237
- pickedFields: 1,
238
- nestedPickedFields: 0,
1236
+ expect(result).toEqual({});
239
1237
  });
240
- });
241
1238
 
242
- it("should count picked fields with custom type as object with group field without fields", () => {
243
- const customtypes = [
244
- {
245
- id: "ct1",
246
- fields: [
247
- "f1",
1239
+ it("should not check for valid fields if allCustomTypes prop is not provided", () => {
1240
+ const result = convertLinkCustomtypesToFieldCheckMap({
1241
+ linkCustomtypes: [
248
1242
  {
249
- id: "g1",
250
- fields: [],
1243
+ id: "customTypeA",
1244
+ fields: [
1245
+ "fieldA",
1246
+ {
1247
+ id: "groupA",
1248
+ fields: ["groupFieldA"],
1249
+ },
1250
+ {
1251
+ id: "contentRelationshipA",
1252
+ customtypes: [
1253
+ {
1254
+ id: "customTypeB",
1255
+ fields: ["fieldB"],
1256
+ },
1257
+ ],
1258
+ },
1259
+ ],
251
1260
  },
252
1261
  ],
253
- },
254
- ];
1262
+ });
255
1263
 
256
- const result = countPickedFields(
257
- convertLinkCustomtypesToFieldCheckMap(customtypes),
258
- );
1264
+ expect(result).toEqual({
1265
+ customTypeA: {
1266
+ fieldA: {
1267
+ type: "checkbox",
1268
+ value: true,
1269
+ },
1270
+ groupA: {
1271
+ type: "group",
1272
+ value: {
1273
+ groupFieldA: {
1274
+ type: "checkbox",
1275
+ value: true,
1276
+ },
1277
+ },
1278
+ },
1279
+ contentRelationshipA: {
1280
+ type: "contentRelationship",
1281
+ value: {
1282
+ customTypeB: {
1283
+ fieldB: {
1284
+ type: "checkbox",
1285
+ value: true,
1286
+ },
1287
+ },
1288
+ },
1289
+ },
1290
+ },
1291
+ });
1292
+ });
1293
+
1294
+ it("should check for valid fields if allCustomTypes is empty", () => {
1295
+ const result = convertLinkCustomtypesToFieldCheckMap({
1296
+ linkCustomtypes: [
1297
+ {
1298
+ id: "customTypeA",
1299
+ fields: [
1300
+ "fieldA",
1301
+ {
1302
+ id: "groupA",
1303
+ fields: ["groupFieldA"],
1304
+ },
1305
+ {
1306
+ id: "contentRelationshipA",
1307
+ customtypes: [
1308
+ {
1309
+ id: "customTypeB",
1310
+ fields: ["fieldB"],
1311
+ },
1312
+ ],
1313
+ },
1314
+ ],
1315
+ },
1316
+ ],
1317
+ allCustomTypes: [],
1318
+ });
259
1319
 
260
- expect(result).toEqual({
261
- pickedFields: 1,
262
- nestedPickedFields: 0,
1320
+ expect(result).toEqual({});
263
1321
  });
264
1322
  });
265
1323
  });