|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * ObjectFieldInspector — the API name box refuses a name another field holds |
| 5 | + * and normalises trailing punctuation (objectstack#27517). |
| 6 | + * |
| 7 | + * Measured on the release-candidate walkthrough: selecting all of an Estimated |
| 8 | + * Hours field's API name and typing `status` (another field's name) left the |
| 9 | + * box reading `statu` with no message, and `statu` was saved; typing |
| 10 | + * `Estimated Hours!` saved `estimated_hours_`. The box now shows what the |
| 11 | + * author typed, refuses a taken name in the Add field popover's own sentence, |
| 12 | + * keeps the name the field had when the edit began, and writes the name the |
| 13 | + * new-object dialog's rule derives. |
| 14 | + * |
| 15 | + * Every case types ONE CHARACTER AT A TIME through a host that feeds each |
| 16 | + * patch back in and follows the rename with the selection, as the Studio host |
| 17 | + * does: the dropped keystroke only exists in that loop. |
| 18 | + */ |
| 19 | + |
| 20 | +import * as React from 'react'; |
| 21 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 22 | +import { render, screen, fireEvent, cleanup } from '@testing-library/react'; |
| 23 | + |
| 24 | +vi.mock('../useMetadata', () => ({ |
| 25 | + useMetadataClient: () => ({ |
| 26 | + list: vi.fn().mockResolvedValue([]), |
| 27 | + listDrafts: vi.fn().mockResolvedValue([]), |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock('../previews/useObjectFields', () => ({ |
| 32 | + useObjectFields: () => ({ fields: [], loading: false, error: null }), |
| 33 | +})); |
| 34 | + |
| 35 | +import { ObjectFieldInspector } from './ObjectFieldInspector'; |
| 36 | +import { fieldNameTakenRefusal } from '../../studio-design/AddFieldPopover'; |
| 37 | + |
| 38 | +afterEach(cleanup); |
| 39 | + |
| 40 | +type Fields = Record<string, Record<string, unknown>>; |
| 41 | + |
| 42 | +const WORK_ORDER: Fields = { |
| 43 | + estimated_hours: { type: 'number', label: 'Estimated Hours' }, |
| 44 | + status: { type: 'select', label: 'Status', options: [{ value: 'new', label: 'New Request' }] }, |
| 45 | +}; |
| 46 | + |
| 47 | +/** The host's draft as last patched, read by the assertions. */ |
| 48 | +let saved: Fields = {}; |
| 49 | + |
| 50 | +function Host({ fields: initial, id }: { fields: Fields; id: string }) { |
| 51 | + const [fields, setFields] = React.useState(initial); |
| 52 | + const [selection, setSelection] = React.useState<any>({ kind: 'field', id }); |
| 53 | + return ( |
| 54 | + <ObjectFieldInspector |
| 55 | + type="object" |
| 56 | + name="work_order" |
| 57 | + draft={{ name: 'work_order', fields }} |
| 58 | + selection={selection} |
| 59 | + onPatch={(patch: any) => { |
| 60 | + saved = patch.fields; |
| 61 | + setFields(patch.fields); |
| 62 | + }} |
| 63 | + onClearSelection={() => {}} |
| 64 | + onSelectionChange={(next: any) => next && setSelection(next)} |
| 65 | + readOnly={false} |
| 66 | + locale={'en-US'} |
| 67 | + /> |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +function apiNameBox(): HTMLInputElement { |
| 72 | + return screen.getByTestId('field-apiname-input') as HTMLInputElement; |
| 73 | +} |
| 74 | + |
| 75 | +/** Select all, then one `change` event per character — a person at a keyboard. */ |
| 76 | +function typeOver(text: string): void { |
| 77 | + fireEvent.focus(apiNameBox()); |
| 78 | + for (let i = 1; i <= text.length; i += 1) { |
| 79 | + fireEvent.change(apiNameBox(), { target: { value: text.slice(0, i) } }); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function renderWorkOrder(): void { |
| 84 | + saved = WORK_ORDER; |
| 85 | + render(<Host fields={WORK_ORDER} id="estimated_hours" />); |
| 86 | +} |
| 87 | + |
| 88 | +describe('ObjectFieldInspector — API name box refuses a taken name (#27517)', () => { |
| 89 | + it('shows every keystroke and the Add field popover refusal, and keeps the previous name', () => { |
| 90 | + renderWorkOrder(); |
| 91 | + typeOver('status'); |
| 92 | + // Pre-fix the box read `statu`: the refused keystroke was dropped silently. |
| 93 | + expect(apiNameBox()).toHaveValue('status'); |
| 94 | + const refusal = screen.getByTestId('field-apiname-refusal'); |
| 95 | + // The same sentence the Add field popover says for the same name. |
| 96 | + expect(refusal.textContent).toBe(fieldNameTakenRefusal('status', ['status'], 'en-US')); |
| 97 | + expect(apiNameBox()).toHaveAttribute('aria-invalid', 'true'); |
| 98 | + expect(apiNameBox().getAttribute('aria-describedby')).toBe(refusal.id); |
| 99 | + // Saves nothing: the field still carries the name it had before the edit, |
| 100 | + // not the last free prefix (`statu`) of the refused one. |
| 101 | + expect(Object.keys(saved).sort()).toEqual(['estimated_hours', 'status']); |
| 102 | + }); |
| 103 | + |
| 104 | + it('takes the name once the author types past the refusal', () => { |
| 105 | + renderWorkOrder(); |
| 106 | + typeOver('status_2'); |
| 107 | + expect(apiNameBox()).toHaveValue('status_2'); |
| 108 | + expect(screen.queryByTestId('field-apiname-refusal')).toBeNull(); |
| 109 | + expect(Object.keys(saved).sort()).toEqual(['status', 'status_2']); |
| 110 | + }); |
| 111 | + |
| 112 | + it('shows the name the field carries once the author leaves a refused box', () => { |
| 113 | + renderWorkOrder(); |
| 114 | + typeOver('status'); |
| 115 | + fireEvent.blur(apiNameBox()); |
| 116 | + expect(apiNameBox()).toHaveValue('estimated_hours'); |
| 117 | + expect(screen.queryByTestId('field-apiname-refusal')).toBeNull(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('saves nothing for an emptied box', () => { |
| 121 | + renderWorkOrder(); |
| 122 | + fireEvent.focus(apiNameBox()); |
| 123 | + fireEvent.change(apiNameBox(), { target: { value: '' } }); |
| 124 | + expect(apiNameBox()).toHaveValue(''); |
| 125 | + expect(Object.keys(saved).sort()).toEqual(['estimated_hours', 'status']); |
| 126 | + fireEvent.blur(apiNameBox()); |
| 127 | + expect(apiNameBox()).toHaveValue('estimated_hours'); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('ObjectFieldInspector — API name box normalises as the new-object dialog does (#27517)', () => { |
| 132 | + it('saves `Estimated Hours!` as estimated_hours, without the trailing underscore', () => { |
| 133 | + saved = {}; |
| 134 | + render(<Host fields={{ field_3: { type: 'number', label: '' } }} id="field_3" />); |
| 135 | + typeOver('Estimated Hours!'); |
| 136 | + // While typing, the box keeps the `_` so the next letter can follow it. |
| 137 | + expect(apiNameBox()).toHaveValue('estimated_hours_'); |
| 138 | + // Pre-fix this read `estimated_hours_`. |
| 139 | + expect(Object.keys(saved)).toEqual(['estimated_hours']); |
| 140 | + fireEvent.blur(apiNameBox()); |
| 141 | + expect(apiNameBox()).toHaveValue('estimated_hours'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('still lets an underscore be typed mid-word', () => { |
| 145 | + saved = {}; |
| 146 | + render(<Host fields={{ field_4: { type: 'text', label: '' } }} id="field_4" />); |
| 147 | + typeOver('repair_ticket'); |
| 148 | + expect(apiNameBox()).toHaveValue('repair_ticket'); |
| 149 | + expect(Object.keys(saved)).toEqual(['repair_ticket']); |
| 150 | + }); |
| 151 | +}); |
0 commit comments