diff --git a/src/app/core/entity/schema-datatypes/datatype-array.spec.ts b/src/app/core/entity/schema-datatypes/datatype-array.spec.ts new file mode 100644 index 0000000000..919ca2a888 --- /dev/null +++ b/src/app/core/entity/schema-datatypes/datatype-array.spec.ts @@ -0,0 +1,70 @@ +import { EntitySchemaField } from "../schema/entity-schema-field"; +import { EntitySchemaService } from "../schema/entity-schema.service"; +import { ConfigurableEnumDatatype } from "../../configurable-enum/configurable-enum-datatype/configurable-enum-datatype"; +import { defaultInteractionTypes } from "../../config/default-config/default-interaction-types"; +import { arrayEntitySchemaDatatype } from "./datatype-array"; + +describe("Schema data type: array", () => { + let schemaService: EntitySchemaService; + const schema: EntitySchemaField = { + dataType: "array", + innerDataType: "configurable-enum", + additional: "test", + }; + beforeEach(() => { + schemaService = new EntitySchemaService(); + schemaService.registerSchemaDatatype( + new ConfigurableEnumDatatype({ + getEnumValues: () => defaultInteractionTypes, + } as any) + ); + }); + + it("should transform enums inside arrays", () => { + const value = defaultInteractionTypes.map(({ id }) => id); + + const obj = arrayEntitySchemaDatatype.transformToObjectFormat( + value, + schema, + schemaService + ); + + expect(obj).toEqual(defaultInteractionTypes); + + const db = arrayEntitySchemaDatatype.transformToDatabaseFormat( + obj, + schema, + schemaService + ); + + expect(db).toEqual(value); + }); + + it("should automatically wrap value into array (and transform to inner type) if not an array yet", () => { + const value = defaultInteractionTypes[1].id; + + const obj = arrayEntitySchemaDatatype.transformToObjectFormat( + value, + schema, + schemaService + ); + + expect(obj).toEqual([defaultInteractionTypes[1]]); + }); + + it("should transform empty values as an empty array", () => { + let obj = arrayEntitySchemaDatatype.transformToObjectFormat( + undefined, + schema, + schemaService + ); + expect(obj).toEqual([]); + + obj = arrayEntitySchemaDatatype.transformToObjectFormat( + "", + schema, + schemaService + ); + expect(obj).toEqual([]); + }); +}); diff --git a/src/app/core/entity/schema-datatypes/datatype-array.ts b/src/app/core/entity/schema-datatypes/datatype-array.ts index 6fc37f9d6d..812eca2f21 100644 --- a/src/app/core/entity/schema-datatypes/datatype-array.ts +++ b/src/app/core/entity/schema-datatypes/datatype-array.ts @@ -49,9 +49,8 @@ export const arrayEntitySchemaDatatype: EntitySchemaDatatype = { return value; } - const arrayElementDatatype: EntitySchemaDatatype = schemaService.getDatatypeOrDefault( - schemaField.innerDataType - ); + const arrayElementDatatype: EntitySchemaDatatype = + schemaService.getDatatypeOrDefault(schemaField.innerDataType); return value.map((el) => arrayElementDatatype.transformToDatabaseFormat( el, @@ -74,12 +73,11 @@ export const arrayEntitySchemaDatatype: EntitySchemaDatatype = { value, parent ); - return value; + value = value ? [value] : []; } - const arrayElementDatatype: EntitySchemaDatatype = schemaService.getDatatypeOrDefault( - schemaField.innerDataType - ); + const arrayElementDatatype: EntitySchemaDatatype = + schemaService.getDatatypeOrDefault(schemaField.innerDataType); return value.map((el) => arrayElementDatatype.transformToObjectFormat(