From bd269fa8b563708f31cfa9a6715b74896a0fed40 Mon Sep 17 00:00:00 2001 From: Benjamin Robinet Date: Wed, 22 May 2024 10:40:37 +0200 Subject: [PATCH] feat: add missing attributes to Media in typescript generator (#19329) --- .../generators/schemas/attributes.test.js | 66 +++++++++++++++++++ .../lib/generators/common/models/mappers.js | 22 ++++++- 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/packages/utils/typescript/lib/__tests__/generators/schemas/attributes.test.js b/packages/utils/typescript/lib/__tests__/generators/schemas/attributes.test.js index 43ad7a06dc9..0dce5613760 100644 --- a/packages/utils/typescript/lib/__tests__/generators/schemas/attributes.test.js +++ b/packages/utils/typescript/lib/__tests__/generators/schemas/attributes.test.js @@ -148,6 +148,72 @@ describe('Attributes', () => { expect(addImport).toHaveBeenCalledWith('Attribute'); }; + describe('Media', () => { + test('Media with multiple and with no allowedTypes', () => { + const attribute = { type: 'media', multiple: true }; + const typeNode = getAttributeType('foo', attribute); + + defaultAssertions(typeNode, 'Attribute.Media'); + + expect(typeNode.typeArguments).toHaveLength(2); + + expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UndefinedKeyword); + + expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword); + }); + + test('Media without multiple with allowedTypes', () => { + const attribute = { type: 'media', allowedTypes: ['images', 'videos'] }; + const typeNode = getAttributeType('foo', attribute); + + defaultAssertions(typeNode, 'Attribute.Media'); + + expect(typeNode.typeArguments).toHaveLength(1); + + expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType); + + const unionTypes = typeNode.typeArguments[0].types; + + attribute.allowedTypes.forEach((value, index) => { + const element = unionTypes[index]; + + expect(element.kind).toBe(ts.SyntaxKind.StringLiteral); + expect(element.text).toBe(value); + }); + }); + + test('Media with multiple and with allowedTypes', () => { + const attribute = { type: 'media', multiple: true, allowedTypes: ['images', 'videos'] }; + const typeNode = getAttributeType('foo', attribute); + + defaultAssertions(typeNode, 'Attribute.Media'); + + expect(typeNode.typeArguments).toHaveLength(2); + + expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType); + + const unionTypes = typeNode.typeArguments[0].types; + + attribute.allowedTypes.forEach((value, index) => { + const element = unionTypes[index]; + + expect(element.kind).toBe(ts.SyntaxKind.StringLiteral); + expect(element.text).toBe(value); + }); + + expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword); + }); + + test('Media without multiple and with no allowedTypes', () => { + const attribute = { type: 'media' }; + const typeNode = getAttributeType('foo', attribute); + + defaultAssertions(typeNode, 'Attribute.Media'); + + expect(typeNode.typeArguments).toBeUndefined(); + }); + }); + describe('Enumeration', () => { test('Enumeration with an enum property', () => { const attribute = { type: 'enumeration', enum: ['a', 'b', 'c'] }; diff --git a/packages/utils/typescript/lib/generators/common/models/mappers.js b/packages/utils/typescript/lib/generators/common/models/mappers.js index 43bfe51da4c..fde9de62110 100644 --- a/packages/utils/typescript/lib/generators/common/models/mappers.js +++ b/packages/utils/typescript/lib/generators/common/models/mappers.js @@ -90,8 +90,26 @@ module.exports = { blocks() { return [withAttributeNamespace('Blocks')]; }, - media() { - return [withAttributeNamespace('Media')]; + media({ attribute }) { + const { allowedTypes, multiple } = attribute; + + const params = []; + + const typesParam = allowedTypes + ? factory.createUnionTypeNode( + allowedTypes.map((allowedType) => factory.createStringLiteral(allowedType)) + ) + : factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword); + + if (allowedTypes || multiple) { + params.push(typesParam); + } + + if (multiple) { + params.push(factory.createTrue()); + } + + return [withAttributeNamespace('Media'), params]; }, relation({ uid, attribute }) { const { relation, target } = attribute;