Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
znamenskii-ilia committed Sep 5, 2024
1 parent 18d36d8 commit b13c79c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { WidgetProps } from "../../../../BaseWidget";
import { handleWidgetTypeUpdate } from "./contentConfig";

describe("handleWidgetTypeUpdate", () => {
it("should update the widget type and type property", () => {
const props = {} as WidgetProps;
const propertyName = "widgetType";
const propertyValue = "COMBOBOX";

expect(handleWidgetTypeUpdate(props, propertyName, propertyValue)).toEqual([
{
propertyPath: propertyName,
propertyValue: propertyValue,
},
{
propertyPath: "type",
propertyValue: "WDS_COMBOBOX_WIDGET",
},
]);
});

it("should not update the type property for unknown widget type", () => {
const props = {} as WidgetProps;
const propertyName = "widgetType";
const propertyValue = "UNKNOWN";

// @ts-expect-error unknown widget type
expect(handleWidgetTypeUpdate(props, propertyName, propertyValue)).toEqual([
{
propertyPath: propertyName,
propertyValue: propertyValue,
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@ import { optionsCustomValidation } from "./validations";

type WidgetTypeValue = "SELECT" | "COMBOBOX";

export const handleWidgetTypeUpdate = (
_props: WidgetProps,
propertyName: string,
propertyValue: WidgetTypeValue,
) => {
const updates: PropertyUpdates[] = [
{
propertyPath: propertyName,
propertyValue: propertyValue,
},
];

// Handle widget morphing
if (propertyName === "widgetType") {
const morphingMap: Record<WidgetTypeValue, string> = {
SELECT: "WDS_SELECT_WIDGET",
COMBOBOX: "WDS_COMBOBOX_WIDGET",
};

const targetWidgetType = morphingMap[propertyValue];

if (targetWidgetType) {
updates.push({
propertyPath: "type",
propertyValue: targetWidgetType,
});
}
}

return updates;
};

export const propertyPaneContentConfig = [
{
sectionName: "Data",
Expand All @@ -28,37 +60,7 @@ export const propertyPaneContentConfig = [
],
isBindProperty: false,
isTriggerProperty: false,
updateHook: (
_props: WidgetProps,
propertyName: string,
propertyValue: WidgetTypeValue,
) => {
const updates: PropertyUpdates[] = [
{
propertyPath: propertyName,
propertyValue: propertyValue,
},
];

// Handle widget morphing
if (propertyName === "widgetType") {
const morphingMap: Record<WidgetTypeValue, string> = {
SELECT: "WDS_SELECT_WIDGET",
COMBOBOX: "WDS_COMBOBOX_WIDGET",
};

const targetWidgetType = morphingMap[propertyValue];

if (targetWidgetType) {
updates.push({
propertyPath: "type",
propertyValue: targetWidgetType,
});
}
}

return updates;
},
updateHook: handleWidgetTypeUpdate,
},
{
helpText: "Displays a list of unique options",
Expand Down

0 comments on commit b13c79c

Please sign in to comment.