From 365fd1e047c00ea96f7ef827f8f754719e9387a7 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 20 Feb 2025 08:12:46 -0700 Subject: [PATCH] Fix error translating legacy setting options (#2648) --- scripts/collect-i18n-general.ts | 7 ++- .../dialog/content/setting/SettingItem.vue | 6 +++ .../setting/__tests__/SettingItem.spec.ts | 47 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/components/dialog/content/setting/__tests__/SettingItem.spec.ts diff --git a/scripts/collect-i18n-general.ts b/scripts/collect-i18n-general.ts index 68592c411..63d97d530 100644 --- a/scripts/collect-i18n-general.ts +++ b/scripts/collect-i18n-general.ts @@ -69,7 +69,12 @@ test('collect-i18n-general', async ({ comfyPage }) => { name: setting.name, tooltip: setting.tooltip, category: setting.category, - options: setting.options + options: + typeof setting.options === 'function' + ? // @ts-expect-error: Audit and deprecate usage of legacy options type: + // (value) => [string | {text: string, value: string}] + setting.options(setting.defaultValue ?? '') + : setting.options })) }) diff --git a/src/components/dialog/content/setting/SettingItem.vue b/src/components/dialog/content/setting/SettingItem.vue index c3b0e114d..92e00c92d 100644 --- a/src/components/dialog/content/setting/SettingItem.vue +++ b/src/components/dialog/content/setting/SettingItem.vue @@ -32,6 +32,12 @@ const props = defineProps<{ const { t } = useI18n() function translateOptions(options: (SettingOption | string)[]) { + if (typeof options === 'function') { + // @ts-expect-error: Audit and deprecate usage of legacy options type: + // (value) => [string | {text: string, value: string}] + return translateOptions(options(props.setting.value ?? '')) + } + return options.map((option) => { const optionLabel = typeof option === 'string' ? option : option.text const optionValue = typeof option === 'string' ? option : option.value diff --git a/src/components/dialog/content/setting/__tests__/SettingItem.spec.ts b/src/components/dialog/content/setting/__tests__/SettingItem.spec.ts new file mode 100644 index 000000000..dcbed0fa4 --- /dev/null +++ b/src/components/dialog/content/setting/__tests__/SettingItem.spec.ts @@ -0,0 +1,47 @@ +// @ts-strict-ignore +import { mount } from '@vue/test-utils' +import { createPinia } from 'pinia' +import PrimeVue from 'primevue/config' +import { describe, expect, it, vi } from 'vitest' +import { createI18n } from 'vue-i18n' + +import SettingItem from '../SettingItem.vue' + +const i18n = createI18n({ + legacy: false, + locale: 'en' +}) + +vi.mock('@/utils/formatUtil', () => ({ + normalizeI18nKey: vi.fn() +})) + +describe('SettingItem', () => { + const mountComponent = (props: any, options = {}): any => { + return mount(SettingItem, { + global: { + plugins: [PrimeVue, i18n, createPinia()] + }, + props, + ...options + }) + } + + it('translates options that use legacy type', () => { + const wrapper = mountComponent({ + setting: { + id: 'Comfy.NodeInputConversionSubmenus', + name: 'Node Input Conversion Submenus', + type: 'combo', + value: 'Top', + options: (value: string) => ['Correctly Translated'] + } + }) + + // Get the options property of the FormItem + const options = wrapper.vm.formItem.options + expect(options).toEqual([ + { text: 'Correctly Translated', value: 'Correctly Translated' } + ]) + }) +})