2022-07-07 05:23:54 +00:00
|
|
|
import { describe, expect, it, vi } from "vitest";
|
2022-08-09 08:32:16 +00:00
|
|
|
import {
|
|
|
|
convertAttributeNameToForm,
|
|
|
|
convertFormValuesToObject,
|
|
|
|
convertToFormValues,
|
|
|
|
} from "./util";
|
2021-12-08 15:08:42 +00:00
|
|
|
|
2022-07-07 05:23:54 +00:00
|
|
|
vi.mock("react");
|
2021-12-08 15:08:42 +00:00
|
|
|
|
2022-08-09 08:32:16 +00:00
|
|
|
const TOKEN = "🍺";
|
|
|
|
|
2021-12-08 15:08:42 +00:00
|
|
|
describe("Tests the form convert util functions", () => {
|
|
|
|
it("convert to form values", () => {
|
|
|
|
const given = {
|
|
|
|
name: "client",
|
|
|
|
other: { one: "1", two: "2" },
|
|
|
|
attributes: { one: ["1"] },
|
|
|
|
};
|
|
|
|
const values: { [index: string]: any } = {};
|
|
|
|
const spy = (name: string, value: any) => (values[name] = value);
|
|
|
|
|
|
|
|
//when
|
|
|
|
convertToFormValues(given, spy);
|
|
|
|
|
|
|
|
//then
|
|
|
|
expect(values).toEqual({
|
|
|
|
name: "client",
|
|
|
|
other: { one: "1", two: "2" },
|
|
|
|
attributes: [
|
|
|
|
{ key: "one", value: "1" },
|
|
|
|
{ key: "", value: "" },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("convert save values", () => {
|
|
|
|
const given = {
|
|
|
|
name: "client",
|
|
|
|
attributes: [{ key: "one", value: "1" }],
|
2022-08-09 08:32:16 +00:00
|
|
|
config: { [`one${TOKEN}two`]: "3" },
|
2021-12-08 15:08:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//when
|
|
|
|
const values = convertFormValuesToObject(given);
|
|
|
|
|
|
|
|
//then
|
|
|
|
expect(values).toEqual({
|
|
|
|
name: "client",
|
|
|
|
attributes: { one: ["1"] },
|
|
|
|
config: { "one.two": "3" },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("convert attributes flatten", () => {
|
|
|
|
const given = {
|
|
|
|
name: "test",
|
|
|
|
description: "",
|
|
|
|
type: "default",
|
|
|
|
attributes: {
|
2022-08-09 08:32:16 +00:00
|
|
|
[`display${TOKEN}on${TOKEN}consent${TOKEN}screen`]: "true",
|
|
|
|
[`include${TOKEN}in${TOKEN}token${TOKEN}scope`]: "true",
|
|
|
|
[`gui${TOKEN}order`]: "1",
|
|
|
|
[`consent${TOKEN}screen${TOKEN}text`]: "",
|
2021-12-08 15:08:42 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
//when
|
|
|
|
const values = convertFormValuesToObject(given);
|
|
|
|
|
|
|
|
//then
|
|
|
|
expect(values).toEqual({
|
|
|
|
name: "test",
|
|
|
|
description: "",
|
|
|
|
type: "default",
|
|
|
|
attributes: {
|
|
|
|
"display.on.consent.screen": "true",
|
|
|
|
"include.in.token.scope": "true",
|
|
|
|
"gui.order": "1",
|
|
|
|
"consent.screen.text": "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("convert flatten attributes to object", () => {
|
|
|
|
const given = {
|
|
|
|
attributes: {
|
|
|
|
"display.on.consent.screen": "true",
|
|
|
|
"include.in.token.scope": "true",
|
|
|
|
"gui.order": "1",
|
|
|
|
"consent.screen.text": "",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const values: { [index: string]: any } = {};
|
|
|
|
const spy = (name: string, value: any) => (values[name] = value);
|
|
|
|
|
|
|
|
//when
|
|
|
|
convertToFormValues(given, spy);
|
|
|
|
|
|
|
|
//then
|
|
|
|
expect(values).toEqual({
|
2022-08-09 08:32:16 +00:00
|
|
|
[`attributes.display${TOKEN}on${TOKEN}consent${TOKEN}screen`]: "true",
|
|
|
|
[`attributes.include${TOKEN}in${TOKEN}token${TOKEN}scope`]: "true",
|
|
|
|
[`attributes.gui${TOKEN}order`]: "1",
|
|
|
|
[`attributes.consent${TOKEN}screen${TOKEN}text`]: "",
|
2021-12-08 15:08:42 +00:00
|
|
|
});
|
|
|
|
});
|
2022-04-05 07:39:32 +00:00
|
|
|
|
|
|
|
it("convert empty to empty object", () => {
|
|
|
|
const given = { attributes: [{ key: "", value: "" }] };
|
|
|
|
|
|
|
|
//when
|
|
|
|
const values = convertFormValuesToObject(given);
|
|
|
|
|
|
|
|
//then
|
|
|
|
expect(values).toEqual({
|
|
|
|
attributes: {},
|
|
|
|
});
|
|
|
|
});
|
2022-04-27 15:32:47 +00:00
|
|
|
|
|
|
|
it("convert single element arrays to string", () => {
|
|
|
|
const given = {
|
2022-08-09 08:32:16 +00:00
|
|
|
config: {
|
|
|
|
group: ["one"],
|
|
|
|
"another.nested": ["value"],
|
|
|
|
},
|
2022-04-27 15:32:47 +00:00
|
|
|
};
|
2022-08-09 08:32:16 +00:00
|
|
|
const values: { [index: string]: any } = {};
|
|
|
|
const spy = (name: string, value: any) => (values[name] = value);
|
2022-04-27 15:32:47 +00:00
|
|
|
|
|
|
|
//when
|
2022-08-09 08:32:16 +00:00
|
|
|
convertToFormValues(given, spy);
|
2022-04-27 15:32:47 +00:00
|
|
|
|
|
|
|
//then
|
2022-08-09 08:32:16 +00:00
|
|
|
expect(values).toEqual({
|
|
|
|
"config.group": "one",
|
|
|
|
[`config.another${TOKEN}nested`]: "value",
|
2022-04-27 15:32:47 +00:00
|
|
|
});
|
|
|
|
});
|
2022-08-09 08:32:16 +00:00
|
|
|
|
|
|
|
it("should convert attribute name to form", () => {
|
|
|
|
const given = "attributes.some.strange.attribute";
|
|
|
|
|
|
|
|
//when
|
|
|
|
const form = convertAttributeNameToForm(given);
|
|
|
|
|
|
|
|
//then
|
|
|
|
expect(form).toEqual(`attributes.some${TOKEN}strange${TOKEN}attribute`);
|
|
|
|
});
|
2021-12-08 15:08:42 +00:00
|
|
|
});
|