@strapi/typescript-utils 0.0.0-next.f45143c5e2a8a9d85691d0abf79a3f42024a0c71 → 0.0.0-next.f4ff842a3cb7b83db540bee67554b704e042b042

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.
package/.eslintignore CHANGED
@@ -1,2 +1,3 @@
1
1
  node_modules/
2
2
  .eslintrc.js
3
+ index.d.ts
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ // FIXME: to remove when migrating this package to ts
2
+ declare module '@strapi/typescript-utils' {
3
+ const utils: any;
4
+ export = utils;
5
+ }
package/jest.config.js CHANGED
@@ -2,4 +2,5 @@
2
2
 
3
3
  module.exports = {
4
4
  preset: '../../../jest-preset.unit.js',
5
+ displayName: 'Typescript utils',
5
6
  };
@@ -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,15 +145,81 @@ 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
+ describe('Media', () => {
152
+ test('Media with multiple and with no allowedTypes', () => {
153
+ const attribute = { type: 'media', multiple: true };
154
+ const typeNode = getAttributeType('foo', attribute);
155
+
156
+ defaultAssertions(typeNode, 'Attribute.Media');
157
+
158
+ expect(typeNode.typeArguments).toHaveLength(2);
159
+
160
+ expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UndefinedKeyword);
161
+
162
+ expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
163
+ });
164
+
165
+ test('Media without multiple with allowedTypes', () => {
166
+ const attribute = { type: 'media', allowedTypes: ['images', 'videos'] };
167
+ const typeNode = getAttributeType('foo', attribute);
168
+
169
+ defaultAssertions(typeNode, 'Attribute.Media');
170
+
171
+ expect(typeNode.typeArguments).toHaveLength(1);
172
+
173
+ expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType);
174
+
175
+ const unionTypes = typeNode.typeArguments[0].types;
176
+
177
+ attribute.allowedTypes.forEach((value, index) => {
178
+ const element = unionTypes[index];
179
+
180
+ expect(element.kind).toBe(ts.SyntaxKind.StringLiteral);
181
+ expect(element.text).toBe(value);
182
+ });
183
+ });
184
+
185
+ test('Media with multiple and with allowedTypes', () => {
186
+ const attribute = { type: 'media', multiple: true, allowedTypes: ['images', 'videos'] };
187
+ const typeNode = getAttributeType('foo', attribute);
188
+
189
+ defaultAssertions(typeNode, 'Attribute.Media');
190
+
191
+ expect(typeNode.typeArguments).toHaveLength(2);
192
+
193
+ expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType);
194
+
195
+ const unionTypes = typeNode.typeArguments[0].types;
196
+
197
+ attribute.allowedTypes.forEach((value, index) => {
198
+ const element = unionTypes[index];
199
+
200
+ expect(element.kind).toBe(ts.SyntaxKind.StringLiteral);
201
+ expect(element.text).toBe(value);
202
+ });
203
+
204
+ expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
205
+ });
206
+
207
+ test('Media without multiple and with no allowedTypes', () => {
208
+ const attribute = { type: 'media' };
209
+ const typeNode = getAttributeType('foo', attribute);
210
+
211
+ defaultAssertions(typeNode, 'Attribute.Media');
212
+
213
+ expect(typeNode.typeArguments).toBeUndefined();
214
+ });
215
+ });
216
+
151
217
  describe('Enumeration', () => {
152
218
  test('Enumeration with an enum property', () => {
153
219
  const attribute = { type: 'enumeration', enum: ['a', 'b', 'c'] };
154
220
  const typeNode = getAttributeType('foo', attribute);
155
221
 
156
- defaultAssertions(typeNode, 'EnumerationAttribute');
222
+ defaultAssertions(typeNode, 'Attribute.Enumeration');
157
223
 
158
224
  expect(typeNode.typeArguments).toHaveLength(1);
159
225
  expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
@@ -174,7 +240,7 @@ describe('Attributes', () => {
174
240
  const attribute = { type: 'uid' };
175
241
  const typeNode = getAttributeType('foo', attribute);
176
242
 
177
- defaultAssertions(typeNode, 'UIDAttribute');
243
+ defaultAssertions(typeNode, 'Attribute.UID');
178
244
 
179
245
  expect(typeNode.typeArguments).toBeUndefined();
180
246
  });
@@ -183,7 +249,7 @@ describe('Attributes', () => {
183
249
  const attribute = { type: 'uid', targetField: 'bar' };
184
250
  const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
185
251
 
186
- defaultAssertions(typeNode, 'UIDAttribute');
252
+ defaultAssertions(typeNode, 'Attribute.UID');
187
253
 
188
254
  expect(typeNode.typeArguments).not.toBeUndefined();
189
255
  expect(typeNode.typeArguments).toHaveLength(2);
@@ -199,7 +265,7 @@ describe('Attributes', () => {
199
265
  const attribute = { type: 'uid', options: { separator: '_' } };
200
266
  const typeNode = getAttributeType('foo', attribute);
201
267
 
202
- defaultAssertions(typeNode, 'UIDAttribute');
268
+ defaultAssertions(typeNode, 'Attribute.UID');
203
269
 
204
270
  expect(typeNode.typeArguments).toHaveLength(3);
205
271
 
@@ -224,7 +290,7 @@ describe('Attributes', () => {
224
290
  const attribute = { type: 'uid', options: { separator: '_' }, targetField: 'bar' };
225
291
  const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
226
292
 
227
- defaultAssertions(typeNode, 'UIDAttribute');
293
+ defaultAssertions(typeNode, 'Attribute.UID');
228
294
 
229
295
  expect(typeNode.typeArguments).toHaveLength(3);
230
296
 
@@ -254,7 +320,7 @@ describe('Attributes', () => {
254
320
  const attribute = { type: 'relation', relation: 'oneToOne', target: 'api::bar.bar' };
255
321
  const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
256
322
 
257
- defaultAssertions(typeNode, 'RelationAttribute');
323
+ defaultAssertions(typeNode, 'Attribute.Relation');
258
324
 
259
325
  expect(typeNode.typeArguments).toHaveLength(3);
260
326
 
@@ -272,7 +338,7 @@ describe('Attributes', () => {
272
338
  const attribute = { type: 'relation', relation: 'morphMany' };
273
339
  const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
274
340
 
275
- defaultAssertions(typeNode, 'RelationAttribute');
341
+ defaultAssertions(typeNode, 'Attribute.Relation');
276
342
 
277
343
  expect(typeNode.typeArguments).toHaveLength(2);
278
344
 
@@ -289,7 +355,7 @@ describe('Attributes', () => {
289
355
  const attribute = { type: 'component', component: 'default.comp', repeatable: true };
290
356
  const typeNode = getAttributeType('foo', attribute);
291
357
 
292
- defaultAssertions(typeNode, 'ComponentAttribute');
358
+ defaultAssertions(typeNode, 'Attribute.Component');
293
359
 
294
360
  expect(typeNode.typeArguments).toHaveLength(2);
295
361
 
@@ -303,7 +369,7 @@ describe('Attributes', () => {
303
369
  const attribute = { type: 'component', component: 'default.comp' };
304
370
  const typeNode = getAttributeType('foo', attribute);
305
371
 
306
- defaultAssertions(typeNode, 'ComponentAttribute');
372
+ defaultAssertions(typeNode, 'Attribute.Component');
307
373
 
308
374
  expect(typeNode.typeArguments).toHaveLength(1);
309
375
 
@@ -317,7 +383,7 @@ describe('Attributes', () => {
317
383
  const attribute = { type: 'dynamiczone', components: ['default.comp1', 'default.comp2'] };
318
384
  const typeNode = getAttributeType('foo', attribute);
319
385
 
320
- defaultAssertions(typeNode, 'DynamicZoneAttribute');
386
+ defaultAssertions(typeNode, 'Attribute.DynamicZone');
321
387
 
322
388
  expect(typeNode.typeArguments).toHaveLength(1);
323
389
 
@@ -359,7 +425,7 @@ describe('Attributes', () => {
359
425
 
360
426
  expect(modifiers).toHaveLength(1);
361
427
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
362
- expect(modifiers[0].typeName.escapedText).toBe('RequiredAttribute');
428
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.Required');
363
429
  });
364
430
  });
365
431
 
@@ -384,7 +450,7 @@ describe('Attributes', () => {
384
450
 
385
451
  expect(modifiers).toHaveLength(1);
386
452
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
387
- expect(modifiers[0].typeName.escapedText).toBe('PrivateAttribute');
453
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.Private');
388
454
  });
389
455
  });
390
456
 
@@ -409,7 +475,7 @@ describe('Attributes', () => {
409
475
 
410
476
  expect(modifiers).toHaveLength(1);
411
477
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
412
- expect(modifiers[0].typeName.escapedText).toBe('UniqueAttribute');
478
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.Unique');
413
479
  });
414
480
  });
415
481
 
@@ -434,7 +500,7 @@ describe('Attributes', () => {
434
500
 
435
501
  expect(modifiers).toHaveLength(1);
436
502
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
437
- expect(modifiers[0].typeName.escapedText).toBe('ConfigurableAttribute');
503
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.Configurable');
438
504
  });
439
505
  });
440
506
 
@@ -455,7 +521,7 @@ describe('Attributes', () => {
455
521
 
456
522
  expect(modifiers).toHaveLength(1);
457
523
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
458
- expect(modifiers[0].typeName.escapedText).toBe('CustomField');
524
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
459
525
  expect(modifiers[0].typeArguments).toHaveLength(1);
460
526
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
461
527
  expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
@@ -473,7 +539,7 @@ describe('Attributes', () => {
473
539
 
474
540
  expect(modifiers).toHaveLength(1);
475
541
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
476
- expect(modifiers[0].typeName.escapedText).toBe('CustomField');
542
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
477
543
  expect(modifiers[0].typeArguments).toHaveLength(2);
478
544
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
479
545
  expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
@@ -507,7 +573,7 @@ describe('Attributes', () => {
507
573
 
508
574
  expect(modifiers).toHaveLength(1);
509
575
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
510
- expect(modifiers[0].typeName.escapedText).toBe('SetPluginOptions');
576
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetPluginOptions');
511
577
  expect(modifiers[0].typeArguments).toHaveLength(1);
512
578
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
513
579
  expect(modifiers[0].typeArguments[0].members).toHaveLength(1);
@@ -546,21 +612,29 @@ describe('Attributes', () => {
546
612
  expect(modifiers).toHaveLength(1);
547
613
 
548
614
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
549
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
615
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
550
616
 
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);
617
+ const [setMinMax] = modifiers;
618
+ const { typeArguments } = setMinMax;
619
+
620
+ expect(typeArguments).toBeDefined();
621
+ expect(typeArguments).toHaveLength(2);
622
+
623
+ const [definition, typeofMinMax] = typeArguments;
554
624
 
555
625
  // 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');
626
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
627
+ expect(definition.members).toHaveLength(1);
628
+
629
+ const [min] = definition.members;
630
+
631
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
632
+ expect(min.name.escapedText).toBe('min');
633
+ expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
634
+ expect(min.type.text).toBe('2');
635
+
636
+ // Check for number keyword on the second typeArgument
637
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
564
638
  });
565
639
 
566
640
  test('No Min, Max: 3', () => {
@@ -570,21 +644,29 @@ describe('Attributes', () => {
570
644
  expect(modifiers).toHaveLength(1);
571
645
 
572
646
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
573
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
647
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
574
648
 
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);
649
+ const [setMinMax] = modifiers;
650
+ const { typeArguments } = setMinMax;
578
651
 
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');
652
+ expect(typeArguments).toBeDefined();
653
+ expect(typeArguments).toHaveLength(2);
654
+
655
+ const [definition, typeofMinMax] = typeArguments;
656
+
657
+ // Max
658
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
659
+ expect(definition.members).toHaveLength(1);
660
+
661
+ const [max] = definition.members;
662
+
663
+ expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
664
+ expect(max.name.escapedText).toBe('max');
665
+ expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
666
+ expect(max.type.text).toBe('3');
667
+
668
+ // Check for number keyword on the second typeArgument
669
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
588
670
  });
589
671
 
590
672
  test('Min: 4, Max: 12', () => {
@@ -594,30 +676,98 @@ describe('Attributes', () => {
594
676
  expect(modifiers).toHaveLength(1);
595
677
 
596
678
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
597
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
679
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
598
680
 
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);
681
+ const [setMinMax] = modifiers;
682
+ const { typeArguments } = setMinMax;
602
683
 
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');
684
+ expect(typeArguments).toBeDefined();
685
+ expect(typeArguments).toHaveLength(2);
612
686
 
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');
687
+ const [definition, typeofMinMax] = typeArguments;
688
+
689
+ // Min/Max
690
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
691
+ expect(definition.members).toHaveLength(2);
692
+
693
+ const [min, max] = definition.members;
694
+
695
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
696
+ expect(min.name.escapedText).toBe('min');
697
+ expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
698
+ expect(min.type.text).toBe('4');
699
+
700
+ expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
701
+ expect(max.name.escapedText).toBe('max');
702
+ expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
703
+ expect(max.type.text).toBe('12');
704
+
705
+ // Check for number keyword on the second typeArgument
706
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
707
+ });
708
+
709
+ test('Min: "1"', () => {
710
+ const attribute = { min: '1' };
711
+ const modifiers = getAttributeModifiers(attribute);
712
+
713
+ expect(modifiers).toHaveLength(1);
714
+
715
+ expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
716
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
717
+
718
+ const [setMinMax] = modifiers;
719
+ const { typeArguments } = setMinMax;
720
+
721
+ expect(typeArguments).toBeDefined();
722
+ expect(typeArguments).toHaveLength(2);
723
+
724
+ const [definition, typeofMinMax] = typeArguments;
725
+
726
+ // Min/Max
727
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
728
+ expect(definition.members).toHaveLength(1);
729
+
730
+ const [min] = definition.members;
731
+
732
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
733
+ expect(min.name.escapedText).toBe('min');
734
+ expect(min.type.kind).toBe(ts.SyntaxKind.StringLiteral);
735
+ expect(min.type.text).toBe('1');
736
+
737
+ // Check for string keyword on the second typeArgument
738
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.StringKeyword);
739
+ });
740
+
741
+ test('Min: 0', () => {
742
+ const attribute = { min: 0 };
743
+ const modifiers = getAttributeModifiers(attribute);
744
+
745
+ expect(modifiers).toHaveLength(1);
746
+
747
+ expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
748
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
749
+
750
+ const [setMinMax] = modifiers;
751
+ const { typeArguments } = setMinMax;
752
+
753
+ expect(typeArguments).toBeDefined();
754
+ expect(typeArguments).toHaveLength(2);
755
+
756
+ const [definition, typeofMinMax] = typeArguments;
757
+
758
+ // Min/Max
759
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
760
+ expect(definition.members).toHaveLength(1);
761
+
762
+ const [min] = definition.members;
763
+
764
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
765
+ expect(min.name.escapedText).toBe('min');
766
+ expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
767
+ expect(min.type.text).toBe('0');
768
+
769
+ // Check for string keyword on the second typeArgument
770
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
621
771
  });
622
772
  });
623
773
 
@@ -636,7 +786,7 @@ describe('Attributes', () => {
636
786
  expect(modifiers).toHaveLength(1);
637
787
 
638
788
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
639
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
789
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
640
790
 
641
791
  expect(modifiers[0].typeArguments).toHaveLength(1);
642
792
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -660,7 +810,7 @@ describe('Attributes', () => {
660
810
  expect(modifiers).toHaveLength(1);
661
811
 
662
812
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
663
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
813
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
664
814
 
665
815
  expect(modifiers[0].typeArguments).toHaveLength(1);
666
816
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -684,7 +834,7 @@ describe('Attributes', () => {
684
834
  expect(modifiers).toHaveLength(1);
685
835
 
686
836
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
687
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
837
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
688
838
 
689
839
  expect(modifiers[0].typeArguments).toHaveLength(1);
690
840
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -726,7 +876,7 @@ describe('Attributes', () => {
726
876
  expect(modifiers).toHaveLength(1);
727
877
 
728
878
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
729
- expect(modifiers[0].typeName.escapedText).toBe('DefaultTo');
879
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
730
880
 
731
881
  expect(modifiers[0].typeArguments).toHaveLength(1);
732
882
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TrueKeyword);
@@ -739,7 +889,7 @@ describe('Attributes', () => {
739
889
  expect(modifiers).toHaveLength(1);
740
890
 
741
891
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
742
- expect(modifiers[0].typeName.escapedText).toBe('DefaultTo');
892
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
743
893
 
744
894
  expect(modifiers[0].typeArguments).toHaveLength(1);
745
895
  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
  });