Skip to content

Commit

Permalink
feat: add missing attributes to Media in typescript generator (#19329)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminrobinet committed May 22, 2024
1 parent 5277eaf commit bd269fa
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'] };
Expand Down
22 changes: 20 additions & 2 deletions packages/utils/typescript/lib/generators/common/models/mappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bd269fa

Please sign in to comment.