Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migration from single to multi value select works automatically #1809

Merged
merged 4 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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