|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * SettingsField — a closed `select` whose stored value is no longer among the |
| 11 | + * manifest's `options` names that value as "no longer available" |
| 12 | + * (objectstack#37480). |
| 13 | + * |
| 14 | + * An option removed in a later release (the knowledge adapter `ragflow`) left |
| 15 | + * an environment that had saved it with a blank trigger: no label and no |
| 16 | + * placeholder, because Radix shows the selected item's text and no item carried |
| 17 | + * the stored code. The select now adds one disabled item for that code, so the |
| 18 | + * trigger reads the code with the locale's "no longer available" mark, and the |
| 19 | + * admin can still pick a current option. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 23 | +import { render, screen, cleanup, fireEvent } from '@testing-library/react'; |
| 24 | +import { createI18n, I18nProvider } from '@object-ui/i18n'; |
| 25 | + |
| 26 | +import { SettingsField } from '../SettingsField'; |
| 27 | +import type { Specifier } from '../types'; |
| 28 | + |
| 29 | +afterEach(cleanup); |
| 30 | + |
| 31 | +/** A closed select: exhaustive `options`, no `valueDomain`. */ |
| 32 | +const ADAPTER: Specifier = { |
| 33 | + type: 'select', |
| 34 | + key: 'adapter', |
| 35 | + label: 'Adapter', |
| 36 | + options: [ |
| 37 | + { value: 'builtin', label: 'Built-in' }, |
| 38 | + { value: 'dify', label: 'Dify' }, |
| 39 | + ], |
| 40 | +}; |
| 41 | + |
| 42 | +function renderIn(language: string, value: unknown) { |
| 43 | + const instance = createI18n({ defaultLanguage: language, detectBrowserLanguage: false }); |
| 44 | + render( |
| 45 | + <I18nProvider instance={instance} persistLanguage={false}> |
| 46 | + <SettingsField spec={ADAPTER} value={value} onChange={vi.fn()} /> |
| 47 | + </I18nProvider>, |
| 48 | + ); |
| 49 | + return instance; |
| 50 | +} |
| 51 | + |
| 52 | +describe('objectstack#37480 — a stored select value outside the current options is named', () => { |
| 53 | + it('under en, the trigger reads the stored code marked "no longer available"', () => { |
| 54 | + const en = renderIn('en', 'ragflow'); |
| 55 | + const named = en.t('console.settingsView.selectValueRetired', { value: 'ragflow' }); |
| 56 | + |
| 57 | + // The key resolves in the en pack (not the raw key, not the bare code). |
| 58 | + expect(named).toBe('ragflow (no longer available)'); |
| 59 | + expect(screen.getByRole('combobox')).toHaveTextContent(named); |
| 60 | + expect(screen.getByRole('combobox')).not.toHaveTextContent(en.t('common.select')); |
| 61 | + }); |
| 62 | + |
| 63 | + it('under zh-CN, the mark is the zh pack\'s text, not the en words', () => { |
| 64 | + const zh = renderIn('zh-CN', 'ragflow'); |
| 65 | + const zhText = zh.t('console.settingsView.selectValueRetired', { value: 'ragflow' }); |
| 66 | + const enText = zh.getFixedT('en')('console.settingsView.selectValueRetired', { value: 'ragflow' }); |
| 67 | + |
| 68 | + expect(zhText).not.toBe(enText); |
| 69 | + expect(zhText).toContain('ragflow'); |
| 70 | + expect(screen.getByRole('combobox')).toHaveTextContent(zhText); |
| 71 | + }); |
| 72 | + |
| 73 | + it('CONTROL — a stored value among the options renders its label, with no retired mark', () => { |
| 74 | + const en = renderIn('en', 'dify'); |
| 75 | + const combobox = screen.getByRole('combobox'); |
| 76 | + expect(combobox).toHaveTextContent('Dify'); |
| 77 | + expect(combobox).not.toHaveTextContent( |
| 78 | + en.t('console.settingsView.selectValueRetired', { value: 'dify' }), |
| 79 | + ); |
| 80 | + expect(screen.queryByTestId('settings-select-retired')).toBeNull(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('CONTROL — no stored value still shows the placeholder, with no retired mark', () => { |
| 84 | + const en = renderIn('en', undefined); |
| 85 | + expect(screen.getByRole('combobox')).toHaveTextContent(en.t('common.select')); |
| 86 | + expect(screen.queryByTestId('settings-select-retired')).toBeNull(); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +/** The same options drawn as a radio group. */ |
| 91 | +const ADAPTER_RADIO: Specifier = { ...ADAPTER, type: 'radio' }; |
| 92 | + |
| 93 | +function renderRadioIn(language: string, value: unknown) { |
| 94 | + const instance = createI18n({ defaultLanguage: language, detectBrowserLanguage: false }); |
| 95 | + render( |
| 96 | + <I18nProvider instance={instance} persistLanguage={false}> |
| 97 | + <SettingsField spec={ADAPTER_RADIO} value={value} onChange={vi.fn()} /> |
| 98 | + </I18nProvider>, |
| 99 | + ); |
| 100 | + return instance; |
| 101 | +} |
| 102 | + |
| 103 | +describe('objectstack#37480 — a stored radio value outside the current options is named', () => { |
| 104 | + it('the group shows a checked, disabled item reading the stored code marked "no longer available"', () => { |
| 105 | + const en = renderRadioIn('en', 'ragflow'); |
| 106 | + const named = en.t('console.settingsView.selectValueRetired', { value: 'ragflow' }); |
| 107 | + |
| 108 | + const item = screen.getByRole('radio', { name: named }); |
| 109 | + expect(item).toBeChecked(); |
| 110 | + expect(item).toBeDisabled(); |
| 111 | + // The current options stay pickable and unchecked. |
| 112 | + expect(screen.getByRole('radio', { name: 'Dify' })).not.toBeChecked(); |
| 113 | + expect(screen.getByRole('radio', { name: 'Dify' })).toBeEnabled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('CONTROL — a stored value among the options checks its own item, with no retired item', () => { |
| 117 | + renderRadioIn('en', 'dify'); |
| 118 | + expect(screen.getByRole('radio', { name: 'Dify' })).toBeChecked(); |
| 119 | + expect(screen.getAllByRole('radio')).toHaveLength(2); |
| 120 | + expect(screen.queryByTestId('settings-radio-retired')).toBeNull(); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +/** The same options drawn as a multiselect. */ |
| 125 | +const ADAPTER_MULTI: Specifier = { ...ADAPTER, type: 'multiselect' }; |
| 126 | + |
| 127 | +function renderMultiIn(language: string, value: unknown, onChange = vi.fn()) { |
| 128 | + const instance = createI18n({ defaultLanguage: language, detectBrowserLanguage: false }); |
| 129 | + render( |
| 130 | + <I18nProvider instance={instance} persistLanguage={false}> |
| 131 | + <SettingsField spec={ADAPTER_MULTI} value={value} onChange={onChange} /> |
| 132 | + </I18nProvider>, |
| 133 | + ); |
| 134 | + return instance; |
| 135 | +} |
| 136 | + |
| 137 | +describe('objectstack#37480 — a stored multiselect member outside the current options is named', () => { |
| 138 | + it('the member shows as a checked checkbox reading the stored code marked "no longer available"', () => { |
| 139 | + const en = renderMultiIn('en', ['ragflow', 'dify']); |
| 140 | + const named = en.t('console.settingsView.selectValueRetired', { value: 'ragflow' }); |
| 141 | + |
| 142 | + expect(screen.getByRole('checkbox', { name: named })).toBeChecked(); |
| 143 | + expect(screen.getByRole('checkbox', { name: 'Dify' })).toBeChecked(); |
| 144 | + expect(screen.getByRole('checkbox', { name: 'Built-in' })).not.toBeChecked(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('ticking a current option keeps the retired member in the array, as before', () => { |
| 148 | + const onChange = vi.fn(); |
| 149 | + renderMultiIn('en', ['ragflow'], onChange); |
| 150 | + fireEvent.click(screen.getByRole('checkbox', { name: 'Dify' })); |
| 151 | + expect(onChange).toHaveBeenCalledWith(['ragflow', 'dify']); |
| 152 | + }); |
| 153 | + |
| 154 | + it('unticking the retired member drops it from the array', () => { |
| 155 | + const onChange = vi.fn(); |
| 156 | + const en = renderMultiIn('en', ['ragflow', 'dify'], onChange); |
| 157 | + fireEvent.click( |
| 158 | + screen.getByRole('checkbox', { |
| 159 | + name: en.t('console.settingsView.selectValueRetired', { value: 'ragflow' }), |
| 160 | + }), |
| 161 | + ); |
| 162 | + expect(onChange).toHaveBeenCalledWith(['dify']); |
| 163 | + }); |
| 164 | + |
| 165 | + it('CONTROL — members all among the options draw only the option checkboxes', () => { |
| 166 | + renderMultiIn('en', ['dify']); |
| 167 | + expect(screen.getAllByRole('checkbox')).toHaveLength(2); |
| 168 | + expect(screen.getByRole('checkbox', { name: 'Dify' })).toBeChecked(); |
| 169 | + expect(screen.queryByTestId('settings-multiselect-retired')).toBeNull(); |
| 170 | + }); |
| 171 | +}); |
0 commit comments