From 530ca75dd0369c6ec14730df02f9995e19d0b788 Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 14 Feb 2025 07:47:18 -0700 Subject: [PATCH] Fix remote widget undefined arg (#2551) --- src/composables/widgets/useComboWidget.ts | 2 +- src/stores/widgetStore.ts | 6 ++- .../widgets/useComboWidget.test.ts | 44 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 tests-ui/tests/composables/widgets/useComboWidget.test.ts diff --git a/src/composables/widgets/useComboWidget.ts b/src/composables/widgets/useComboWidget.ts index c2fa0358d..ffd399342 100644 --- a/src/composables/widgets/useComboWidget.ts +++ b/src/composables/widgets/useComboWidget.ts @@ -15,7 +15,7 @@ export const useComboWidget = () => { inputData: InputSpec ) => { const widgetStore = useWidgetStore() - const { remote, options } = inputData[1] + const { remote, options } = inputData[1] || {} const defaultValue = widgetStore.getDefaultValue(inputData) const res = { diff --git a/src/stores/widgetStore.ts b/src/stores/widgetStore.ts index 140731d81..5d3ac6b51 100644 --- a/src/stores/widgetStore.ts +++ b/src/stores/widgetStore.ts @@ -47,9 +47,11 @@ export const useWidgetStore = defineStore('widget', () => { if (Array.isArray(inputData[0])) return getDefaultValue(transformComboInput(inputData)) - const widgetType = getWidgetType(inputData[0], inputData[1].name) + const widgetType = getWidgetType(inputData[0], inputData[1]?.name) const [_, props] = inputData + + if (!props) return undefined if (props.default) return props.default if (widgetType === 'COMBO' && props.options?.length) return props.options[0] @@ -63,7 +65,7 @@ export const useWidgetStore = defineStore('widget', () => { 'COMBO', { options: inputData[0], - ...Object(inputData[1]) + ...Object(inputData[1] || {}) } ] : inputData diff --git a/tests-ui/tests/composables/widgets/useComboWidget.test.ts b/tests-ui/tests/composables/widgets/useComboWidget.test.ts new file mode 100644 index 000000000..1c952c98a --- /dev/null +++ b/tests-ui/tests/composables/widgets/useComboWidget.test.ts @@ -0,0 +1,44 @@ +import { createPinia, setActivePinia } from 'pinia' + +import { useComboWidget } from '@/composables/widgets/useComboWidget' +import type { InputSpec } from '@/types/apiTypes' + +jest.mock('@/scripts/widgets', () => ({ + addValueControlWidgets: jest.fn() +})) + +describe('useComboWidget', () => { + beforeEach(() => { + setActivePinia(createPinia()) + jest.clearAllMocks() + }) + + it('should handle undefined spec', () => { + const constructor = useComboWidget() + const mockNode = { + addWidget: jest.fn().mockReturnValue({ options: {} } as any) + } + + const inputSpec: InputSpec = ['COMBO', undefined] + + const widget = constructor( + mockNode as any, + 'inputName', + inputSpec, + undefined as any + ) + + expect(mockNode.addWidget).toHaveBeenCalledWith( + 'combo', + 'inputName', + undefined, // default value + expect.any(Function), // callback + expect.objectContaining({ + values: 'COMBO' + }) + ) + expect(widget).toEqual({ + widget: { options: {} } + }) + }) +})