|
| 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 quick-picker's subtitle never repeats the field the option's title reads. |
| 11 | + * |
| 12 | + * A lookup that declares no `displayField` titles each option through the |
| 13 | + * referenced object's own name field (`getRecordDisplayName`), while the |
| 14 | + * subtitle skipped only the widget's `'name'` guess. On an object whose name |
| 15 | + * field is anything else — `title`, `customer_name`, every AI-built object — |
| 16 | + * the subtitle's first, widest slot repeated the title and pushed the field |
| 17 | + * that tells two records apart (the phone number) into truncation. |
| 18 | + * |
| 19 | + * The subtitle now skips the field the title resolved from: the declared |
| 20 | + * `displayField`, else the object's resolved name field — unless a |
| 21 | + * `titleFormat` template, not a field, titles the record. |
| 22 | + */ |
| 23 | + |
| 24 | +import * as React from 'react'; |
| 25 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 26 | +import { render, screen, cleanup, waitFor, act, fireEvent } from '@testing-library/react'; |
| 27 | +import '@testing-library/jest-dom'; |
| 28 | +import { LookupField } from './LookupField'; |
| 29 | + |
| 30 | +const FIELDS: Record<string, { type: string; label: string }> = { |
| 31 | + title: { type: 'text', label: 'Customer Name' }, |
| 32 | + phone: { type: 'phone', label: 'Phone' }, |
| 33 | + source: { type: 'text', label: 'Source' }, |
| 34 | + code: { type: 'text', label: 'Code' }, |
| 35 | +}; |
| 36 | + |
| 37 | +const ROWS: Record<string, unknown>[] = [ |
| 38 | + { id: 'c1', title: 'Wang Jing', phone: '18624570001', source: 'Store', code: 'C-001' }, |
| 39 | + { id: 'c2', title: 'Zhou Mingxuan', phone: '18624570002', source: 'Web', code: 'C-002' }, |
| 40 | +]; |
| 41 | + |
| 42 | +function makeBackend(decl: Record<string, unknown>) { |
| 43 | + return { |
| 44 | + find: vi.fn(async () => ({ data: ROWS.map((r) => ({ ...r })), total: ROWS.length })), |
| 45 | + findOne: vi.fn(async (_o: string, id: string) => ROWS.find((r) => r.id === id) ?? null), |
| 46 | + getObjectSchema: vi.fn(async (objectName: string) => ({ name: objectName, fields: FIELDS, ...decl })), |
| 47 | + } as any; |
| 48 | +} |
| 49 | + |
| 50 | +async function settle(): Promise<void> { |
| 51 | + for (let i = 0; i < 6; i++) { |
| 52 | + await act(async () => { |
| 53 | + await new Promise((r) => setTimeout(r, 0)); |
| 54 | + }); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Open the dropdown and read, per option, its title and the fields its |
| 60 | + * subtitle previews, in order. |
| 61 | + */ |
| 62 | +async function openPicker( |
| 63 | + decl: Record<string, unknown>, |
| 64 | + field: Record<string, unknown> = {}, |
| 65 | +): Promise<Array<{ title: string; preview: string[] }>> { |
| 66 | + const ds = makeBackend(decl); |
| 67 | + render( |
| 68 | + <LookupField |
| 69 | + value={undefined} |
| 70 | + onChange={() => {}} |
| 71 | + dataSource={ds} |
| 72 | + field={{ reference: 'customer', ...field } as never} |
| 73 | + />, |
| 74 | + ); |
| 75 | + await waitFor(() => expect(ds.getObjectSchema).toHaveBeenCalledWith('customer')); |
| 76 | + await settle(); |
| 77 | + await act(async () => { |
| 78 | + fireEvent.click(screen.getByTestId('lookup-trigger')); |
| 79 | + }); |
| 80 | + await waitFor(() => expect(screen.getAllByRole('option')).toHaveLength(2)); |
| 81 | + await settle(); |
| 82 | + return screen.getAllByRole('option').map((el) => ({ |
| 83 | + title: el.getAttribute('title') ?? '', |
| 84 | + preview: Array.from(el.querySelectorAll<HTMLElement>('[data-lookup-preview]')).map( |
| 85 | + (v) => v.dataset.lookupPreview ?? '', |
| 86 | + ), |
| 87 | + })); |
| 88 | +} |
| 89 | + |
| 90 | +afterEach(() => { |
| 91 | + cleanup(); |
| 92 | + try { |
| 93 | + localStorage.clear(); |
| 94 | + } catch { |
| 95 | + /* no storage in this environment */ |
| 96 | + } |
| 97 | +}); |
| 98 | + |
| 99 | +describe('LookupField subtitle skips the field the title reads', () => { |
| 100 | + it("the object declares nameField 'title': the subtitle starts with the phone, not the title again", async () => { |
| 101 | + const options = await openPicker({ nameField: 'title' }); |
| 102 | + expect(options.map((o) => o.title)).toEqual(['Wang Jing', 'Zhou Mingxuan']); |
| 103 | + for (const o of options) expect(o.preview).toEqual(['phone', 'source']); |
| 104 | + }); |
| 105 | + |
| 106 | + it("no nameField declared: the derived name field 'title' is skipped the same way", async () => { |
| 107 | + const options = await openPicker({}); |
| 108 | + expect(options.map((o) => o.title)).toEqual(['Wang Jing', 'Zhou Mingxuan']); |
| 109 | + for (const o of options) expect(o.preview).toEqual(['phone', 'source']); |
| 110 | + }); |
| 111 | + |
| 112 | + it('an explicitly authored descriptionField still leads the subtitle', async () => { |
| 113 | + const options = await openPicker({ nameField: 'title' }, { descriptionField: 'source' }); |
| 114 | + for (const o of options) expect(o.preview).toEqual(['source', 'phone']); |
| 115 | + }); |
| 116 | + |
| 117 | + it("control: a declared displayField titles the option, so the object's name field stays in the subtitle", async () => { |
| 118 | + const options = await openPicker({ nameField: 'title' }, { displayField: 'code' }); |
| 119 | + expect(options.map((o) => o.title)).toEqual(['C-001', 'C-002']); |
| 120 | + for (const o of options) expect(o.preview).toEqual(['title', 'phone', 'source']); |
| 121 | + }); |
| 122 | + |
| 123 | + it('control: a titleFormat template titles the record, so the derived field stays in the subtitle', async () => { |
| 124 | + const options = await openPicker({ titleFormat: '{code} / {source}' }); |
| 125 | + expect(options.map((o) => o.title)).toEqual(['C-001 / Store', 'C-002 / Web']); |
| 126 | + for (const o of options) expect(o.preview).toEqual(['title', 'phone', 'source']); |
| 127 | + }); |
| 128 | +}); |
0 commit comments