Fix remote widget undefined arg (#2551)

This commit is contained in:
bymyself
2025-02-14 07:47:18 -07:00
committed by GitHub
parent f9c2db5908
commit 530ca75dd0
3 changed files with 49 additions and 3 deletions

View File

@@ -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 = {

View File

@@ -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

View File

@@ -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: {} }
})
})
})