|
| 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 domain-bearing `select` shows a stored code as its |
| 11 | + * option's name, and still stores the code (objectstack#28068). |
| 12 | + * |
| 13 | + * Company `country` and Localization `default_country` are selects over the |
| 14 | + * `iso_3166_alpha2` domain whose options name each code ("Germany (DE)"). The |
| 15 | + * combobox showed the stored `DE` bare in the box, so the names were visible |
| 16 | + * only in the suggestion list. The suggest-but-allow-anything contract of the |
| 17 | + * domain control is unchanged: a value outside the table is still committed |
| 18 | + * verbatim, and the server stays the judge of the domain. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 22 | +import { render, cleanup, fireEvent } from '@testing-library/react'; |
| 23 | +import { useState } from 'react'; |
| 24 | + |
| 25 | +import { SettingsField } from '../SettingsField'; |
| 26 | +import type { Specifier } from '../types'; |
| 27 | + |
| 28 | +/** Abridged `company.country` as the server declares it. */ |
| 29 | +const COUNTRY: Specifier = { |
| 30 | + type: 'select', |
| 31 | + key: 'country', |
| 32 | + label: 'Country', |
| 33 | + valueDomain: 'iso_3166_alpha2', |
| 34 | + options: [ |
| 35 | + { value: 'US', label: 'United States (US)' }, |
| 36 | + { value: 'DE', label: 'Germany (DE)' }, |
| 37 | + { value: 'FR', label: 'France (FR)' }, |
| 38 | + ], |
| 39 | +}; |
| 40 | + |
| 41 | +/** A settings page's draft in one row: `value` follows what `onChange` commits. */ |
| 42 | +function Row({ initial, onCommit }: { initial: unknown; onCommit: (v: unknown) => void }) { |
| 43 | + const [value, setValue] = useState<unknown>(initial); |
| 44 | + return ( |
| 45 | + <SettingsField |
| 46 | + spec={COUNTRY} |
| 47 | + value={value} |
| 48 | + onChange={(next) => { |
| 49 | + onCommit(next); |
| 50 | + setValue(next); |
| 51 | + }} |
| 52 | + /> |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +function renderRow(initial: unknown) { |
| 57 | + const onCommit = vi.fn(); |
| 58 | + const view = render(<Row initial={initial} onCommit={onCommit} />); |
| 59 | + const input = view.container.querySelector('input') as HTMLInputElement; |
| 60 | + return { ...view, input, onCommit }; |
| 61 | +} |
| 62 | + |
| 63 | +afterEach(cleanup); |
| 64 | + |
| 65 | +describe('SettingsField — a domain-bearing select shows the option name for a stored code', () => { |
| 66 | + it('a stored DE reads as its name in the box', () => { |
| 67 | + const { input } = renderRow('DE'); |
| 68 | + expect(input.value).toBe('Germany (DE)'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('picking a named option stores its code and keeps the name in the box', () => { |
| 72 | + const { input, onCommit } = renderRow('US'); |
| 73 | + // A datalist pick writes the suggestion's `value` attribute into the box. |
| 74 | + fireEvent.change(input, { target: { value: 'France (FR)' } }); |
| 75 | + expect(onCommit).toHaveBeenLastCalledWith('FR'); |
| 76 | + expect(input.value).toBe('France (FR)'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('a code typed by hand is committed as typed and reads as its name on blur', () => { |
| 80 | + const { input, onCommit } = renderRow(null); |
| 81 | + fireEvent.change(input, { target: { value: 'DE' } }); |
| 82 | + expect(onCommit).toHaveBeenLastCalledWith('DE'); |
| 83 | + expect(input.value).toBe('DE'); |
| 84 | + fireEvent.blur(input); |
| 85 | + expect(input.value).toBe('Germany (DE)'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('a free-text value outside the table is still committed verbatim', () => { |
| 89 | + const { input, onCommit } = renderRow('DE'); |
| 90 | + fireEvent.change(input, { target: { value: 'CH' } }); |
| 91 | + expect(onCommit).toHaveBeenLastCalledWith('CH'); |
| 92 | + fireEvent.change(input, { target: { value: 'Germ' } }); |
| 93 | + expect(onCommit).toHaveBeenLastCalledWith('Germ'); |
| 94 | + fireEvent.blur(input); |
| 95 | + // No option carries it, so it shows as itself. |
| 96 | + expect(input.value).toBe('Germ'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('an unmatched stored value shows verbatim, and an emptied box is unset', () => { |
| 100 | + const { input, onCommit } = renderRow('CH'); |
| 101 | + expect(input.value).toBe('CH'); |
| 102 | + fireEvent.change(input, { target: { value: '' } }); |
| 103 | + expect(onCommit).toHaveBeenLastCalledWith(null); |
| 104 | + expect(input.value).toBe(''); |
| 105 | + }); |
| 106 | + |
| 107 | + it('a value changed from outside the box shows as its name', () => { |
| 108 | + // A reset or a re-read after save hands the row a new value while the |
| 109 | + // admin's last pick is still held in the box. |
| 110 | + const { container, rerender } = render( |
| 111 | + <SettingsField spec={COUNTRY} value="US" onChange={() => {}} />, |
| 112 | + ); |
| 113 | + const box = container.querySelector('input') as HTMLInputElement; |
| 114 | + fireEvent.change(box, { target: { value: 'United States (US)' } }); |
| 115 | + rerender(<SettingsField spec={COUNTRY} value="DE" onChange={() => {}} />); |
| 116 | + expect(box.value).toBe('Germany (DE)'); |
| 117 | + }); |
| 118 | +}); |
0 commit comments