From b13c79c63b809bfdb281dc74a7eaac028ece56a5 Mon Sep 17 00:00:00 2001 From: Ilia Znamenskii Date: Thu, 5 Sep 2024 11:03:24 +0200 Subject: [PATCH] add unit tests --- .../propertyPaneConfig/contentConfig.test.ts | 35 ++++++++++ .../propertyPaneConfig/contentConfig.ts | 64 ++++++++++--------- 2 files changed, 68 insertions(+), 31 deletions(-) create mode 100644 app/client/src/widgets/wds/WDSComboBoxWidget/config/propertyPaneConfig/contentConfig.test.ts diff --git a/app/client/src/widgets/wds/WDSComboBoxWidget/config/propertyPaneConfig/contentConfig.test.ts b/app/client/src/widgets/wds/WDSComboBoxWidget/config/propertyPaneConfig/contentConfig.test.ts new file mode 100644 index 00000000000..d53d00b39e5 --- /dev/null +++ b/app/client/src/widgets/wds/WDSComboBoxWidget/config/propertyPaneConfig/contentConfig.test.ts @@ -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, + }, + ]); + }); +}); diff --git a/app/client/src/widgets/wds/WDSComboBoxWidget/config/propertyPaneConfig/contentConfig.ts b/app/client/src/widgets/wds/WDSComboBoxWidget/config/propertyPaneConfig/contentConfig.ts index 95d8f24bda1..9e41483d389 100644 --- a/app/client/src/widgets/wds/WDSComboBoxWidget/config/propertyPaneConfig/contentConfig.ts +++ b/app/client/src/widgets/wds/WDSComboBoxWidget/config/propertyPaneConfig/contentConfig.ts @@ -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 = { + 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", @@ -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 = { - 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",