@strapi/typescript-utils 0.0.0-next.e41415e8ff5f565ff959667d5c5ba4f20bee013c → 0.0.0-next.e6eaa3d0563c85f80fd88b258df70a55c057096e

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,22 +1,22 @@
1
1
  'use strict';
2
2
 
3
- jest.mock('../../../generators/schemas/imports', () => ({ addImport: jest.fn() }));
3
+ jest.mock('../../../generators/common/imports', () => ({ addImport: jest.fn() }));
4
4
 
5
5
  const consoleWarnMock = jest.spyOn(console, 'warn').mockImplementation();
6
6
 
7
7
  const ts = require('typescript');
8
8
 
9
- const attributeToPropertySignature = require('../../../generators/schemas/attributes');
9
+ const attributeToPropertySignature = require('../../../generators/common/models/attributes');
10
10
  const {
11
11
  getAttributeType,
12
12
  getAttributeModifiers,
13
- } = require('../../../generators/schemas/attributes');
14
- const { addImport } = require('../../../generators/schemas/imports');
13
+ } = require('../../../generators/common/models/attributes');
14
+ const { addImport } = require('../../../generators/common/imports');
15
15
 
16
16
  // TODO: emit definition (to a string) & also check snapshots based on that. It would allow checking both the structure & the output.
