-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathentries.test.ts
More file actions
1543 lines (1329 loc) · 61.9 KB
/
entries.test.ts
File metadata and controls
1543 lines (1329 loc) · 61.9 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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import fs from 'fs';
import { resolve } from 'path';
import { expect } from 'chai';
import cloneDeep from 'lodash/cloneDeep';
import { cliux } from '@contentstack/cli-utilities';
import fancy from 'fancy-test';
import Sinon from 'sinon';
import config from '../../../src/config';
import { $t, auditMsg } from '../../../src/messages';
import { ContentType, Entries, GlobalField } from '../../../src/modules';
import { CtConstructorParam, EntryStruct, ModuleConstructorParam } from '../../../src/types';
import {
schema,
emptyEntries,
ctBlock,
entryBlock,
ctJsonRTE,
entryJsonRTE,
ctGroupField,
entryGroupField,
} from '../mock/mock.json';
import { mockLogger } from '../mock-logger';
describe('Entries module', () => {
let constructorParam: ModuleConstructorParam & CtConstructorParam;
let ctStub: Sinon.SinonStub;
let gfStub: Sinon.SinonStub;
beforeEach(() => {
constructorParam = {
moduleName: 'entries',
ctSchema: cloneDeep(require('../mock/contents/content_types/schema.json')),
gfSchema: cloneDeep(require('../mock/contents/global_fields/globalfields.json')),
config: Object.assign(config, { basePath: resolve(__dirname, '..', 'mock', 'contents'), flags: {} }),
};
// Mock the logger for all tests
Sinon.stub(require('@contentstack/cli-utilities'), 'log').value(mockLogger);
});
before(() => {
ctStub = Sinon.stub(ContentType.prototype, 'run').resolves({ ct1: [{}] });
gfStub = Sinon.stub(GlobalField.prototype, 'run').resolves({ gf1: [{}] });
});
after(() => {
Sinon.restore(); // Clears Sinon spies/stubs/mocks
ctStub.restore();
gfStub.restore();
});
describe('run method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should throw folder path validation error', async () => {
const ctInstance = new Entries({
...constructorParam,
config: { ...constructorParam.config, basePath: resolve(__dirname, '..', 'mock', 'contents-1') },
});
try {
await ctInstance.run();
} catch (error: any) {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.eql($t(auditMsg.NOT_VALID_PATH, { path: ctInstance.folderPath }));
}
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(Entries.prototype, 'prepareEntryMetaData', async () => {})
.stub(Entries.prototype, 'fixPrerequisiteData', async () => {})
.stub(Entries.prototype, 'writeFixContent', async () => {})
.stub(Entries.prototype, 'lookForReference', async () => {})
.stub(Entries.prototype, 'locales', [{ code: 'en-us' }] as any)
.it('should return missing refs', async () => {
const ctInstance = new (class Class extends Entries {
constructor() {
super(constructorParam);
this.missingRefs['test-entry-id'] = [{ uid: 'test', treeStr: 'gf_0' }];
}
})();
const missingRefs = await ctInstance.run();
expect((missingRefs as any).missingEntryRefs).not.to.be.empty;
expect((missingRefs as any).missingEntryRefs).deep.contain({ 'test-entry-id': [{ uid: 'test', treeStr: 'gf_0' }] });
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(Entries.prototype, 'prepareEntryMetaData', async () => {})
.stub(Entries.prototype, 'fixPrerequisiteData', async () => {})
.stub(Entries.prototype, 'lookForReference', async () => {})
.stub(Entries.prototype, 'writeFixContent', async () => {})
.stub(Entries.prototype, 'locales', [{ code: 'en-us' }] as any)
.it('should call prepareEntryMetaData & fixPrerequisiteData methods', async () => {
const prepareEntryMetaData = Sinon.spy(Entries.prototype, 'prepareEntryMetaData');
const fixPrerequisiteData = Sinon.spy(Entries.prototype, 'fixPrerequisiteData');
const lookForReference = Sinon.spy(Entries.prototype, 'lookForReference');
const writeFixContent = Sinon.spy(Entries.prototype, 'writeFixContent');
const ctInstance = new Entries({ ...constructorParam, fix: true });
const missingRefs = await ctInstance.run();
expect((missingRefs as any).missingEntryRefs).to.be.empty;
expect(writeFixContent.callCount).to.be.equals(1);
expect(lookForReference.callCount).to.be.equals(1);
expect(fixPrerequisiteData.callCount).to.be.equals(1);
expect(prepareEntryMetaData.callCount).to.be.equals(1);
});
});
describe('fixPrerequisiteData method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should call content type and global fields fix functionality', async () => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
await ctInstance.fixPrerequisiteData();
expect(ctStub.callCount).to.be.equals(1);
expect(gfStub.callCount).to.be.equals(1);
expect(ctInstance.ctSchema).deep.contain({ ct1: [{}] });
expect(ctInstance.gfSchema).deep.contain({ gf1: [{}] });
});
});
describe('writeFixContent method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(fs, 'writeFileSync', () => {})
.stub(cliux, 'confirm', async () => true)
.it('should ask confirmation adn write content in given path', async ({}) => {
const writeFileSync = Sinon.spy(fs, 'writeFileSync');
const ctInstance = new Entries({ ...constructorParam, fix: true });
await ctInstance.writeFixContent(resolve(__dirname, '..', 'mock', 'contents'), {});
expect(writeFileSync.callCount).to.be.equals(1);
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(fs, 'writeFileSync', () => {})
.it("should skip confirmation if 'yes' flag passed", async ({}) => {
const writeFileSync = Sinon.spy(fs, 'writeFileSync');
const ctInstance = new Entries({ ...constructorParam, fix: true });
ctInstance.config.flags.yes = true;
await ctInstance.writeFixContent(resolve(__dirname, '..', 'mock', 'contents'), {});
expect(writeFileSync.callCount).to.be.equals(1);
expect(writeFileSync.calledWithExactly(resolve(__dirname, '..', 'mock', 'contents'), JSON.stringify({}))).to.be
.true;
});
});
describe('lookForReference method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(Entries.prototype, 'runFixOnSchema', () => emptyEntries)
.stub(Entries.prototype, 'validateReferenceField', () => [])
.stub(Entries.prototype, 'validateGlobalField', () => {})
.stub(Entries.prototype, 'validateJsonRTEFields', () => {})
.stub(Entries.prototype, 'validateModularBlocksField', () => {})
.stub(Entries.prototype, 'validateGroupField', () => {})
.it('should call datatype specific methods', async ({}) => {
const ctInstance = new (class Class extends Entries {
constructor() {
super({ ...constructorParam, fix: true });
this.currentUid = 'reference';
this.missingRefs = { reference: [] };
this.missingMandatoryFields['reference'] = [];
}
})();
const runFixOnSchema = Sinon.spy(ctInstance, 'runFixOnSchema');
const validateReferenceField = Sinon.spy(ctInstance, 'validateReferenceField');
const validateGlobalField = Sinon.spy(ctInstance, 'validateGlobalField');
const validateJsonRTEFields = Sinon.spy(ctInstance, 'validateJsonRTEFields');
const validateModularBlocksField = Sinon.spy(ctInstance, 'validateModularBlocksField');
const validateGroupField = Sinon.spy(ctInstance, 'validateGroupField');
await ctInstance.lookForReference([], { schema } as any, {});
expect(runFixOnSchema.callCount).to.be.equals(1);
expect(validateReferenceField.callCount).to.be.equals(1);
expect(validateGlobalField.callCount).to.be.equals(1);
expect(validateJsonRTEFields.callCount).to.be.equals(1);
expect(validateModularBlocksField.callCount).to.be.equals(1);
expect(validateGroupField.callCount).to.be.equals(1);
});
});
describe('validateReferenceField method', () => {
class Class extends Entries {
public entries: Record<string, EntryStruct> = (
require('../mock/contents/entries/page_1/en-us/e7f6e3cc-64ca-4226-afb3-7794242ae5f5-entries.json') as any
)['test-uid-2'];
constructor() {
super(constructorParam);
}
}
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(Entries.prototype, 'validateReferenceValues', () => {})
.it('should call validateReferenceField method', async ({}) => {
const validateReferenceValues = Sinon.spy(Entries.prototype, 'validateReferenceValues');
const ctInstance = new Class();
await ctInstance.validateReferenceField([], ctInstance.ctSchema[3].schema as any, ctInstance.entries as any);
expect(validateReferenceValues.callCount).to.be.equals(1);
expect(
validateReferenceValues.alwaysCalledWith(
[],
ctInstance.ctSchema[3].schema as unknown as any,
ctInstance.entries as any,
),
).to.be.true;
});
fancy.stdout({ print: process.env.PRINT === 'true' || false }).it('should return missing reference', async () => {
const ctInstance = new Class();
const missingRefs = await ctInstance.validateReferenceField(
[{ uid: 'test-uid', name: 'reference', field: 'reference' }],
ctInstance.ctSchema[3].schema as any,
ctInstance.entries['reference'] as any,
);
expect(missingRefs).deep.equal([
{
tree: [
{
uid: 'test-uid',
name: 'reference',
field: 'reference',
},
],
data_type: undefined,
missingRefs: [
{
uid: 'test-uid-1',
_content_type_uid: 'page_0',
},
],
display_name: undefined,
uid: undefined,
name: undefined,
treeStr: 'reference',
},
]);
});
});
// describe('validateGlobalField method', () => {
// let lookForReferenceSpy;
// let ctInstance;
// beforeEach(() => {
// // Restore original methods before each test
// Sinon.restore();
// // Spy on the lookForReference method
// lookForReferenceSpy = Sinon.spy(Entries.prototype, 'lookForReference');
// // Create a new instance of Entries for each test
// ctInstance = new (class extends Entries {
// public entries: Record<string, EntryStruct> = (
// require('../mock/contents/entries/page_1/en-us/e7f6e3cc-64ca-4226-afb3-7794242ae5f5-entries.json') as any
// )['test-uid-2'];
// })(constructorParam);
// });
// it('should call lookForReference method', async () => {
// // Call the method under test
// await ctInstance.validateGlobalField([], ctInstance.ctSchema as any, ctInstance.entries);
// // Assertions
// expect(lookForReferenceSpy.callCount).to.be.equals(1);
// expect(lookForReferenceSpy.calledWithExactly([], ctInstance.ctSchema, ctInstance.entries)).to.be.true;
// });
// });
describe('validateJsonRTEFields method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(Entries.prototype, 'jsonRefCheck', () => {})
.it('should do recursive call on validateJsonRTEFields method', async ({}) => {
const jsonRefCheck = Sinon.spy(Entries.prototype, 'jsonRefCheck');
const validateJsonRTEFields = Sinon.spy(Entries.prototype, 'validateJsonRTEFields');
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
await ctInstance.validateJsonRTEFields([], ctJsonRTE as any, entryJsonRTE as any);
expect(jsonRefCheck.callCount).to.be.equals(4);
expect(validateJsonRTEFields.callCount).to.be.equals(3);
expect(validateJsonRTEFields.calledWithExactly([], ctJsonRTE as any, entryJsonRTE as any)).to.be.true;
expect(jsonRefCheck.calledWithExactly([], ctJsonRTE as any, entryJsonRTE.children[0] as any)).to.be.true;
});
});
describe('validateModularBlocksField method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(Entries.prototype, 'modularBlockRefCheck', () => {})
.stub(Entries.prototype, 'lookForReference', () => {})
.it(
'should iterate each blocks and call modularBlockRefCheck & lookForReference methods number of blocks exist in the entry times',
async ({}) => {
const modularBlockRefCheck = Sinon.spy(Entries.prototype, 'modularBlockRefCheck');
const lookForReference = Sinon.spy(Entries.prototype, 'lookForReference');
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
await ctInstance.validateModularBlocksField([], ctBlock as any, entryBlock as any);
expect(modularBlockRefCheck.callCount).to.be.equals(3);
expect(lookForReference.callCount).to.be.equals(5);
expect(modularBlockRefCheck.calledWithExactly([], ctBlock.blocks as any, entryBlock[0] as any, 0)).to.be.true;
expect(
lookForReference.calledWithExactly(
[{ uid: 'gf_1', name: 'GF 1' }],
ctBlock.blocks[1] as any,
entryBlock[0].gf_1 as any,
),
).to.be.true;
},
);
});
describe('validateGroupField method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(Entries.prototype, 'lookForReference', () => {})
.it('should call lookForReference method to iterate GroupField schema', async ({}) => {
const lookForReference = Sinon.spy(Entries.prototype, 'lookForReference');
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
await ctInstance.validateGroupField([], ctGroupField as any, entryGroupField as any);
expect(lookForReference.callCount).to.be.equals(1);
expect(lookForReference.calledWithExactly([], ctGroupField as any, entryGroupField)).to.be.true;
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(Entries.prototype, 'lookForReference', () => {})
.it(
'should iterate all group entries and call lookForReference method to iterate GroupField schema',
async ({}) => {
const lookForReference = Sinon.spy(Entries.prototype, 'lookForReference');
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
await ctInstance.validateGroupField([], ctGroupField as any, [entryGroupField, entryGroupField] as any);
expect(lookForReference.callCount).to.be.equals(2);
expect(
lookForReference.calledWithExactly(
[{ uid: ctGroupField.uid, display_name: ctGroupField.display_name }],
ctGroupField as any,
entryGroupField,
),
).to.be.true;
},
);
});
describe('fixGlobalFieldReferences method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(Entries.prototype, 'runFixOnSchema', (...args: any[]) => args[2])
.it('should call runFixOnSchema for single global field entry', async ({}) => {
const runFixOnSchema = Sinon.spy(Entries.prototype, 'runFixOnSchema');
const ctInstance = new Entries({ ...constructorParam, fix: true });
const globalFieldSchema = {
uid: 'gf_1',
display_name: 'Global Field 1',
data_type: 'global_field',
multiple: false,
schema: [
{ uid: 'reference', display_name: 'Reference', data_type: 'reference' }
]
};
const entryData = {
reference: [{ uid: 'test-uid-1', _content_type_uid: 'page_0' }]
};
const result = await ctInstance.fixGlobalFieldReferences([], globalFieldSchema as any, entryData as any);
expect(runFixOnSchema.callCount).to.be.equals(1);
expect(runFixOnSchema.firstCall.args[0]).to.deep.equal([{ uid: globalFieldSchema.uid, display_name: globalFieldSchema.display_name }]);
expect(runFixOnSchema.firstCall.args[1]).to.deep.equal(globalFieldSchema.schema);
expect(runFixOnSchema.firstCall.args[2]).to.deep.equal(entryData);
expect(result).to.deep.equal(entryData);
});
});
describe('validateSelectField method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should validate single select field with valid value', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: false,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData = 'option1';
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.validateSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.be.an('array');
expect(result.length).to.equal(0); // No validation errors
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should flag single select field with invalid value', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: false,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData = 'invalid_option';
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.validateSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.be.an('array');
expect(result.length).to.equal(1);
expect(result[0]).to.have.property('missingCTSelectFieldValues', 'invalid_option');
expect(result[0]).to.have.property('display_name', 'Select Field');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should handle empty single select field value', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: false,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData = '';
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.validateSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.be.an('array');
expect(result.length).to.equal(1);
expect(result[0]).to.have.property('missingCTSelectFieldValues', 'Not Selected');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should handle null single select field value', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: false,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData = null;
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.validateSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.be.an('array');
expect(result.length).to.equal(1);
expect(result[0]).to.have.property('missingCTSelectFieldValues', 'Not Selected');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should validate multiple select field with valid values', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: true,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' },
{ value: 'option3', display_name: 'Option 3' }
]
}
};
const entryData = ['option1', 'option2'];
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.validateSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.be.an('array');
expect(result.length).to.equal(0); // No validation errors
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should flag multiple select field with invalid values', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: true,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData = ['option1', 'invalid_option', 'option2'];
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.validateSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.be.an('array');
expect(result.length).to.equal(1);
expect(result[0]).to.have.property('missingCTSelectFieldValues');
expect(result[0].missingCTSelectFieldValues).to.include('invalid_option');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should handle empty multiple select field array', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: true,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData: string[] = [];
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.validateSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.be.an('array');
expect(result.length).to.equal(1);
expect(result[0]).to.have.property('missingCTSelectFieldValues', 'Not Selected');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should handle number data type with zero value', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'number',
display_type: 'dropdown',
multiple: false,
enum: {
choices: [
{ value: 0, display_name: 'Zero' },
{ value: 1, display_name: 'One' }
]
}
};
const entryData = 0;
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.validateSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.be.an('array');
expect(result.length).to.equal(0); // Zero should be valid for number type
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should return empty array when display_type is missing', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
// No display_type
multiple: false,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' }
]
}
};
const entryData = 'invalid_option';
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.validateSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.be.an('array');
expect(result.length).to.equal(0); // No display_type means no validation
});
});
describe('fixSelectField method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should return original value when fix is disabled', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
(ctInstance as any).config = { ...constructorParam.config, fixSelectField: false };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: false,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData = 'invalid_option';
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.fixSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.equal('invalid_option'); // Should return original value unchanged
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should fix single select field with invalid value', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
(ctInstance as any).config = { ...constructorParam.config, fixSelectField: true };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: false,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData = 'invalid_option';
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.fixSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.equal('option1'); // Should be replaced with first valid option
expect((ctInstance as any).missingSelectFeild['test-entry']).to.have.length(1);
expect((ctInstance as any).missingSelectFeild['test-entry'][0]).to.have.property('fixStatus', 'Fixed');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should not change single select field with valid value', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
(ctInstance as any).config = { ...constructorParam.config, fixSelectField: true };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: false,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData = 'option2';
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.fixSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.equal('option2'); // Should remain unchanged
expect((ctInstance as any).missingSelectFeild['test-entry']).to.have.length(0);
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should fix multiple select field with invalid values', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
(ctInstance as any).config = { ...constructorParam.config, fixSelectField: true };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: true,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' },
{ value: 'option3', display_name: 'Option 3' }
]
}
};
const entryData = ['option1', 'invalid_option', 'option2'];
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.fixSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.deep.equal(['option1', 'option2']); // Invalid option should be removed
expect((ctInstance as any).missingSelectFeild['test-entry']).to.have.length(1);
expect((ctInstance as any).missingSelectFeild['test-entry'][0]).to.have.property('fixStatus', 'Fixed');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should add default value to empty multiple select field', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
(ctInstance as any).config = { ...constructorParam.config, fixSelectField: true };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: true,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' }
]
}
};
const entryData: string[] = [];
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.fixSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.deep.equal(['option1']); // Should add first option
expect((ctInstance as any).missingSelectFeild['test-entry']).to.have.length(1);
expect((ctInstance as any).missingSelectFeild['test-entry'][0]).to.have.property('fixStatus', 'Fixed');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should handle min_instance requirement for multiple select field', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
(ctInstance as any).config = { ...constructorParam.config, fixSelectField: true };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: true,
min_instance: 3,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' },
{ value: 'option2', display_name: 'Option 2' },
{ value: 'option3', display_name: 'Option 3' },
{ value: 'option4', display_name: 'Option 4' }
]
}
};
const entryData = ['option1'];
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.fixSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.have.length(3); // Should have min_instance number of values
expect(result).to.include('option1'); // Original value should remain
expect((ctInstance as any).missingSelectFeild['test-entry']).to.have.length(1);
expect((ctInstance as any).missingSelectFeild['test-entry'][0]).to.have.property('fixStatus', 'Fixed');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should handle empty choices array gracefully', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
(ctInstance as any).config = { ...constructorParam.config, fixSelectField: true };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
display_type: 'dropdown',
multiple: false,
enum: {
choices: [] // Empty choices
}
};
const entryData = 'invalid_option';
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.fixSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.equal(null); // Should be set to null when no choices available
expect((ctInstance as any).missingSelectFeild['test-entry']).to.have.length(1);
expect((ctInstance as any).missingSelectFeild['test-entry'][0]).to.have.property('fixStatus', 'Fixed');
});
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should not record fix when display_type is missing', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).currentTitle = 'Test Entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };
(ctInstance as any).missingSelectFeild = { 'test-entry': [] };
(ctInstance as any).missingMandatoryFields = { 'test-entry': [] };
(ctInstance as any).config = { ...constructorParam.config, fixSelectField: true };
const selectFieldSchema = {
uid: 'select_field',
display_name: 'Select Field',
data_type: 'select',
// No display_type
multiple: false,
enum: {
choices: [
{ value: 'option1', display_name: 'Option 1' }
]
}
};
const entryData = 'invalid_option';
const tree = [{ uid: 'test-entry', name: 'Test Entry' }];
const result = ctInstance.fixSelectField(tree, selectFieldSchema as any, entryData);
expect(result).to.equal('option1'); // Should still fix the value
expect((ctInstance as any).missingSelectFeild['test-entry']).to.have.length(0); // But not record it
});
});
describe('validateReferenceField method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.it('should validate reference field with valid UID', async ({}) => {
const ctInstance = new Entries(constructorParam);
(ctInstance as any).currentUid = 'test-entry';
(ctInstance as any).missingRefs = { 'test-entry': [] };