|
| 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 | + * The highlight strip's chip caption climbs the details body's label ladder |
| 11 | + * (objectstack#26417). |
| 12 | + * |
| 13 | + * The card's sighting: an AI-built object with no i18n bundle, whose record |
| 14 | + * drawer drew 「陈静 | 周明轩」 under the title with an EMPTY caption over each |
| 15 | + * value. The synthesized page hands `record:highlights` bare names, the |
| 16 | + * renderer turns each into `{ name }`, and the strip asked |
| 17 | + * `fieldLabel(objectName, name, field.label)`: with no bundle key and no |
| 18 | + * authored label that is `undefined`, a blank caption. |
| 19 | + * |
| 20 | + * The details body names the same fields through one ladder — bundle key, then |
| 21 | + * the entry's label (the authored one, else the label the OBJECT declares, |
| 22 | + * read with `pickLocalized`), then the field name (`withDeclaredLabel` in |
| 23 | + * `record-details.tsx`, `field.label || field.name` in `DetailSection`). The |
| 24 | + * strip now climbs that same ladder. Every leg reads a chip that is on screen |
| 25 | + * (its value is asserted first), so an empty caption cannot pass as a strip |
| 26 | + * that drew nothing. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from 'vitest'; |
| 30 | +import { render, screen, cleanup, within } from '@testing-library/react'; |
| 31 | +import '@testing-library/jest-dom'; |
| 32 | +import * as React from 'react'; |
| 33 | +import { HighlightFieldsProvider, RecordContextProvider } from '@object-ui/react'; |
| 34 | +import { I18nProvider, createI18n } from '@object-ui/i18n'; |
| 35 | +import { HeaderHighlight } from '../../HeaderHighlight'; |
| 36 | +import { RecordDetailsRenderer } from '../record-details'; |
| 37 | +import { RecordHighlightsRenderer } from '../record-highlights'; |
| 38 | + |
| 39 | +const OBJECT = 'home_project'; |
| 40 | + |
| 41 | +/** An AI-built object: declared labels, and no bundle entry anywhere. */ |
| 42 | +const objectSchema = { |
| 43 | + name: OBJECT, |
| 44 | + label: '家装项目', |
| 45 | + fields: { |
| 46 | + owner: { type: 'text', label: '所有者' }, |
| 47 | + customer: { type: 'text', label: '关联客户' }, |
| 48 | + // An inline locale map, the other spelling a declared label may take. |
| 49 | + stage: { type: 'text', label: { en: 'Stage', 'zh-CN': '阶段' } }, |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +const record = { |
| 54 | + id: 'P1', |
| 55 | + owner: '陈静', |
| 56 | + customer: '周明轩', |
| 57 | + stage: 'design-stage', |
| 58 | + notes: 'notes-value', |
| 59 | +}; |
| 60 | + |
| 61 | +type Provider = { language: string } | { instance: ReturnType<typeof createI18n> }; |
| 62 | + |
| 63 | +const withI18n = (provider: Provider, node: React.ReactNode) => ( |
| 64 | + <I18nProvider |
| 65 | + {...('instance' in provider |
| 66 | + ? { instance: provider.instance } |
| 67 | + : { config: { defaultLanguage: provider.language, detectBrowserLanguage: false } })} |
| 68 | + persistLanguage={false} |
| 69 | + > |
| 70 | + {node} |
| 71 | + </I18nProvider> |
| 72 | +); |
| 73 | + |
| 74 | +/** The record page's strip, from the bare names the synthesized page hands it. */ |
| 75 | +function mountStrip(fields: unknown[], provider: Provider = { language: 'zh' }) { |
| 76 | + return render( |
| 77 | + withI18n( |
| 78 | + provider, |
| 79 | + <RecordContextProvider |
| 80 | + objectName={OBJECT} |
| 81 | + recordId={record.id} |
| 82 | + data={record} |
| 83 | + objectSchema={objectSchema as never} |
| 84 | + > |
| 85 | + <HighlightFieldsProvider> |
| 86 | + <div data-testid="strip"> |
| 87 | + <RecordHighlightsRenderer schema={{ fields } as never} /> |
| 88 | + </div> |
| 89 | + </HighlightFieldsProvider> |
| 90 | + </RecordContextProvider>, |
| 91 | + ), |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +/** The caption over the chip that shows `value` — the chip's first span. */ |
| 96 | +function captionOf(value: string): string | null { |
| 97 | + const strip = within(screen.getByTestId('strip').querySelector('section') as HTMLElement); |
| 98 | + const chip = strip.getByText(value).closest('section > div > div') as HTMLElement | null; |
| 99 | + if (!chip) throw new Error(`HARNESS DEAD: no chip holds ${value}`); |
| 100 | + return chip.firstElementChild?.textContent ?? null; |
| 101 | +} |
| 102 | + |
| 103 | +beforeAll(() => Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 })); |
| 104 | + |
| 105 | +beforeEach(() => { |
| 106 | + // `useRecordEditable` probes `POST /api/v1/security/explain`; a double keeps |
| 107 | + // the socket closed. Its answer is orthogonal to the captions. |
| 108 | + vi.stubGlobal('fetch', vi.fn(async () => ({ |
| 109 | + ok: true, |
| 110 | + status: 200, |
| 111 | + json: async () => ({ allowed: true }), |
| 112 | + text: async () => '{"allowed":true}', |
| 113 | + })) as never); |
| 114 | +}); |
| 115 | + |
| 116 | +afterEach(() => { |
| 117 | + cleanup(); |
| 118 | + vi.unstubAllGlobals(); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('a highlight chip with no bundle entry is captioned by its declared label (objectstack#26417)', () => { |
| 122 | + it('bare names on an object with no bundle: each chip reads the label the object declares', () => { |
| 123 | + mountStrip(['owner', 'customer']); |
| 124 | + expect(captionOf('陈静')).toBe('所有者'); |
| 125 | + expect(captionOf('周明轩')).toBe('关联客户'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('an inline locale map is read in the session language, never as an object', () => { |
| 129 | + mountStrip(['stage'], { language: 'zh' }); |
| 130 | + expect(captionOf('design-stage')).toBe('阶段'); |
| 131 | + cleanup(); |
| 132 | + mountStrip(['stage'], { language: 'en' }); |
| 133 | + expect(captionOf('design-stage')).toBe('Stage'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('a field the object declares no label for falls back to its name, never a blank caption', () => { |
| 137 | + mountStrip(['notes']); |
| 138 | + expect(captionOf('notes-value')).toBe('notes'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('a translated label still wins over the declared one', () => { |
| 142 | + const instance = createI18n({ |
| 143 | + defaultLanguage: 'zh', |
| 144 | + detectBrowserLanguage: false, |
| 145 | + resources: { zh: { app: { fields: { [OBJECT]: { owner: '负责人' } } } } }, |
| 146 | + }); |
| 147 | + mountStrip(['owner', 'customer'], { instance }); |
| 148 | + expect(captionOf('陈静')).toBe('负责人'); |
| 149 | + // The bundle names one field; the other still climbs to its declared label. |
| 150 | + expect(captionOf('周明轩')).toBe('关联客户'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('an authored label on the entry still wins over the declared one', () => { |
| 154 | + mountStrip([{ name: 'owner', label: 'Account Owner' }, 'customer']); |
| 155 | + expect(captionOf('陈静')).toBe('Account Owner'); |
| 156 | + expect(captionOf('周明轩')).toBe('关联客户'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('the strip and the details body name the same field the same way', async () => { |
| 160 | + // `customer` and `stage`, not `owner`: with no name field declared, the body |
| 161 | + // drops the first text field as the record's title (its own dedupe rule). |
| 162 | + mountStrip(['customer', 'stage']); |
| 163 | + const stripCaptions = [captionOf('周明轩'), captionOf('design-stage')]; |
| 164 | + expect(stripCaptions).toEqual(['关联客户', '阶段']); |
| 165 | + cleanup(); |
| 166 | + render( |
| 167 | + withI18n( |
| 168 | + { language: 'zh' }, |
| 169 | + <RecordContextProvider |
| 170 | + objectName={OBJECT} |
| 171 | + recordId={record.id} |
| 172 | + data={record} |
| 173 | + objectSchema={objectSchema as never} |
| 174 | + > |
| 175 | + <div data-testid="body"> |
| 176 | + <RecordDetailsRenderer schema={{ sections: [{ label: 'Main', fields: ['customer', 'stage'] }] } as never} /> |
| 177 | + </div> |
| 178 | + </RecordContextProvider>, |
| 179 | + ), |
| 180 | + ); |
| 181 | + const body = within(screen.getByTestId('body')); |
| 182 | + // Live control: the body drew the record's values. |
| 183 | + expect(await body.findByText('周明轩')).toBeInTheDocument(); |
| 184 | + expect(body.getByText('design-stage')).toBeInTheDocument(); |
| 185 | + for (const caption of stripCaptions) expect(body.getByText(caption as string)).toBeInTheDocument(); |
| 186 | + }); |
| 187 | +}); |
| 188 | + |
| 189 | +describe('the caption follows an object definition that arrives after the record (objectstack#26417)', () => { |
| 190 | + it('names the field until the definition lands, then reads its declared label', () => { |
| 191 | + const strip = (schema: unknown) => |
| 192 | + withI18n( |
| 193 | + { language: 'zh' }, |
| 194 | + <div data-testid="strip"> |
| 195 | + <HeaderHighlight |
| 196 | + fields={[{ name: 'owner' }] as never} |
| 197 | + data={record} |
| 198 | + objectName={OBJECT} |
| 199 | + objectSchema={schema} |
| 200 | + /> |
| 201 | + </div>, |
| 202 | + ); |
| 203 | + const { rerender } = render(strip(undefined)); |
| 204 | + expect(captionOf('陈静')).toBe('owner'); |
| 205 | + rerender(strip(objectSchema)); |
| 206 | + expect(captionOf('陈静')).toBe('所有者'); |
| 207 | + }); |
| 208 | +}); |
0 commit comments