Skip to content

Commit

Permalink
fix: migration from single to multi value select works automatically (#…
Browse files Browse the repository at this point in the history
…1809)

Co-authored-by: Sebastian <sebastian.leidig@gmail.com>
  • Loading branch information
TheSlimvReal and sleidig authored Apr 11, 2023
1 parent cafa57e commit 201df90
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
70 changes: 70 additions & 0 deletions src/app/core/entity/schema-datatypes/datatype-array.spec.ts
Original file line number Diff line number Diff line change
@@ -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([]);
});
});
12 changes: 5 additions & 7 deletions src/app/core/entity/schema-datatypes/datatype-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit 201df90

Please sign in to comment.