@payloadcms/plugin-import-export 4.0.0-internal.4a8e202 → 4.0.0-internal.4c2ee12

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.
Files changed (141) hide show
  1. package/dist/components/CollectionField/index.d.ts +3 -2
  2. package/dist/components/CollectionField/index.d.ts.map +1 -1
  3. package/dist/components/CollectionField/index.js.map +1 -1
  4. package/dist/components/ExportListMenuItem/index.d.ts.map +1 -1
  5. package/dist/components/ExportListMenuItem/index.js +14 -12
  6. package/dist/components/ExportListMenuItem/index.js.map +1 -1
  7. package/dist/components/ExportPreview/index.css +0 -3
  8. package/dist/components/ExportPreview/index.d.ts.map +1 -1
  9. package/dist/components/ExportPreview/index.js +11 -1
  10. package/dist/components/ExportPreview/index.js.map +1 -1
  11. package/dist/components/FieldsToExport/index.d.ts +3 -2
  12. package/dist/components/FieldsToExport/index.d.ts.map +1 -1
  13. package/dist/components/FieldsToExport/index.js +18 -15
  14. package/dist/components/FieldsToExport/index.js.map +1 -1
  15. package/dist/components/FieldsToExport/reduceFields.d.ts +6 -0
  16. package/dist/components/FieldsToExport/reduceFields.d.ts.map +1 -1
  17. package/dist/components/FieldsToExport/reduceFields.js +4 -0
  18. package/dist/components/FieldsToExport/reduceFields.js.map +1 -1
  19. package/dist/components/FormatField/index.d.ts +3 -2
  20. package/dist/components/FormatField/index.d.ts.map +1 -1
  21. package/dist/components/FormatField/index.js.map +1 -1
  22. package/dist/components/ImportListMenuItem/index.d.ts.map +1 -1
  23. package/dist/components/ImportListMenuItem/index.js +14 -12
  24. package/dist/components/ImportListMenuItem/index.js.map +1 -1
  25. package/dist/components/ImportPreview/index.css +0 -3
  26. package/dist/components/ImportPreview/index.d.ts.map +1 -1
  27. package/dist/components/ImportPreview/index.js +27 -8
  28. package/dist/components/ImportPreview/index.js.map +1 -1
  29. package/dist/components/LimitField/index.d.ts +3 -2
  30. package/dist/components/LimitField/index.d.ts.map +1 -1
  31. package/dist/components/LimitField/index.js.map +1 -1
  32. package/dist/components/LocaleField/index.d.ts +3 -2
  33. package/dist/components/LocaleField/index.d.ts.map +1 -1
  34. package/dist/components/LocaleField/index.js.map +1 -1
  35. package/dist/components/Page/index.d.ts +3 -2
  36. package/dist/components/Page/index.d.ts.map +1 -1
  37. package/dist/components/Page/index.js.map +1 -1
  38. package/dist/components/RelationshipCell/getRelationshipGroupKey.d.ts +7 -0
  39. package/dist/components/RelationshipCell/getRelationshipGroupKey.d.ts.map +1 -0
  40. package/dist/components/RelationshipCell/getRelationshipGroupKey.js +3 -0
  41. package/dist/components/RelationshipCell/getRelationshipGroupKey.js.map +1 -0
  42. package/dist/components/RelationshipCell/getRelationshipGroups.d.ts +43 -0
  43. package/dist/components/RelationshipCell/getRelationshipGroups.d.ts.map +1 -0
  44. package/dist/components/RelationshipCell/getRelationshipGroups.js +109 -0
  45. package/dist/components/RelationshipCell/getRelationshipGroups.js.map +1 -0
  46. package/dist/components/RelationshipCell/index.css +30 -0
  47. package/dist/components/RelationshipCell/index.d.ts +15 -0
  48. package/dist/components/RelationshipCell/index.d.ts.map +1 -0
  49. package/dist/components/RelationshipCell/index.js +57 -0
  50. package/dist/components/RelationshipCell/index.js.map +1 -0
  51. package/dist/components/SortBy/index.d.ts +3 -2
  52. package/dist/components/SortBy/index.d.ts.map +1 -1
  53. package/dist/components/SortBy/index.js.map +1 -1
  54. package/dist/components/SortOrder/index.d.ts +3 -2
  55. package/dist/components/SortOrder/index.d.ts.map +1 -1
  56. package/dist/components/SortOrder/index.js.map +1 -1
  57. package/dist/export/batchProcessor.d.ts +2 -1
  58. package/dist/export/batchProcessor.d.ts.map +1 -1
  59. package/dist/export/batchProcessor.js +6 -2
  60. package/dist/export/batchProcessor.js.map +1 -1
  61. package/dist/export/createExport.d.ts +2 -0
  62. package/dist/export/createExport.d.ts.map +1 -1
  63. package/dist/export/createExport.js +6 -1
  64. package/dist/export/createExport.js.map +1 -1
  65. package/dist/export/getCreateExportCollectionTask.d.ts.map +1 -1
  66. package/dist/export/getCreateExportCollectionTask.js +11 -0
  67. package/dist/export/getCreateExportCollectionTask.js.map +1 -1
  68. package/dist/export/getExportCollection.d.ts.map +1 -1
  69. package/dist/export/getExportCollection.js +5 -0
  70. package/dist/export/getExportCollection.js.map +1 -1
  71. package/dist/export/handleDownload.d.ts.map +1 -1
  72. package/dist/export/handleDownload.js +7 -3
  73. package/dist/export/handleDownload.js.map +1 -1
  74. package/dist/export/handlePreview.d.ts.map +1 -1
  75. package/dist/export/handlePreview.js +24 -5
  76. package/dist/export/handlePreview.js.map +1 -1
  77. package/dist/exports/types.d.ts +1 -1
  78. package/dist/exports/types.d.ts.map +1 -1
  79. package/dist/exports/types.js.map +1 -1
  80. package/dist/import/batchProcessor.d.ts +2 -1
  81. package/dist/import/batchProcessor.d.ts.map +1 -1
  82. package/dist/import/batchProcessor.js +15 -8
  83. package/dist/import/batchProcessor.js.map +1 -1
  84. package/dist/import/createImport.d.ts +3 -2
  85. package/dist/import/createImport.d.ts.map +1 -1
  86. package/dist/import/createImport.js +8 -1
  87. package/dist/import/createImport.js.map +1 -1
  88. package/dist/import/getCreateImportCollectionTask.d.ts.map +1 -1
  89. package/dist/import/getCreateImportCollectionTask.js +5 -2
  90. package/dist/import/getCreateImportCollectionTask.js.map +1 -1
  91. package/dist/import/getImportCollection.d.ts.map +1 -1
  92. package/dist/import/getImportCollection.js +2 -0
  93. package/dist/import/getImportCollection.js.map +1 -1
  94. package/dist/import/handlePreview.d.ts.map +1 -1
  95. package/dist/import/handlePreview.js +5 -1
  96. package/dist/import/handlePreview.js.map +1 -1
  97. package/dist/types.d.ts +17 -1
  98. package/dist/types.d.ts.map +1 -1
  99. package/dist/types.js.map +1 -1
  100. package/dist/utilities/getExportFieldFunctions.js +64 -27
  101. package/dist/utilities/getExportFieldFunctions.js.map +1 -1
  102. package/dist/utilities/getFileFromDoc.js +3 -3
  103. package/dist/utilities/getFileFromDoc.js.map +1 -1
  104. package/dist/utilities/getSelect.d.ts.map +1 -1
  105. package/dist/utilities/getSelect.js +11 -3
  106. package/dist/utilities/getSelect.js.map +1 -1
  107. package/dist/utilities/getSubmittedFormValues.d.ts +12 -0
  108. package/dist/utilities/getSubmittedFormValues.d.ts.map +1 -0
  109. package/dist/utilities/getSubmittedFormValues.js +24 -0
  110. package/dist/utilities/getSubmittedFormValues.js.map +1 -0
  111. package/dist/utilities/setNestedValue.d.ts +2 -1
  112. package/dist/utilities/setNestedValue.d.ts.map +1 -1
  113. package/dist/utilities/setNestedValue.js +65 -34
  114. package/dist/utilities/setNestedValue.js.map +1 -1
  115. package/package.json +9 -9
  116. package/dist/components/FieldsToExport/reduceFields.spec.js +0 -239
  117. package/dist/components/FieldsToExport/reduceFields.spec.js.map +0 -1
  118. package/dist/utilities/applyFieldHooks.spec.js +0 -205
  119. package/dist/utilities/applyFieldHooks.spec.js.map +0 -1
  120. package/dist/utilities/buildDisabledFieldRegex.spec.js +0 -64
  121. package/dist/utilities/buildDisabledFieldRegex.spec.js.map +0 -1
  122. package/dist/utilities/fieldToRegex.spec.js +0 -151
  123. package/dist/utilities/fieldToRegex.spec.js.map +0 -1
  124. package/dist/utilities/filterToSelectedFields.spec.js +0 -204
  125. package/dist/utilities/filterToSelectedFields.spec.js.map +0 -1
  126. package/dist/utilities/flattenObject.spec.js +0 -158
  127. package/dist/utilities/flattenObject.spec.js.map +0 -1
  128. package/dist/utilities/getExportFieldFunctions.spec.js +0 -50
  129. package/dist/utilities/getExportFieldFunctions.spec.js.map +0 -1
  130. package/dist/utilities/getImportFieldFunctions.spec.js +0 -167
  131. package/dist/utilities/getImportFieldFunctions.spec.js.map +0 -1
  132. package/dist/utilities/getSchemaColumns.spec.js +0 -157
  133. package/dist/utilities/getSchemaColumns.spec.js.map +0 -1
  134. package/dist/utilities/parseCSV.spec.js +0 -195
  135. package/dist/utilities/parseCSV.spec.js.map +0 -1
  136. package/dist/utilities/processRichTextField.spec.js +0 -308
  137. package/dist/utilities/processRichTextField.spec.js.map +0 -1
  138. package/dist/utilities/siblingDoc.spec.js +0 -278
  139. package/dist/utilities/siblingDoc.spec.js.map +0 -1
  140. package/dist/utilities/unflattenObject.spec.js +0 -819
  141. package/dist/utilities/unflattenObject.spec.js.map +0 -1
