feat: add missing attributes to Media in typescript generator (#19329)

This commit is contained in:
Benjamin Robinet 2024-05-22 10:40:37 +02:00 committed by GitHub
parent 5277eaf60c
commit bd269fa8b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 86 additions and 2 deletions

View File

@ -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'] };

View File

@ -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;