|
| 1 | +/** |
| 2 | + * objectstack#36707 — the related tab's derived 「+ 添加」 on a `lookup` |
| 3 | + * relationship (`derivedAddOf`, the lookup arm): |
| 4 | + * |
| 5 | + * 1. the relationship lookup's `dependsOn` scopes the picker from the parent |
| 6 | + * record. An opportunity's 合同 tab: `contract.opportunity` `dependsOn` |
| 7 | + * the contract's account, so the picker lists only unlinked contracts of |
| 8 | + * the opportunity's own account — never another customer's, which a pick |
| 9 | + * would have linked here. A scope it cannot resolve offers no picker; |
| 10 | + * 2. the picker's label column is the picked object's name field, not a |
| 11 | + * hard-coded `name` (an empty 「NAME」 column on contracts, whose name |
| 12 | + * field is `contract_number`). |
| 13 | + */ |
| 14 | +import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest'; |
| 15 | +import { render, screen, waitFor, fireEvent, within } from '@testing-library/react'; |
| 16 | +import '@testing-library/jest-dom'; |
| 17 | +import React from 'react'; |
| 18 | +import { FilterScopeProvider, RecordContextProvider } from '@object-ui/react'; |
| 19 | +import { ComponentRegistry } from '@object-ui/core'; |
| 20 | +import { RelatedList, derivedAddOf } from '../RelatedList'; |
| 21 | +import { resetRelatedTabConditionsForTests } from '../relatedTabConditions'; |
| 22 | +import { RelatedListEnlargedContext } from '../relatedListCompact'; |
| 23 | + |
| 24 | +afterEach(() => resetRelatedTabConditionsForTests()); |
| 25 | + |
| 26 | +/** `object-grid` lives in `plugin-grid`, which this package cannot import. */ |
| 27 | +function ObjectGridStandIn({ schema }: { schema: any }) { |
| 28 | + const rows: any[] = schema?.data?.items ?? []; |
| 29 | + return <ul data-object-grid-stand-in="">{rows.map((row) => <li key={row.id}>{String(row.subject ?? row.contract_number ?? row.id)}</li>)}</ul>; |
| 30 | +} |
| 31 | + |
| 32 | +beforeAll(() => { |
| 33 | + Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 }); |
| 34 | + ComponentRegistry.register('object-grid', ObjectGridStandIn as never); |
| 35 | +}); |
| 36 | + |
| 37 | +const CONTRACT_FIELDS = { |
| 38 | + contract_number: { type: 'autonumber', label: 'Contract Number' }, |
| 39 | + account: { type: 'master_detail', reference: 'account', label: 'Account', required: true }, |
| 40 | + opportunity: { type: 'lookup', reference: 'opportunity', label: 'Opportunity', dependsOn: ['account'] }, |
| 41 | + status: { type: 'text', label: 'Status' }, |
| 42 | +}; |
| 43 | +const CONTRACT_SCHEMA = { name: 'contract', nameField: 'contract_number', fields: CONTRACT_FIELDS }; |
| 44 | + |
| 45 | +/** The unlinked contracts of two accounts; the stand-in server honours equality on `account`. */ |
| 46 | +const POOL = [ |
| 47 | + { id: 'k-own', contract_number: 'CTR-0003', account: 'A-1', opportunity: null, status: 'Draft' }, |
| 48 | + { id: 'k-other', contract_number: 'CTR-0004', account: 'A-2', opportunity: null, status: 'Draft' }, |
| 49 | +]; |
| 50 | +const LINKED = [{ id: 'k-here', contract_number: 'CTR-0001', account: 'A-1', opportunity: 'O-1', status: 'Active' }]; |
| 51 | + |
| 52 | +function dataSourceFor(schema: Record<string, unknown> = CONTRACT_SCHEMA) { |
| 53 | + return { |
| 54 | + find: vi.fn(async (resource: string, params?: any) => { |
| 55 | + const filter = JSON.stringify(params?.$filter ?? []); |
| 56 | + if (resource === 'contract' && JSON.stringify(params ?? {}).includes('O-1')) return { data: LINKED, total: LINKED.length }; |
| 57 | + const data = POOL.filter((r) => !filter.includes('"account"') || filter.includes(`"${r.account}"`)); |
| 58 | + return { data, total: data.length }; |
| 59 | + }), |
| 60 | + getObjectSchema: vi.fn(async () => schema), |
| 61 | + create: vi.fn(async () => ({})), |
| 62 | + update: vi.fn(async () => ({})), |
| 63 | + batchTransaction: vi.fn(async (ops: unknown[]) => ({ results: ops.map(() => ({})) })), |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function renderTab(parent: Record<string, unknown>, { onNew = vi.fn(), schema }: { onNew?: () => void; schema?: Record<string, unknown> } = {}) { |
| 68 | + const dataSource = dataSourceFor(schema); |
| 69 | + render( |
| 70 | + <FilterScopeProvider currentUserId="u-1"> |
| 71 | + <RecordContextProvider objectName="opportunity" recordId="O-1" data={parent} dataSource={dataSource as any}> |
| 72 | + <RelatedListEnlargedContext.Provider value> |
| 73 | + <RelatedList |
| 74 | + title="Contracts" |
| 75 | + type="table" |
| 76 | + api="contract" |
| 77 | + objectName="contract" |
| 78 | + referenceField="opportunity" |
| 79 | + parentId="O-1" |
| 80 | + pageSize={25} |
| 81 | + columns={[{ field: 'contract_number', label: 'Contract Number' }]} |
| 82 | + onNew={onNew} |
| 83 | + dataSource={dataSource as never} |
| 84 | + /> |
| 85 | + </RelatedListEnlargedContext.Provider> |
| 86 | + </RecordContextProvider> |
| 87 | + </FilterScopeProvider>, |
| 88 | + ); |
| 89 | + return { dataSource }; |
| 90 | +} |
| 91 | + |
| 92 | +/** The picker's column headers that carry a name, in order (the select-all column has none). */ |
| 93 | +const headerTexts = (dialog: HTMLElement) => |
| 94 | + Array.from(within(dialog).getByTestId('record-picker-sticky-header').querySelectorAll('th')) |
| 95 | + .map((th) => th.textContent?.trim() ?? '') |
| 96 | + .filter((text) => text !== ''); |
| 97 | +const addButton = () => document.querySelector('[data-related-enlarged-toolbar] [data-related-add]') as HTMLElement; |
| 98 | +const pickerReads = (ds: ReturnType<typeof dataSourceFor>) => |
| 99 | + ds.find.mock.calls.filter(([r, p]) => r === 'contract' && !JSON.stringify(p ?? {}).includes('O-1')); |
| 100 | + |
| 101 | +describe('derivedAddOf — a lookup relationship scoped by dependsOn (objectstack#36707)', () => { |
| 102 | + const unlinked = { field: 'opportunity', operator: 'is_empty' }; |
| 103 | + |
| 104 | + it('each dependsOn field must equal the parent\'s value: string and { field, param } entries', () => { |
| 105 | + expect(derivedAddOf(CONTRACT_FIELDS, 'opportunity', 'contract', { id: 'O-1', account: 'A-1' })).toEqual({ |
| 106 | + picker: { object: 'contract', filter: [unlinked, { field: 'account', operator: 'equals', value: 'A-1' }] }, |
| 107 | + }); |
| 108 | + const byParam = { ...CONTRACT_FIELDS, opportunity: { type: 'lookup', reference: 'opportunity', dependsOn: [{ field: 'account', param: 'customer' }] } }; |
| 109 | + expect(derivedAddOf(byParam, 'opportunity', 'contract', { id: 'O-1', customer: 'A-9', account: 'A-1' })?.picker.filter).toEqual([ |
| 110 | + unlinked, |
| 111 | + { field: 'account', operator: 'equals', value: 'A-9' }, |
| 112 | + ]); |
| 113 | + }); |
| 114 | + |
| 115 | + it('an expanded reference on the parent reads as its id', () => { |
| 116 | + expect(derivedAddOf(CONTRACT_FIELDS, 'opportunity', 'contract', { id: 'O-1', account: { id: 'A-1', name: 'Initech' } })?.picker.filter).toEqual([ |
| 117 | + unlinked, |
| 118 | + { field: 'account', operator: 'equals', value: 'A-1' }, |
| 119 | + ]); |
| 120 | + }); |
| 121 | + |
| 122 | + it.each([ |
| 123 | + ['the parent has no value for it', { id: 'O-1', account: null }], |
| 124 | + ['the parent record is not read', undefined], |
| 125 | + ] as const)('no picker when the scope cannot be resolved: %s', (_label, parent) => { |
| 126 | + expect(derivedAddOf(CONTRACT_FIELDS, 'opportunity', 'contract', parent as any)).toBeUndefined(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('no picker when a lookupFilters comparand on the relationship reads $record', () => { |
| 130 | + const fields = { |
| 131 | + ...CONTRACT_FIELDS, |
| 132 | + opportunity: { type: 'lookup', reference: 'opportunity', lookupFilters: [{ field: 'region', operator: 'eq', value: { $record: 'region' } }] }, |
| 133 | + }; |
| 134 | + expect(derivedAddOf(fields, 'opportunity', 'contract', { id: 'O-1', region: 'east' })).toBeUndefined(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('control: an unscoped lookup (static lookupFilters included) keeps the unlinked-only picker, parent read or not', () => { |
| 138 | + const fields = { |
| 139 | + ...CONTRACT_FIELDS, |
| 140 | + opportunity: { type: 'lookup', reference: 'opportunity', lookupFilters: [{ field: 'is_active', operator: 'eq', value: true }] }, |
| 141 | + }; |
| 142 | + expect(derivedAddOf(fields, 'opportunity', 'contract')).toEqual({ picker: { object: 'contract', filter: [unlinked] } }); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe('the opportunity\'s 合同 tab picker (objectstack#36707)', () => { |
| 147 | + it('lists only the account\'s own unlinked contracts, led by the contract number under its label', async () => { |
| 148 | + const { dataSource } = renderTab({ id: 'O-1', name: 'Initech Cloud Migration', account: 'A-1' }); |
| 149 | + await waitFor(() => expect(screen.getByText('CTR-0001')).toBeInTheDocument()); |
| 150 | + fireEvent.click(addButton()); |
| 151 | + const dialog = await screen.findByTestId('record-picker-dialog'); |
| 152 | + expect(await within(dialog).findByTestId('record-row-k-own')).toHaveTextContent('CTR-0003'); |
| 153 | + expect(within(dialog).queryByTestId('record-row-k-other')).toBeNull(); |
| 154 | + const reads = pickerReads(dataSource); |
| 155 | + expect(reads.length).toBeGreaterThan(0); |
| 156 | + for (const [, params] of reads) { |
| 157 | + const filter = JSON.stringify((params as any)?.$filter ?? []); |
| 158 | + expect(filter).toContain('"A-1"'); |
| 159 | + expect(filter).toContain('is_empty'); |
| 160 | + } |
| 161 | + const headers = headerTexts(dialog); |
| 162 | + expect(headers[0]).toBe('Contract Number'); |
| 163 | + expect(headers).not.toContain('Name'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('a parent with no account offers no picker: 「+ 添加」 creates a contract', async () => { |
| 167 | + const onNew = vi.fn(); |
| 168 | + const { dataSource } = renderTab({ id: 'O-1', name: 'Initech Cloud Migration', account: null }, { onNew }); |
| 169 | + await waitFor(() => expect(screen.getByText('CTR-0001')).toBeInTheDocument()); |
| 170 | + fireEvent.click(addButton()); |
| 171 | + expect(onNew).toHaveBeenCalledTimes(1); |
| 172 | + expect(screen.queryByTestId('record-picker-dialog')).toBeNull(); |
| 173 | + expect(pickerReads(dataSource)).toHaveLength(0); |
| 174 | + }); |
| 175 | + |
| 176 | + it('with no declared nameField the label column is the derived title field, still never an empty 「NAME」', async () => { |
| 177 | + const { nameField: _drop, ...undeclared } = CONTRACT_SCHEMA; |
| 178 | + renderTab({ id: 'O-1', account: 'A-1' }, { schema: undeclared }); |
| 179 | + await waitFor(() => expect(screen.getByText('CTR-0001')).toBeInTheDocument()); |
| 180 | + fireEvent.click(addButton()); |
| 181 | + const dialog = await screen.findByTestId('record-picker-dialog'); |
| 182 | + await within(dialog).findByTestId('record-row-k-own'); |
| 183 | + expect(headerTexts(dialog)).not.toContain('Name'); |
| 184 | + }); |
| 185 | +}); |
0 commit comments