17
17
  describe('Attributes', () => {
18
18
  afterEach(() => {
19
- jest.resetAllMocks();
19
+ jest.clearAllMocks();
20
20
  });
21
21
 
22
22
  describe('Attribute to Property Signature', () => {
@@ -48,7 +48,7 @@ describe('Attributes', () => {
48
48
 
49
49
  expect(prop.type.types).toHaveLength(1);
50
50
  expect(prop.type.types[0].kind).toBe(ts.SyntaxKind.TypeReference);
51
- expect(prop.type.types[0].typeName.escapedText).toBe('StringAttribute');
51
+ expect(prop.type.types[0].typeName.escapedText).toBe('Attribute.String');
52
52
  expect(prop.type.types[0].typeArguments).toBeUndefined();
53
53
  });
54
54
 
@@ -60,7 +60,7 @@ describe('Attributes', () => {
60
60
 
61
61
  expect(prop.type.types).toHaveLength(1);
62
62
  expect(prop.type.types[0].kind).toBe(ts.SyntaxKind.TypeReference);
63
- expect(prop.type.types[0].typeName.escapedText).toBe('ComponentAttribute');
63
+ expect(prop.type.types[0].typeName.escapedText).toBe('Attribute.Component');
64
64
  expect(prop.type.types[0].typeArguments).toHaveLength(1);
65
65
  expect(prop.type.types[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
66
66
  expect(prop.type.types[0].typeArguments[0].text).toBe('default.comp');
@@ -82,14 +82,14 @@ describe('Attributes', () => {
82
82
  const [attributeType, requiredOptionType] = prop.type.types;
83
83
 
84
84
  expect(attributeType.kind).toBe(ts.SyntaxKind.TypeReference);
85
- expect(attributeType.typeName.escapedText).toBe('EnumerationAttribute');
85
+ expect(attributeType.typeName.escapedText).toBe('Attribute.Enumeration');
86
86
  expect(attributeType.typeArguments).toHaveLength(1);
87
87
  expect(attributeType.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
88
88
  expect(attributeType.typeArguments[0].elements[0].text).toBe('a');
89
89
  expect(attributeType.typeArguments[0].elements[1].text).toBe('b');
90
90
 
91
91
  expect(requiredOptionType.kind).toBe(ts.SyntaxKind.TypeReference);
92
- expect(requiredOptionType.typeName.escapedText).toBe('DefaultTo');
92
+ expect(requiredOptionType.typeName.escapedText).toBe('Attribute.DefaultTo');
93
93
  expect(requiredOptionType.typeArguments).toHaveLength(1);
94
94
  expect(requiredOptionType.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
95
95
  expect(requiredOptionType.typeArguments[0].text).toBe('b');
@@ -108,22 +108,22 @@ describe('Attributes', () => {
108
108
  });
109
109
 
110
110
  test.each([
111
- ['string', 'StringAttribute'],
112
- ['text', 'TextAttribute'],
113
- ['richtext', 'RichTextAttribute'],
114
- ['password', 'PasswordAttribute'],
115
- ['email', 'EmailAttribute'],
116
- ['date', 'DateAttribute'],
117
- ['time', 'TimeAttribute'],
118
- ['datetime', 'DateTimeAttribute'],
119
- ['timestamp', 'TimestampAttribute'],
120
- ['integer', 'IntegerAttribute'],
121
- ['biginteger', 'BigIntegerAttribute'],
122
- ['float', 'FloatAttribute'],
123
- ['decimal', 'DecimalAttribute'],
124
- ['boolean', 'BooleanAttribute'],
125
- ['json', 'JSONAttribute'],
126
- ['media', 'MediaAttribute'],
111
+ ['string', 'Attribute.String'],
112
+ ['text', 'Attribute.Text'],
113
+ ['richtext', 'Attribute.RichText'],
114
+ ['password', 'Attribute.Password'],
115
+ ['email', 'Attribute.Email'],
116
+ ['date', 'Attribute.Date'],
117
+ ['time', 'Attribute.Time'],
118
+ ['datetime', 'Attribute.DateTime'],
119
+ ['timestamp', 'Attribute.Timestamp'],
120
+ ['integer', 'Attribute.Integer'],
121
+ ['biginteger', 'Attribute.BigInteger'],
122
+ ['float', 'Attribute.Float'],
123
+ ['decimal', 'Attribute.Decimal'],
124
+ ['boolean', 'Attribute.Boolean'],
125
+ ['json', 'Attribute.JSON'],
126
+ ['media', 'Attribute.Media'],
127
127
  ])('Basic %p attribute should map to a %p type', (type, expectedType) => {
128
128
  const typeNode = getAttributeType('foo', { type });
129
129
 
@@ -134,7 +134,7 @@ describe('Attributes', () => {
134
134
  expect(typeNode.typeArguments).toBeUndefined();
135
135
 
136
136
  expect(consoleWarnMock).not.toHaveBeenCalled();
137
- expect(addImport).toHaveBeenCalledWith(expectedType);
137
+ expect(addImport).toHaveBeenCalledWith('Attribute');
138
138
  });
139
139
 
140
140
  describe('Complex types (with generic type parameters)', () => {
@@ -145,7 +145,7 @@ describe('Attributes', () => {
145
145
  expect(typeNode.typeName.escapedText).toBe(typeName);
146
146
 
147
147
  expect(consoleWarnMock).not.toHaveBeenCalled();
148
- expect(addImport).toHaveBeenCalledWith(typeName);
148
+ expect(addImport).toHaveBeenCalledWith('Attribute');
149
149
  };
150
150
 
151
151
  describe('Enumeration', () => {
@@ -153,7 +153,7 @@ describe('Attributes', () => {
153
153
  const attribute = { type: 'enumeration', enum: ['a', 'b', 'c'] };
154
154
  const typeNode = getAttributeType('foo', attribute);
155
155
 
156
- defaultAssertions(typeNode, 'EnumerationAttribute');
156
+ defaultAssertions(typeNode, 'Attribute.Enumeration');
157
157
 
158
158
  expect(typeNode.typeArguments).toHaveLength(1);
159
159
  expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
@@ -174,7 +174,7 @@ describe('Attributes', () => {
174
174
  const attribute = { type: 'uid' };
175
175
  const typeNode = getAttributeType('foo', attribute);
176
176
 
177
- defaultAssertions(typeNode, 'UIDAttribute');
177
+ defaultAssertions(typeNode, 'Attribute.UID');
178
178
 
179
179
  expect(typeNode.typeArguments).toBeUndefined();
180
180
  });
@@ -183,7 +183,7 @@ describe('Attributes', () => {
183
183
  const attribute = { type: 'uid', targetField: 'bar' };
184
184
  const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
185
185
 
186
- defaultAssertions(typeNode, 'UIDAttribute');
186
+ defaultAssertions(typeNode, 'Attribute.UID');
187
187
 
188
188
  expect(typeNode.typeArguments).not.toBeUndefined();
189
189
  expect(typeNode.typeArguments).toHaveLength(2);
@@ -199,7 +199,7 @@ describe('Attributes', () => {
199
199
  const attribute = { type: 'uid', options: { separator: '_' } };
200
200
  const typeNode = getAttributeType('foo', attribute);
201
201
 
202
- defaultAssertions(typeNode, 'UIDAttribute');
202
+ defaultAssertions(typeNode, 'Attribute.UID');
203
203
 
204
204
  expect(typeNode.typeArguments).toHaveLength(3);
205
205
 
@@ -224,7 +224,7 @@ describe('Attributes', () => {
224
224
  const attribute = { type: 'uid', options: { separator: '_' }, targetField: 'bar' };
225
225
  const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
226
226
 
227
- defaultAssertions(typeNode, 'UIDAttribute');
227
+ defaultAssertions(typeNode, 'Attribute.UID');
228
228
 
229
229
  expect(typeNode.typeArguments).toHaveLength(3);
230
230
 
@@ -254,7 +254,7 @@ describe('Attributes', () => {
254
254
  const attribute = { type: 'relation', relation: 'oneToOne', target: 'api::bar.bar' };
255
255
  const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
256
256
 
257
- defaultAssertions(typeNode, 'RelationAttribute');
257
+ defaultAssertions(typeNode, 'Attribute.Relation');
258
258
 
259
259
  expect(typeNode.typeArguments).toHaveLength(3);
260
260
 
@@ -272,7 +272,7 @@ describe('Attributes', () => {
272
272
  const attribute = { type: 'relation', relation: 'morphMany' };
273
273
  const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
274
274
 
275
- defaultAssertions(typeNode, 'RelationAttribute');
275
+ defaultAssertions(typeNode, 'Attribute.Relation');
276
276
 
277
277
  expect(typeNode.typeArguments).toHaveLength(2);
278
278
 
@@ -289,7 +289,7 @@ describe('Attributes', () => {
289
289
  const attribute = { type: 'component', component: 'default.comp', repeatable: true };
290
290
  const typeNode = getAttributeType('foo', attribute);
291
291
 
292
- defaultAssertions(typeNode, 'ComponentAttribute');
292
+ defaultAssertions(typeNode, 'Attribute.Component');
293
293
 
294
294
  expect(typeNode.typeArguments).toHaveLength(2);
295
295
 
@@ -303,7 +303,7 @@ describe('Attributes', () => {
303
303
  const attribute = { type: 'component', component: 'default.comp' };
304
304
  const typeNode = getAttributeType('foo', attribute);
305
305
 
306
- defaultAssertions(typeNode, 'ComponentAttribute');
306
+ defaultAssertions(typeNode, 'Attribute.Component');
307
307
 
308
308
  expect(typeNode.typeArguments).toHaveLength(1);
309
309
 
@@ -317,7 +317,7 @@ describe('Attributes', () => {
317
317
  const attribute = { type: 'dynamiczone', components: ['default.comp1', 'default.comp2'] };
318
318
  const typeNode = getAttributeType('foo', attribute);
319
319
 
320
- defaultAssertions(typeNode, 'DynamicZoneAttribute');
320
+ defaultAssertions(typeNode, 'Attribute.DynamicZone');
321
321
 
322
322
  expect(typeNode.typeArguments).toHaveLength(1);
323
323
 
@@ -359,7 +359,7 @@ describe('Attributes', () => {
359
359
 
360
360
  expect(modifiers).toHaveLength(1);
361
361
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
362
- expect(modifiers[0].typeName.escapedText).toBe('RequiredAttribute');
362
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.Required');
363
363
  });
364
364
  });
365
365
 
@@ -384,7 +384,7 @@ describe('Attributes', () => {
384
384
 
385
385
  expect(modifiers).toHaveLength(1);
386
386
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
387
- expect(modifiers[0].typeName.escapedText).toBe('PrivateAttribute');
387
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.Private');
388
388
  });
389
389
  });
390
390
 
@@ -409,7 +409,7 @@ describe('Attributes', () => {
409
409
 
410
410
  expect(modifiers).toHaveLength(1);
411
411
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
412
- expect(modifiers[0].typeName.escapedText).toBe('UniqueAttribute');
412
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.Unique');
413
413
  });
414
414
  });
415
415
 
@@ -434,7 +434,7 @@ describe('Attributes', () => {
434
434
 
435
435
  expect(modifiers).toHaveLength(1);
436
436
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
437
- expect(modifiers[0].typeName.escapedText).toBe('ConfigurableAttribute');
437
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.Configurable');
438
438
  });
439
439
  });
440
440
 
@@ -455,7 +455,7 @@ describe('Attributes', () => {
455
455
 
456
456
  expect(modifiers).toHaveLength(1);
457
457
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
458
- expect(modifiers[0].typeName.escapedText).toBe('CustomField');
458
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
459
459
  expect(modifiers[0].typeArguments).toHaveLength(1);
460
460
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
461
461
  expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
@@ -473,7 +473,7 @@ describe('Attributes', () => {
473
473
 
474
474
  expect(modifiers).toHaveLength(1);
475
475
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
476
- expect(modifiers[0].typeName.escapedText).toBe('CustomField');
476
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
477
477
  expect(modifiers[0].typeArguments).toHaveLength(2);
478
478
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
479
479
  expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
@@ -507,7 +507,7 @@ describe('Attributes', () => {
507
507
 
508
508
  expect(modifiers).toHaveLength(1);
509
509
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
510
- expect(modifiers[0].typeName.escapedText).toBe('SetPluginOptions');
510
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetPluginOptions');
511
511
  expect(modifiers[0].typeArguments).toHaveLength(1);
512
512
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
513
513
  expect(modifiers[0].typeArguments[0].members).toHaveLength(1);
@@ -546,21 +546,29 @@ describe('Attributes', () => {
546
546
  expect(modifiers).toHaveLength(1);
547
547
 
548
548
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
549
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
549
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
550
550
 
551
- expect(modifiers[0].typeArguments).toHaveLength(1);
552
- expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
553
- expect(modifiers[0].typeArguments[0].members).toHaveLength(1);
551
+ const [setMinMax] = modifiers;
552
+ const { typeArguments } = setMinMax;
553
+
554
+ expect(typeArguments).toBeDefined();
555
+ expect(typeArguments).toHaveLength(2);
556
+
557
+ const [definition, typeofMinMax] = typeArguments;
554
558
 
555
559
  // Min
556
- expect(modifiers[0].typeArguments[0].members[0].kind).toBe(
557
- ts.SyntaxKind.PropertyDeclaration
558
- );
559
- expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('min');
560
- expect(modifiers[0].typeArguments[0].members[0].type.kind).toBe(
561
- ts.SyntaxKind.NumericLiteral
562
- );
563
- expect(modifiers[0].typeArguments[0].members[0].type.text).toBe('2');
560
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
561
+ expect(definition.members).toHaveLength(1);
562
+
563
+ const [min] = definition.members;
564
+
565
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
566
+ expect(min.name.escapedText).toBe('min');
567
+ expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
568
+ expect(min.type.text).toBe('2');
569
+
570
+ // Check for number keyword on the second typeArgument
571
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
564
572
  });
565
573
 
566
574
  test('No Min, Max: 3', () => {
@@ -570,21 +578,29 @@ describe('Attributes', () => {
570
578
  expect(modifiers).toHaveLength(1);
571
579
 
572
580
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
573
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
581
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
574
582
 
575
- expect(modifiers[0].typeArguments).toHaveLength(1);
576
- expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
577
- expect(modifiers[0].typeArguments[0].members).toHaveLength(1);
583
+ const [setMinMax] = modifiers;
584
+ const { typeArguments } = setMinMax;
578
585
 
579
- // Min
580
- expect(modifiers[0].typeArguments[0].members[0].kind).toBe(
581
- ts.SyntaxKind.PropertyDeclaration
582
- );
583
- expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('max');
584
- expect(modifiers[0].typeArguments[0].members[0].type.kind).toBe(
585
- ts.SyntaxKind.NumericLiteral
586
- );
587
- expect(modifiers[0].typeArguments[0].members[0].type.text).toBe('3');
586
+ expect(typeArguments).toBeDefined();
587
+ expect(typeArguments).toHaveLength(2);
588
+
589
+ const [definition, typeofMinMax] = typeArguments;
590
+
591
+ // Max
592
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
593
+ expect(definition.members).toHaveLength(1);
594
+
595
+ const [max] = definition.members;
596
+
597
+ expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
598
+ expect(max.name.escapedText).toBe('max');
599
+ expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
600
+ expect(max.type.text).toBe('3');
601
+
602
+ // Check for number keyword on the second typeArgument
603
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
588
604
  });
589
605
 
590
606
  test('Min: 4, Max: 12', () => {
@@ -594,30 +610,98 @@ describe('Attributes', () => {
594
610
  expect(modifiers).toHaveLength(1);
595
611
 
596
612
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
597
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
613
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
598
614
 
599
- expect(modifiers[0].typeArguments).toHaveLength(1);
600
- expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
601
- expect(modifiers[0].typeArguments[0].members).toHaveLength(2);
615
+ const [setMinMax] = modifiers;
616
+ const { typeArguments } = setMinMax;
602
617
 
603
- // Min
604
- expect(modifiers[0].typeArguments[0].members[0].kind).toBe(
605
- ts.SyntaxKind.PropertyDeclaration
606
- );
607
- expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('min');
608
- expect(modifiers[0].typeArguments[0].members[0].type.kind).toBe(
609
- ts.SyntaxKind.NumericLiteral
610
- );
611
- expect(modifiers[0].typeArguments[0].members[0].type.text).toBe('4');
618
+ expect(typeArguments).toBeDefined();
619
+ expect(typeArguments).toHaveLength(2);
612
620
 
613
- expect(modifiers[0].typeArguments[0].members[1].kind).toBe(
614
- ts.SyntaxKind.PropertyDeclaration
615
- );
616
- expect(modifiers[0].typeArguments[0].members[1].name.escapedText).toBe('max');
617
- expect(modifiers[0].typeArguments[0].members[1].type.kind).toBe(
618
- ts.SyntaxKind.NumericLiteral
619
- );
620
- expect(modifiers[0].typeArguments[0].members[1].type.text).toBe('12');
621
+ const [definition, typeofMinMax] = typeArguments;
622
+
623
+ // Min/Max
624
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
625
+ expect(definition.members).toHaveLength(2);
626
+
627
+ const [min, max] = definition.members;
628
+
629
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
630
+ expect(min.name.escapedText).toBe('min');
631
+ expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
632
+ expect(min.type.text).toBe('4');
633
+
634
+ expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
635
+ expect(max.name.escapedText).toBe('max');
636
+ expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
637
+ expect(max.type.text).toBe('12');
638
+
639
+ // Check for number keyword on the second typeArgument
640
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
641
+ });
642
+
643
+ test('Min: "1"', () => {
644
+ const attribute = { min: '1' };
645
+ const modifiers = getAttributeModifiers(attribute);
646
+
647
+ expect(modifiers).toHaveLength(1);
648
+
649
+ expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
650
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
651
+
652
+ const [setMinMax] = modifiers;
653
+ const { typeArguments } = setMinMax;
654
+
655
+ expect(typeArguments).toBeDefined();
656
+ expect(typeArguments).toHaveLength(2);
657
+
658
+ const [definition, typeofMinMax] = typeArguments;
659
+
660
+ // Min/Max
661
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
662
+ expect(definition.members).toHaveLength(1);
663
+
664
+ const [min] = definition.members;
665
+
666
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
667
+ expect(min.name.escapedText).toBe('min');
668
+ expect(min.type.kind).toBe(ts.SyntaxKind.StringLiteral);
669
+ expect(min.type.text).toBe('1');
670
+
671
+ // Check for string keyword on the second typeArgument
672
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.StringKeyword);
673
+ });
674
+
675
+ test('Min: 0', () => {
676
+ const attribute = { min: 0 };
677
+ const modifiers = getAttributeModifiers(attribute);
678
+
679
+ expect(modifiers).toHaveLength(1);
680
+
681
+ expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
682
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
683
+
684
+ const [setMinMax] = modifiers;
685
+ const { typeArguments } = setMinMax;
686
+
687
+ expect(typeArguments).toBeDefined();
688
+ expect(typeArguments).toHaveLength(2);
689
+
690
+ const [definition, typeofMinMax] = typeArguments;
691
+
692
+ // Min/Max
693
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
694
+ expect(definition.members).toHaveLength(1);
695
+
696
+ const [min] = definition.members;
697
+
698
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
699
+ expect(min.name.escapedText).toBe('min');
700
+ expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
701
+ expect(min.type.text).toBe('0');
702
+
703
+ // Check for string keyword on the second typeArgument
704
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
621
705
  });
622
706
  });
623
707
 
@@ -636,7 +720,7 @@ describe('Attributes', () => {
636
720
  expect(modifiers).toHaveLength(1);
637
721
 
638
722
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
639
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
723
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
640
724
 
641
725
  expect(modifiers[0].typeArguments).toHaveLength(1);
642
726
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -660,7 +744,7 @@ describe('Attributes', () => {
660
744
  expect(modifiers).toHaveLength(1);
661
745
 
662
746
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
663
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
747
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
664
748
 
665
749
  expect(modifiers[0].typeArguments).toHaveLength(1);
666
750
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -684,7 +768,7 @@ describe('Attributes', () => {
684
768
  expect(modifiers).toHaveLength(1);
685
769
 
686
770
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
687
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
771
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
688
772
 
689
773
  expect(modifiers[0].typeArguments).toHaveLength(1);
690
774
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -726,7 +810,7 @@ describe('Attributes', () => {
726
810
  expect(modifiers).toHaveLength(1);
727
811
 
728
812
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
729
- expect(modifiers[0].typeName.escapedText).toBe('DefaultTo');
813
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
730
814
 
731
815
  expect(modifiers[0].typeArguments).toHaveLength(1);
732
816
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TrueKeyword);
@@ -739,7 +823,7 @@ describe('Attributes', () => {
739
823
  expect(modifiers).toHaveLength(1);
740
824
 
741
825
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
742
- expect(modifiers[0].typeName.escapedText).toBe('DefaultTo');
826
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
743
827
 
744
828
  expect(modifiers[0].typeArguments).toHaveLength(1);
745
829
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -6,7 +6,7 @@ const {
6
6
  addImport,
7
7
  generateImportDefinition,
8
8
  getImports,
9
- } = require('../../../generators/schemas/imports');
9
+ } = require('../../../generators/common/imports');
10
10
 
11
11
  describe('Imports', () => {
12
12
  test('When first loaded, the list of imports should be empty', () => {
@@ -27,28 +27,30 @@ describe('Imports', () => {
27
27
  });
28
28
 
29
29
  test('Generate an import type definition containing the registered import', () => {
30
- const def = generateImportDefinition();
30
+ const defs = generateImportDefinition();
31
31
 
32
- expect(def.kind).toBe(ts.SyntaxKind.ImportDeclaration);
32
+ defs.forEach((def) => {
33
+ expect(def.kind).toBe(ts.SyntaxKind.ImportDeclaration);
33
34
 
34
- // Module specifier
35
- expect(def.moduleSpecifier.kind).toBe(ts.SyntaxKind.StringLiteral);
36
- expect(def.moduleSpecifier.text).toBe('@strapi/strapi');
35
+ // Module specifier
36
+ expect(def.moduleSpecifier.kind).toBe(ts.SyntaxKind.StringLiteral);
37
+ expect(def.moduleSpecifier.text).toBe('@strapi/strapi');
37
38
 
38
- // Import clause (should be named imports)
39
- expect(def.importClause.kind).toBe(ts.SyntaxKind.ImportClause);
39
+ // Import clause (should be named imports)
40
+ expect(def.importClause.kind).toBe(ts.SyntaxKind.ImportClause);
40
41
 
41
- const { elements } = def.importClause.namedBindings;
42
+ const { elements } = def.importClause.namedBindings;
42
43
 
43
- expect(elements).toHaveLength(2);
44
+ expect(elements).toHaveLength(2);
44
45
 
45
- // Import clauses
46
- getImports().forEach((namedImport, index) => {
47
- const element = elements[index];
46
+ // Import clauses
47
+ getImports().forEach((namedImport, index) => {
48
+ const element = elements[index];
48
49
 
49
- expect(element.kind).toBe(ts.SyntaxKind.ImportSpecifier);
50
- expect(element.name.kind).toBe(ts.SyntaxKind.Identifier);
51
- expect(element.name.escapedText).toBe(namedImport);
50
+ expect(element.kind).toBe(ts.SyntaxKind.ImportSpecifier);
51
+ expect(element.name.kind).toBe(ts.SyntaxKind.Identifier);
52
+ expect(element.name.escapedText).toBe(namedImport);
53
+ });
52
54
  });
53
55
  });
54
56
  });
@@ -4,67 +4,15 @@ const ts = require('typescript');
4
4
  const { factory } = require('typescript');
5
5
 
6
6
  const {
7
- getAllStrapiSchemas,
8
7
  getDefinitionAttributesCount,
9
8
  getSchemaExtendsTypeName,
10
9
  getSchemaInterfaceName,
11
10
  getSchemaModelType,
12
11
  getTypeNode,
13
12
  toTypeLiteral,
14
- } = require('../../../generators/schemas/utils');
13
+ } = require('../../../generators/common/models/utils');
15
14
 
16
15
  describe('Utils', () => {
17
- describe('Get All Strapi Schemas', () => {
18
- test('Get both components and content types', () => {
19
- const strapi = {
20
- contentTypes: {
21
- ctA: {},
22
- ctB: {},
23
- },
24
- components: {
25
- comp1: {},
26
- comp2: {},
27
- comp3: {},
28
- },
29
- };
30
-
31
- const schemas = getAllStrapiSchemas(strapi);
32
-
33
- expect(schemas).toMatchObject({ ctA: {}, ctB: {}, comp1: {}, comp2: {}, comp3: {} });
34
- });
35
-
36
- test('Get only components if there is no content type', () => {
37
- const strapi = {
38
- contentTypes: {},
39
-
40
- components: {
41
- comp1: {},
42
- comp2: {},
43
- comp3: {},
44
- },
45
- };
46
-
47
- const schemas = getAllStrapiSchemas(strapi);
48
-
49
- expect(schemas).toMatchObject({ comp1: {}, comp2: {}, comp3: {} });
50
- });
51
-
52
- test('Get only content types if there is no component', () => {
53
- const strapi = {
54
- contentTypes: {
55
- ctA: {},
56
- ctB: {},
57
- },
58
-
59
- components: {},
60
- };
61
-
62
- const schemas = getAllStrapiSchemas(strapi);
63
-
64
- expect(schemas).toMatchObject({ ctA: {}, ctB: {} });
65
- });
66
- });
67
-
68
16
  describe('Get Definition Attributes Count', () => {
69
17
  const createMainNode = (members = []) => {
70
18
  return factory.createInterfaceDeclaration(
@@ -172,10 +120,10 @@ describe('Utils', () => {
172
120
 
173
121
  describe('Get Schema Extends Type Name', () => {
174
122
  test.each([
175
- [{ modelType: 'component', kind: null }, 'ComponentSchema'],
176
- [{ modelType: 'contentType', kind: 'singleType' }, 'SingleTypeSchema'],
177
- [{ modelType: 'contentType', kind: 'collectionType' }, 'CollectionTypeSchema'],
178
- [{ modelType: 'invalidType', kind: 'foo' }, 'Schema'],
123
+ [{ modelType: 'component', kind: null }, 'Schema.Component'],
124
+ [{ modelType: 'contentType', kind: 'singleType' }, 'Schema.SingleType'],
125
+ [{ modelType: 'contentType', kind: 'collectionType' }, 'Schema.CollectionType'],
126
+ [{ modelType: 'invalidType', kind: 'foo' }, null],
179
127
  ])("Expect %p to generate %p as the base type for a schema's interface", (schema, expected) => {
180
128
  expect(getSchemaExtendsTypeName(schema)).toBe(expected);
181
129
  });