|
| 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 | + * objectstack#38895 — on a phone, a record page's related tab draws each row |
| 11 | + * as the list card (objectstack#28484), and the card's BODY follows the |
| 12 | + * RELATIONSHIP's list, not the child object's `highlightFields` |
| 13 | + * (objectstack#37891, which stays for every other list): the relationship's |
| 14 | + * `relatedListColumns` when it declares them, else the child's |
| 15 | + * `highlightFields` — the override chain of ADR-0035 §3/§4 the tab's table |
| 16 | + * already follows. The lookup back to the page's own record is never a part: |
| 17 | + * it is the same value on every card, and the list never expands it, so it |
| 18 | + * could only draw a raw id. |
| 19 | + * |
| 20 | + * Driven through the real chain the tab draws: `RelatedList` (enlarged, a |
| 21 | + * list of the parent record) → `object-grid` → its card renderer. The fixture |
| 22 | + * is HotCRM's opportunity line on a product's page: `highlightFields` leads |
| 23 | + * with the product (right for the line's own page and the deal's list), while |
| 24 | + * the product relationship's `relatedListColumns` leads with the deal. |
| 25 | + * |
| 26 | + * ── Directions on the base tree (measured by reverting the fix) ────────── |
| 27 | + * declared columns: the deal is a part, by its name RED |
| 28 | + * declared columns: no part is the product's id RED |
| 29 | + * no declared columns: highlightFields minus the product RED |
| 30 | + * control: the tab's table at a desktop width keeps its columns GREEN |
| 31 | + */ |
| 32 | +import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest'; |
| 33 | +import { render, cleanup, waitFor, within } from '@testing-library/react'; |
| 34 | +import '@testing-library/jest-dom'; |
| 35 | +import React from 'react'; |
| 36 | + |
| 37 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 38 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 39 | + return { |
| 40 | + ...actual, |
| 41 | + usePermissions: () => ({ isLoaded: false, checkField: () => true, getObjectApiOperations: () => undefined, can: () => true }), |
| 42 | + }; |
| 43 | +}); |
| 44 | + |
| 45 | +// Registers `object-grid`, the enlarged related tab's face. |
| 46 | +import '../index'; |
| 47 | +import { registerAllFields } from '@object-ui/fields'; |
| 48 | +import { LocalizationProvider } from '@object-ui/i18n'; |
| 49 | +import { ActionProvider, FilterScopeProvider, SchemaRendererProvider } from '@object-ui/react'; |
| 50 | +import { RelatedList } from '../../../plugin-detail/src/RelatedList'; |
| 51 | +import { RelatedListEnlargedContext } from '../../../plugin-detail/src/relatedListCompact'; |
| 52 | +import { resetRelatedTabConditionsForTests } from '../../../plugin-detail/src/relatedTabConditions'; |
| 53 | + |
| 54 | +registerAllFields(); |
| 55 | + |
| 56 | +const ORIGINAL_INNER_WIDTH = window.innerWidth; |
| 57 | +function setWidth(px: number) { |
| 58 | + Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: px }); |
| 59 | +} |
| 60 | + |
| 61 | +beforeAll(() => { |
| 62 | + if (!Element.prototype.scrollIntoView) Element.prototype.scrollIntoView = vi.fn() as any; |
| 63 | +}); |
| 64 | + |
| 65 | +afterEach(() => { |
| 66 | + cleanup(); |
| 67 | + setWidth(ORIGINAL_INNER_WIDTH); |
| 68 | + resetRelatedTabConditionsForTests(); |
| 69 | +}); |
| 70 | + |
| 71 | +const LINE_OBJECT = 'crm_opportunity_line_item_38895'; |
| 72 | +const LINE_FIELDS = { |
| 73 | + id: { type: 'text' }, |
| 74 | + product_title: { type: 'text', label: 'Product Title' }, |
| 75 | + crm_product: { type: 'lookup', reference: 'crm_product', label: 'Product' }, |
| 76 | + crm_opportunity: { type: 'lookup', reference: 'crm_opportunity', label: 'Opportunity' }, |
| 77 | + quantity: { type: 'number', label: 'Quantity', scale: 0 }, |
| 78 | + unit_price: { type: 'currency', label: 'Sales Price' }, |
| 79 | + total_price: { |
| 80 | + type: 'formula', |
| 81 | + label: 'Total', |
| 82 | + returnType: 'currency', |
| 83 | + expression: 'record.quantity * record.unit_price', |
| 84 | + }, |
| 85 | +}; |
| 86 | +/** HotCRM's line: the product leads, for the line's own page and the deal's list. */ |
| 87 | +const HIGHLIGHTS = ['crm_product', 'quantity', 'unit_price', 'total_price']; |
| 88 | +/** HotCRM's `relatedListColumns` on the line's product relationship: the deal leads. */ |
| 89 | +const PRODUCT_RELATED_COLUMNS = ['crm_opportunity', 'quantity', 'unit_price', 'total_price']; |
| 90 | + |
| 91 | +const PRODUCT_ID = 'TcoGX1WJ77TN-SKX'; |
| 92 | +const DEALS: Record<string, { id: string; name: string }> = { |
| 93 | + o1: { id: 'o1', name: 'Globex Line Expansion' }, |
| 94 | + o2: { id: 'o2', name: 'Lattice Student Success Platform' }, |
| 95 | +}; |
| 96 | +const LINES = [ |
| 97 | + { id: 'l1', product_title: 'AI Agent Seat (Annual)', crm_product: PRODUCT_ID, crm_opportunity: 'o1', quantity: 8, unit_price: 1000, total_price: 8000 }, |
| 98 | + { id: 'l2', product_title: 'AI Agent Seat (Annual)', crm_product: PRODUCT_ID, crm_opportunity: 'o2', quantity: 5, unit_price: 1000, total_price: 5000 }, |
| 99 | +]; |
| 100 | + |
| 101 | +/** |
| 102 | + * A backend that honours `$expand`, as the ObjectStack read path does. It |
| 103 | + * holds the lines alone, so a lookup the list did not expand cannot be |
| 104 | + * resolved by a second read and draws whatever the row carries. |
| 105 | + */ |
| 106 | +function makeDataSource() { |
| 107 | + return { |
| 108 | + find: vi.fn(async (object: string, params: { $expand?: string[] } = {}) => { |
| 109 | + if (object !== LINE_OBJECT) return { data: [], total: 0 }; |
| 110 | + const expand = new Set(params.$expand ?? []); |
| 111 | + return { |
| 112 | + data: LINES.map((line) => ({ |
| 113 | + ...line, |
| 114 | + ...(expand.has('crm_opportunity') ? { crm_opportunity: DEALS[line.crm_opportunity] } : {}), |
| 115 | + ...(expand.has('crm_product') ? { crm_product: { id: PRODUCT_ID, name: 'AI Agent Seat (Annual)' } } : {}), |
| 116 | + })), |
| 117 | + total: LINES.length, |
| 118 | + }; |
| 119 | + }), |
| 120 | + getObjectSchema: vi.fn(async (name: string) => ({ |
| 121 | + name, |
| 122 | + nameField: 'product_title', |
| 123 | + highlightFields: HIGHLIGHTS, |
| 124 | + fields: structuredClone(LINE_FIELDS), |
| 125 | + })), |
| 126 | + } as any; |
| 127 | +} |
| 128 | + |
| 129 | +/** The product page's 「Opportunity Line Items」 tab. */ |
| 130 | +async function renderProductLinesTab(opts: { width: number; columns?: string[] }) { |
| 131 | + setWidth(opts.width); |
| 132 | + const ds = makeDataSource(); |
| 133 | + const utils = render( |
| 134 | + <LocalizationProvider value={{ currency: 'USD', locale: 'en' }}> |
| 135 | + <ActionProvider> |
| 136 | + <SchemaRendererProvider dataSource={ds}> |
| 137 | + <FilterScopeProvider currentUserId="u-1"> |
| 138 | + <RelatedListEnlargedContext.Provider value> |
| 139 | + <RelatedList |
| 140 | + title="Opportunity Line Items" |
| 141 | + type="table" |
| 142 | + api={LINE_OBJECT} |
| 143 | + objectName={LINE_OBJECT} |
| 144 | + referenceField="crm_product" |
| 145 | + parentId={PRODUCT_ID} |
| 146 | + {...(opts.columns ? { columns: opts.columns } : {})} |
| 147 | + dataSource={ds} |
| 148 | + /> |
| 149 | + </RelatedListEnlargedContext.Provider> |
| 150 | + </FilterScopeProvider> |
| 151 | + </SchemaRendererProvider> |
| 152 | + </ActionProvider> |
| 153 | + </LocalizationProvider>, |
| 154 | + ); |
| 155 | + return { ...utils, ds }; |
| 156 | +} |
| 157 | + |
| 158 | +const cards = () => Array.from(document.querySelectorAll<HTMLElement>('[data-testid="grid-mobile-card"]')); |
| 159 | +const parts = (card: HTMLElement) => |
| 160 | + within(card).queryAllByTestId('grid-card-subtitle-part').map((p) => p.textContent?.trim()); |
| 161 | + |
| 162 | +async function phoneCards(columns?: string[]) { |
| 163 | + const g = await renderProductLinesTab({ width: 390, ...(columns ? { columns } : {}) }); |
| 164 | + await waitFor(() => expect(cards()).toHaveLength(2), { timeout: 5000 }); |
| 165 | + // The money cells read the field defs, so a dollar amount proves they landed. |
| 166 | + await waitFor(() => expect(document.body.textContent).toContain('$')); |
| 167 | + expect(document.querySelector('table'), 'the card layout, not the table').toBeNull(); |
| 168 | + return { ...g, first: cards()[0], second: cards()[1] }; |
| 169 | +} |
| 170 | + |
| 171 | +describe('the phone card of a related tab follows the relationship\'s list (objectstack#38895)', () => { |
| 172 | + it('with relatedListColumns declared, each card names its deal and its quantity, never the page\'s product', async () => { |
| 173 | + const { first, second, ds } = await phoneCards(PRODUCT_RELATED_COLUMNS); |
| 174 | + // The list expands the deal it lists and never the key back to the page. |
| 175 | + const lineReads = ds.find.mock.calls.filter((call: unknown[]) => call[0] === LINE_OBJECT); |
| 176 | + const params = lineReads.at(-1)?.[1] as { $expand?: string[] }; |
| 177 | + expect(params.$expand).toContain('crm_opportunity'); |
| 178 | + expect(params.$expand ?? []).not.toContain('crm_product'); |
| 179 | + |
| 180 | + expect(parts(first)).toEqual(['Globex Line Expansion', 'Quantity 8', 'Total $8,000.00']); |
| 181 | + expect(parts(second)).toEqual(['Lattice Student Success Platform', 'Quantity 5', 'Total $5,000.00']); |
| 182 | + // The amount is the first money field of the relationship's list. |
| 183 | + expect(within(first).getByTestId('grid-card-amount').textContent).toBe('$1,000.00'); |
| 184 | + for (const card of [first, second]) { |
| 185 | + expect(card.textContent, 'no raw id of the page\'s own record').not.toContain(PRODUCT_ID); |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + it('with no relatedListColumns, the card falls back to the child\'s highlightFields, minus the key back to the page', async () => { |
| 190 | + const { first, second } = await phoneCards(); |
| 191 | + expect(parts(first)).toEqual(['Quantity 8', 'Total $8,000.00']); |
| 192 | + expect(within(first).getByTestId('grid-card-amount').textContent).toBe('$1,000.00'); |
| 193 | + for (const card of [first, second]) { |
| 194 | + expect(card.textContent, 'no raw id of the page\'s own record').not.toContain(PRODUCT_ID); |
| 195 | + } |
| 196 | + }); |
| 197 | + |
| 198 | + it('CONTROL: the same tab at a desktop width is the table of the relationship\'s columns', async () => { |
| 199 | + await renderProductLinesTab({ width: 1440, columns: PRODUCT_RELATED_COLUMNS }); |
| 200 | + await waitFor(() => expect(document.querySelector('table')).not.toBeNull(), { timeout: 5000 }); |
| 201 | + await waitFor(() => expect(document.body.textContent).toContain('Globex Line Expansion')); |
| 202 | + expect(cards()).toHaveLength(0); |
| 203 | + const headers = Array.from(document.querySelectorAll('thead th')).map((th) => th.textContent?.trim() ?? ''); |
| 204 | + for (const label of ['Opportunity', 'Quantity', 'Sales Price', 'Total']) { |
| 205 | + expect(headers.some((h) => h.includes(label)), `a ${label} column`).toBe(true); |
| 206 | + } |
| 207 | + expect(headers.some((h) => h === 'Product'), 'no column for the key back to the page').toBe(false); |
| 208 | + }); |
| 209 | +}); |
0 commit comments