@@ -1,819 +0,0 @@
1
- import { getImportFieldFunctions } from './getImportFieldFunctions.js';
2
- import { unflattenObject } from './unflattenObject.js';
3
- import { describe, it, expect, vi } from 'vitest';
4
- describe('unflattenObject', ()=>{
5
- const mockReq = {
6
- payload: {
7
- logger: {
8
- error: vi.fn()
9
- }
10
- }
11
- };
12
- describe('hasMany number fields', ()=>{
13
- const fields = [
14
- {
15
- name: 'hasManyNumber',
16
- type: 'number',
17
- hasMany: true
18
- }
19
- ];
20
- it('should handle comma-separated number strings', ()=>{
21
- const data = {
22
- hasManyNumber: '1,2,3,5,8'
23
- };
24
- const result = unflattenObject({
25
- data,
26
- fields,
27
- req: mockReq
28
- });
29
- expect(result).toEqual({
30
- hasManyNumber: [
31
- 1,
32
- 2,
33
- 3,
34
- 5,
35
- 8
36
- ]
37
- });
38
- });
39
- it('should handle comma-separated numbers with spaces', ()=>{
40
- const data = {
41
- hasManyNumber: ' 10 , 20 , 30 '
42
- };
43
- const result = unflattenObject({
44
- data,
45
- fields,
46
- req: mockReq
47
- });
48
- expect(result).toEqual({
49
- hasManyNumber: [
50
- 10,
51
- 20,
52
- 30
53
- ]
54
- });
55
- });
56
- it('should filter out empty values in comma-separated strings', ()=>{
57
- const data = {
58
- hasManyNumber: '1,,3,,5'
59
- };
60
- const result = unflattenObject({
61
- data,
62
- fields,
63
- req: mockReq
64
- });
65
- expect(result).toEqual({
66
- hasManyNumber: [
67
- 1,
68
- 3,
69
- 5
70
- ]
71
- });
72
- });
73
- it('should handle single number values', ()=>{
74
- const data = {
75
- hasManyNumber: 42
76
- };
77
- const result = unflattenObject({
78
- data,
79
- fields,
80
- req: mockReq
81
- });
82
- expect(result).toEqual({
83
- hasManyNumber: [
84
- 42
85
- ]
86
- });
87
- });
88
- it('should handle single string number values', ()=>{
89
- const data = {
90
- hasManyNumber: '42'
91
- };
92
- const result = unflattenObject({
93
- data,
94
- fields,
95
- req: mockReq
96
- });
97
- expect(result).toEqual({
98
- hasManyNumber: [
99
- 42
100
- ]
101
- });
102
- });
103
- it('should handle indexed array format', ()=>{
104
- const data = {
105
- hasManyNumber_0: 1,
106
- hasManyNumber_1: 2,
107
- hasManyNumber_2: 3
108
- };
109
- const result = unflattenObject({
110
- data,
111
- fields,
112
- req: mockReq
113
- });
114
- expect(result).toEqual({
115
- hasManyNumber: [
116
- 1,
117
- 2,
118
- 3
119
- ]
120
- });
121
- });
122
- it('should filter out null and empty values from indexed arrays', ()=>{
123
- const data = {
124
- hasManyNumber_0: 1,
125
- hasManyNumber_1: null,
126
- hasManyNumber_2: '',
127
- hasManyNumber_3: 3
128
- };
129
- const result = unflattenObject({
130
- data,
131
- fields,
132
- req: mockReq
133
- });
134
- expect(result).toEqual({
135
- hasManyNumber: [
136
- 1,
137
- 3
138
- ]
139
- });
140
- });
141
- it('should handle empty, null, and undefined values', ()=>{
142
- // explicit null gets converted to empty array in postProcess for hasMany
143
- expect(unflattenObject({
144
- data: {
145
- hasManyNumber: null
146
- },
147
- fields,
148
- req: mockReq
149
- })).toEqual({
150
- hasManyNumber: []
151
- });
152
- // undefined is skipped entirely (preserves existing data)
153
- expect(unflattenObject({
154
- data: {
155
- hasManyNumber: undefined
156
- },
157
- fields,
158
- req: mockReq
159
- })).toEqual({});
160
- });
161
- });
162
- describe('hasMany relationship fields', ()=>{
163
- const fields = [
164
- {
165
- name: 'hasManyRelationship',
166
- type: 'relationship',
167
- hasMany: true,
168
- relationTo: 'posts'
169
- }
170
- ];
171
- it('should handle comma-separated ID strings', ()=>{
172
- const data = {
173
- hasManyRelationship: 'id1,id2,id3'
174
- };
175
- const result = unflattenObject({
176
- data,
177
- fields,
178
- req: mockReq
179
- });
180
- expect(result).toEqual({
181
- hasManyRelationship: [
182
- 'id1',
183
- 'id2',
184
- 'id3'
185
- ]
186
- });
187
- });
188
- it('should handle comma-separated IDs with spaces', ()=>{
189
- const data = {
190
- hasManyRelationship: ' id1 , id2 , id3 '
191
- };
192
- const result = unflattenObject({
193
- data,
194
- fields,
195
- req: mockReq
196
- });
197
- expect(result).toEqual({
198
- hasManyRelationship: [
199
- 'id1',
200
- 'id2',
201
- 'id3'
202
- ]
203
- });
204
- });
205
- it('should filter out empty values in comma-separated IDs', ()=>{
206
- const data = {
207
- hasManyRelationship: 'id1,,id3,,id5'
208
- };
209
- const result = unflattenObject({
210
- data,
211
- fields,
212
- req: mockReq
213
- });
214
- expect(result).toEqual({
215
- hasManyRelationship: [
216
- 'id1',
217
- 'id3',
218
- 'id5'
219
- ]
220
- });
221
- });
222
- it('should handle single ID values', ()=>{
223
- const data = {
224
- hasManyRelationship: 'singleId'
225
- };
226
- const result = unflattenObject({
227
- data,
228
- fields,
229
- req: mockReq
230
- });
231
- expect(result).toEqual({
232
- hasManyRelationship: [
233
- 'singleId'
234
- ]
235
- });
236
- });
237
- it('should handle indexed array format', ()=>{
238
- const data = {
239
- hasManyRelationship_0: 'id1',
240
- hasManyRelationship_1: 'id2',
241
- hasManyRelationship_2: 'id3'
242
- };
243
- const result = unflattenObject({
244
- data,
245
- fields,
246
- req: mockReq
247
- });
248
- expect(result).toEqual({
249
- hasManyRelationship: [
250
- 'id1',
251
- 'id2',
252
- 'id3'
253
- ]
254
- });
255
- });
256
- it('should handle MongoDB ObjectIDs', ()=>{
257
- const data = {
258
- hasManyRelationship: '507f1f77bcf86cd799439011,507f191e810c19729de860ea'
259
- };
260
- const result = unflattenObject({
261
- data,
262
- fields,
263
- req: mockReq
264
- });
265
- expect(result).toEqual({
266
- hasManyRelationship: [
267
- '507f1f77bcf86cd799439011',
268
- '507f191e810c19729de860ea'
269
- ]
270
- });
271
- });
272
- });
273
- describe('localized fields', ()=>{
274
- const fields = [
275
- {
276
- name: 'title',
277
- type: 'text',
278
- localized: true
279
- }
280
- ];
281
- it('should transform locale-specific keys to nested structure', ()=>{
282
- const data = {
283
- title_en: 'English Title',
284
- title_es: 'Título en Español'
285
- };
286
- const result = unflattenObject({
287
- data,
288
- fields,
289
- req: mockReq
290
- });
291
- expect(result).toEqual({
292
- title: {
293
- en: 'English Title',
294
- es: 'Título en Español'
295
- }
296
- });
297
- });
298
- it('should handle missing locales', ()=>{
299
- const data = {
300
- title_en: 'English Title'
301
- };
302
- const result = unflattenObject({
303
- data,
304
- fields,
305
- req: mockReq
306
- });
307
- expect(result).toEqual({
308
- title: {
309
- en: 'English Title'
310
- }
311
- });
312
- });
313
- });
314
- describe('blocks fields', ()=>{
315
- const fields = [
316
- {
317
- name: 'blocks',
318
- type: 'blocks'
319
- }
320
- ];
321
- it('should handle block fields with blockType', ()=>{
322
- const data = {
323
- blocks_0_hero_title: 'Hero Title',
324
- blocks_0_hero_subtitle: 'Hero Subtitle',
325
- blocks_0_hero_blockType: 'hero'
326
- };
327
- const result = unflattenObject({
328
- data,
329
- fields,
330
- req: mockReq
331
- });
332
- expect(result).toEqual({
333
- blocks: [
334
- {
335
- blockType: 'hero',
336
- title: 'Hero Title',
337
- subtitle: 'Hero Subtitle'
338
- }
339
- ]
340
- });
341
- });
342
- it('should handle multiple blocks', ()=>{
343
- const data = {
344
- blocks_0_hero_title: 'Hero Title',
345
- blocks_0_hero_blockType: 'hero',
346
- blocks_1_text_content: 'Text Content',
347
- blocks_1_text_blockType: 'text'
348
- };
349
- const result = unflattenObject({
350
- data,
351
- fields,
352
- req: mockReq
353
- });
354
- expect(result).toEqual({
355
- blocks: [
356
- {
357
- blockType: 'hero',
358
- title: 'Hero Title'
359
- },
360
- {
361
- blockType: 'text',
362
- content: 'Text Content'
363
- }
364
- ]
365
- });
366
- });
367
- });
368
- describe('array fields', ()=>{
369
- const fields = [
370
- {
371
- name: 'items',
372
- type: 'array'
373
- }
374
- ];
375
- it('should handle indexed array objects', ()=>{
376
- const data = {
377
- items_0_name: 'Item 1',
378
- items_0_value: 10,
379
- items_1_name: 'Item 2',
380
- items_1_value: 20
381
- };
382
- const result = unflattenObject({
383
- data,
384
- fields,
385
- req: mockReq
386
- });
387
- expect(result).toEqual({
388
- items: [
389
- {
390
- name: 'Item 1',
391
- value: 10
392
- },
393
- {
394
- name: 'Item 2',
395
- value: 20
396
- }
397
- ]
398
- });
399
- });
400
- it('should handle sparse arrays', ()=>{
401
- const data = {
402
- items_0_name: 'Item 1',
403
- items_2_name: 'Item 3'
404
- };
405
- const result = unflattenObject({
406
- data,
407
- fields,
408
- req: mockReq
409
- });
410
- expect(result).toEqual({
411
- items: [
412
- {
413
- name: 'Item 1'
414
- },
415
- null,
416
- {
417
- name: 'Item 3'
418
- }
419
- ]
420
- });
421
- });
422
- });
423
- describe('group fields', ()=>{
424
- const fields = [
425
- {
426
- name: 'group',
427
- type: 'group'
428
- }
429
- ];
430
- it('should handle nested group fields', ()=>{
431
- const data = {
432
- group_field1: 'Value 1',
433
- group_field2: 'Value 2'
434
- };
435
- const result = unflattenObject({
436
- data,
437
- fields,
438
- req: mockReq
439
- });
440
- expect(result).toEqual({
441
- group: {
442
- field1: 'Value 1',
443
- field2: 'Value 2'
444
- }
445
- });
446
- });
447
- });
448
- describe('polymorphic relationships', ()=>{
449
- const fields = [
450
- {
451
- name: 'polymorphic',
452
- type: 'relationship',
453
- relationTo: [
454
- 'posts',
455
- 'pages'
456
- ]
457
- }
458
- ];
459
- it('should handle polymorphic relationship with id and relationTo', ()=>{
460
- const data = {
461
- polymorphic_id: '123',
462
- polymorphic_relationTo: 'posts'
463
- };
464
- const result = unflattenObject({
465
- data,
466
- fields,
467
- req: mockReq
468
- });
469
- expect(result).toEqual({
470
- polymorphic: {
471
- relationTo: 'posts',
472
- value: '123'
473
- }
474
- });
475
- });
476
- it('should handle explicitly null polymorphic relationships', ()=>{
477
- const data = {
478
- polymorphic_id: null,
479
- polymorphic_relationTo: null
480
- };
481
- const result = unflattenObject({
482
- data,
483
- fields,
484
- req: mockReq
485
- });
486
- expect(result).toEqual({
487
- polymorphic: null
488
- });
489
- });
490
- it('should skip polymorphic relationships with undefined values', ()=>{
491
- const data = {
492
- polymorphic_id: undefined,
493
- polymorphic_relationTo: undefined
494
- };
495
- const result = unflattenObject({
496
- data,
497
- fields,
498
- req: mockReq
499
- });
500
- // Both undefined means field is not set (preserves existing data)
501
- expect(result).toEqual({});
502
- });
503
- it('should skip polymorphic relationship with undefined id', ()=>{
504
- const data = {
505
- polymorphic_id: undefined,
506
- polymorphic_relationTo: 'posts'
507
- };
508
- const result = unflattenObject({
509
- data,
510
- fields,
511
- req: mockReq
512
- });
513
- // Undefined ID means don't update this field
514
- expect(result).toEqual({});
515
- });
516
- it('should skip polymorphic relationship with undefined relationTo', ()=>{
517
- const data = {
518
- polymorphic_id: '123',
519
- polymorphic_relationTo: undefined
520
- };
521
- const result = unflattenObject({
522
- data,
523
- fields,
524
- req: mockReq
525
- });
526
- // Undefined relationTo means don't update this field
527
- expect(result).toEqual({});
528
- });
529
- it('should handle polymorphic hasMany relationships', ()=>{
530
- const fields = [
531
- {
532
- name: 'polymorphicMany',
533
- type: 'relationship',
534
- hasMany: true,
535
- relationTo: [
536
- 'posts',
537
- 'pages'
538
- ]
539
- }
540
- ];
541
- const data = {
542
- polymorphicMany_0_id: '123',
543
- polymorphicMany_0_relationTo: 'posts',
544
- polymorphicMany_1_id: '456',
545
- polymorphicMany_1_relationTo: 'pages'
546
- };
547
- const result = unflattenObject({
548
- data,
549
- fields,
550
- req: mockReq
551
- });
552
- expect(result).toEqual({
553
- polymorphicMany: [
554
- {
555
- relationTo: 'posts',
556
- value: '123'
557
- },
558
- {
559
- relationTo: 'pages',
560
- value: '456'
561
- }
562
- ]
563
- });
564
- });
565
- it('should filter out empty polymorphic hasMany items', ()=>{
566
- const fields = [
567
- {
568
- name: 'polymorphicMany',
569
- type: 'relationship',
570
- hasMany: true,
571
- relationTo: [
572
- 'posts',
573
- 'pages'
574
- ]
575
- }
576
- ];
577
- const data = {
578
- polymorphicMany_0_id: '123',
579
- polymorphicMany_0_relationTo: 'posts',
580
- polymorphicMany_1_id: null,
581
- polymorphicMany_1_relationTo: null,
582
- polymorphicMany_2_id: '456',
583
- polymorphicMany_2_relationTo: 'pages'
584
- };
585
- const result = unflattenObject({
586
- data,
587
- fields,
588
- req: mockReq
589
- });
590
- expect(result).toEqual({
591
- polymorphicMany: [
592
- {
593
- relationTo: 'posts',
594
- value: '123'
595
- },
596
- {
597
- relationTo: 'pages',
598
- value: '456'
599
- }
600
- ]
601
- });
602
- });
603
- it('should handle all empty polymorphic hasMany items', ()=>{
604
- const fields = [
605
- {
606
- name: 'polymorphicMany',
607
- type: 'relationship',
608
- hasMany: true,
609
- relationTo: [
610
- 'posts',
611
- 'pages'
612
- ]
613
- }
614
- ];
615
- const data = {
616
- polymorphicMany_0_id: null,
617
- polymorphicMany_0_relationTo: null,
618
- polymorphicMany_1_id: '',
619
- polymorphicMany_1_relationTo: ''
620
- };
621
- const result = unflattenObject({
622
- data,
623
- fields,
624
- req: mockReq
625
- });
626
- expect(result).toEqual({
627
- polymorphicMany: []
628
- });
629
- });
630
- });
631
- describe('field-hook key resolution', ()=>{
632
- it('should fire an import hook on a digit-only field name nested inside an array', ()=>{
633
- const fields = [
634
- {
635
- name: 'archives',
636
- type: 'array',
637
- flattenedFields: [
638
- {
639
- name: '2024',
640
- type: 'text'
641
- }
642
- ]
643
- }
644
- ];
645
- const importFieldHooks = {
646
- archives_2024: {
647
- type: 'beforeImport',
648
- fn: ({ value })=>`transformed-${value}`
649
- }
650
- };
651
- const data = {
652
- archives_0_2024: 'raw'
653
- };
654
- const result = unflattenObject({
655
- data,
656
- fields,
657
- importFieldHooks,
658
- req: mockReq
659
- });
660
- const archives = result.archives;
661
- expect(archives[0]['2024']).toBe('transformed-raw');
662
- });
663
- });
664
- describe('blocks with nested arrays', ()=>{
665
- const faqFields = [
666
- {
667
- name: 'faqContent',
668
- type: 'blocks',
669
- blocks: [
670
- {
671
- slug: 'faqSection',
672
- flattenedFields: [
673
- {
674
- name: 'faqs',
675
- type: 'array',
676
- flattenedFields: [
677
- {
678
- name: 'id',
679
- type: 'text'
680
- },
681
- {
682
- name: 'question',
683
- type: 'text'
684
- },
685
- {
686
- name: 'answer',
687
- type: 'richText'
688
- }
689
- ]
690
- }
691
- ]
692
- }
693
- ]
694
- }
695
- ];
696
- it('should unflatten a nested array inside a block as an array, not an object with numeric keys', ()=>{
697
- const data = {
698
- faqContent_0_faqSection_id: '6a1714b81e5f4cdbb51f18b1',
699
- faqContent_0_faqSection_faqs_0_id: '6a1714c01e5f4cdbb51f18b2',
700
- faqContent_0_faqSection_faqs_0_question: 'ipsum',
701
- faqContent_0_faqSection_blockType: 'faqSection'
702
- };
703
- const result = unflattenObject({
704
- data,
705
- fields: faqFields,
706
- req: mockReq
707
- });
708
- const faqContent = result.faqContent;
709
- expect(Array.isArray(faqContent)).toBe(true);
710
- expect(faqContent).toHaveLength(1);
711
- expect(faqContent[0].blockType).toBe('faqSection');
712
- const faqs = faqContent[0].faqs;
713
- expect(Array.isArray(faqs)).toBe(true);
714
- const faqItems = faqs;
715
- expect(faqItems[0].question).toBe('ipsum');
716
- expect(faqItems[0].id).toBe('6a1714c01e5f4cdbb51f18b2');
717
- });
718
- it('should parse a richText JSON string inside a nested array within a block', ()=>{
719
- const richTextValue = {
720
- root: {
721
- children: [
722
- {
723
- children: [
724
- {
725
- detail: 0,
726
- format: 0,
727
- mode: 'normal',
728
- text: 'porksum',
729
- type: 'text',
730
- version: 1
731
- }
732
- ],
733
- direction: null,
734
- format: '',
735
- indent: 0,
736
- type: 'paragraph',
737
- version: 1
738
- }
739
- ],
740
- direction: null,
741
- format: '',
742
- indent: 0,
743
- type: 'root',
744
- version: 1
745
- }
746
- };
747
- const data = {
748
- faqContent_0_faqSection_id: '6a1714b81e5f4cdbb51f18b1',
749
- faqContent_0_faqSection_faqs_0_id: '6a1714c01e5f4cdbb51f18b2',
750
- faqContent_0_faqSection_faqs_0_question: 'ipsum',
751
- faqContent_0_faqSection_faqs_0_answer: JSON.stringify(richTextValue),
752
- faqContent_0_faqSection_blockType: 'faqSection'
753
- };
754
- const importFieldHooks = getImportFieldFunctions({
755
- fields: faqFields
756
- });
757
- const result = unflattenObject({
758
- data,
759
- fields: faqFields,
760
- importFieldHooks,
761
- req: mockReq
762
- });
763
- const faqContent = result.faqContent;
764
- const faqs = faqContent[0].faqs;
765
- expect(typeof faqs[0].answer).not.toBe('string');
766
- expect(faqs[0].answer).toEqual(richTextValue);
767
- });
768
- });
769
- describe('edge cases', ()=>{
770
- it('should handle empty data', ()=>{
771
- const result = unflattenObject({
772
- data: {},
773
- fields: [],
774
- req: mockReq
775
- });
776
- expect(result).toEqual({});
777
- });
778
- it('should handle null data', ()=>{
779
- const result = unflattenObject({
780
- data: null,
781
- fields: [],
782
- req: mockReq
783
- });
784
- expect(result).toEqual({});
785
- });
786
- it('should handle undefined values', ()=>{
787
- const data = {
788
- field1: undefined,
789
- field2: 'value'
790
- };
791
- const result = unflattenObject({
792
- data,
793
- fields: [],
794
- req: mockReq
795
- });
796
- expect(result).toEqual({
797
- field2: 'value'
798
- });
799
- });
800
- it('should preserve null values for validation', ()=>{
801
- const data = {
802
- field1: null,
803
- field2: 'value'
804
- };
805
- const result = unflattenObject({
806
- data,
807
- fields: [],
808
- req: mockReq
809
- });
810
- // null values are preserved for validation
811
- expect(result).toEqual({
812
- field1: null,
813
- field2: 'value'
814
- });
815
- });
816
- });
817
- });
818
-
819
- //# sourceMappingURL=unflattenObject.spec.js.map