|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#27121 — the shared-record page reads the resolve answer's |
| 5 | + * `fields` projection (objectstack#26991): each row carries the field's |
| 6 | + * authored label, a picklist value its authored option label, a currency |
| 7 | + * value its currency through `resolveFieldCurrency`, and a lookup its stored |
| 8 | + * id. A key with no `fields` entry, and an answer from a server that predates |
| 9 | + * the projection, keep the humanized label and the JSON-shape value. |
| 10 | + * |
| 11 | + * The stub answers `/share-links/:token/resolve` the way both producers do: |
| 12 | + * enveloped, with the redacted fields already stripped from `record` and |
| 13 | + * `fields` covering exactly the keys `record` carries. Every visitor is |
| 14 | + * anonymous, and every request the page makes is recorded, so a renderer that |
| 15 | + * fetched the lookup target (or `/meta`) would show up here. |
| 16 | + */ |
| 17 | + |
| 18 | +import '@testing-library/jest-dom/vitest'; |
| 19 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 20 | +import { cleanup, render, screen, within } from '@testing-library/react'; |
| 21 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 22 | +import { formatCurrency } from '@object-ui/fields'; |
| 23 | +import { resolveFieldCurrency } from '@object-ui/i18n'; |
| 24 | +import { SchemaRendererProvider } from '@object-ui/react'; |
| 25 | +import SharedRecordPage from './SharedRecordPage'; |
| 26 | + |
| 27 | +const TOKEN = 'tok_27121'; |
| 28 | +const RESOLVE_PATH = `/api/v1/share-links/${TOKEN}/resolve`; |
| 29 | + |
| 30 | +/** An `account`-like record as resolve hands it over, `ssn` already redacted. */ |
| 31 | +const ACCOUNT = { |
| 32 | + id: 'acc_1', |
| 33 | + name: 'Acme Corporation', |
| 34 | + account_type: 'customer', |
| 35 | + regions: ['emea', 'apac'], |
| 36 | + annual_revenue: 1250000, |
| 37 | + pipeline_value: 480000, |
| 38 | + parent_id: 'acc_parent_9', |
| 39 | + founded_on: '2026-03-15', |
| 40 | +}; |
| 41 | + |
| 42 | +const FIXED_EUR = { currencyMode: 'fixed', defaultCurrency: 'EUR' } as const; |
| 43 | +const DYNAMIC = { currencyMode: 'dynamic', defaultCurrency: 'CNY' } as const; |
| 44 | + |
| 45 | +/** The projection the server builds for ACCOUNT — `founded_on` deliberately left out. */ |
| 46 | +const FIELDS = { |
| 47 | + id: { label: 'Record ID', type: 'text' }, |
| 48 | + name: { label: 'Account Name', type: 'text' }, |
| 49 | + account_type: { |
| 50 | + label: 'Relationship', |
| 51 | + type: 'select', |
| 52 | + options: [ |
| 53 | + { value: 'prospect', label: 'Prospect' }, |
| 54 | + { value: 'customer', label: 'Paying customer', color: 'green' }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + regions: { |
| 58 | + label: 'Sales Regions', |
| 59 | + type: 'multiselect', |
| 60 | + multiple: true, |
| 61 | + options: [ |
| 62 | + { value: 'emea', label: 'Europe & Africa' }, |
| 63 | + { value: 'apac', label: 'Asia Pacific' }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + annual_revenue: { label: 'Yearly Revenue', type: 'currency', currencyConfig: FIXED_EUR }, |
| 67 | + pipeline_value: { label: 'Open Pipeline', type: 'currency', currencyConfig: DYNAMIC }, |
| 68 | + parent_id: { label: 'Parent Account', type: 'lookup', reference: 'account', displayField: 'name' }, |
| 69 | +}; |
| 70 | + |
| 71 | +const json = (status: number, body: unknown) => |
| 72 | + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); |
| 73 | + |
| 74 | +/** Stub the resolve route; answer `fields` only when given. Returns every requested path. */ |
| 75 | +function shareServer(record: Record<string, unknown>, fields?: Record<string, unknown>): string[] { |
| 76 | + const requested: string[] = []; |
| 77 | + vi.stubGlobal( |
| 78 | + 'fetch', |
| 79 | + vi.fn(async (input: string | URL | Request) => { |
| 80 | + const raw = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; |
| 81 | + const { pathname } = new URL(raw, 'http://localhost'); |
| 82 | + requested.push(pathname); |
| 83 | + if (pathname === RESOLVE_PATH) { |
| 84 | + return json(200, { |
| 85 | + success: true, |
| 86 | + data: { |
| 87 | + record, |
| 88 | + link: { |
| 89 | + id: 'sl_1', |
| 90 | + token: TOKEN, |
| 91 | + object_name: 'account', |
| 92 | + record_id: String(record.id), |
| 93 | + permission: 'view', |
| 94 | + audience: 'anyone', |
| 95 | + }, |
| 96 | + redactFields: ['ssn'], |
| 97 | + ...(fields ? { fields } : {}), |
| 98 | + }, |
| 99 | + }); |
| 100 | + } |
| 101 | + return new Response(null, { status: 404 }); |
| 102 | + }), |
| 103 | + ); |
| 104 | + return requested; |
| 105 | +} |
| 106 | + |
| 107 | +function renderLink(dataSource?: Record<string, unknown>) { |
| 108 | + const routed = ( |
| 109 | + <MemoryRouter initialEntries={[`/s/${TOKEN}`]}> |
| 110 | + <Routes> |
| 111 | + <Route path="/s/:token" element={<SharedRecordPage />} /> |
| 112 | + </Routes> |
| 113 | + </MemoryRouter> |
| 114 | + ); |
| 115 | + return render( |
| 116 | + dataSource ? <SchemaRendererProvider dataSource={dataSource as never}>{routed}</SchemaRendererProvider> : routed, |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +/** The value cell beside the row whose label reads `label`. */ |
| 121 | +function cellOf(label: string): HTMLElement { |
| 122 | + const term = screen.getByText(label, { selector: 'dt' }); |
| 123 | + const value = term.parentElement?.querySelector('dd'); |
| 124 | + expect(value).not.toBeNull(); |
| 125 | + return value as HTMLElement; |
| 126 | +} |
| 127 | + |
| 128 | +afterEach(() => { |
| 129 | + cleanup(); |
| 130 | + vi.unstubAllGlobals(); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('SharedRecordPage — rows read the resolve answer’s fields projection (objectstack#27121)', () => { |
| 134 | + it('labels each row with the field’s authored label', async () => { |
| 135 | + shareServer(ACCOUNT, FIELDS); |
| 136 | + renderLink(); |
| 137 | + |
| 138 | + await screen.findByText('Shared account'); |
| 139 | + expect(cellOf('Account Name')).toHaveTextContent('Acme Corporation'); |
| 140 | + expect(cellOf('Yearly Revenue')).toBeInTheDocument(); |
| 141 | + expect(cellOf('Record ID')).toHaveTextContent('acc_1'); |
| 142 | + // The humanized key is gone wherever an authored label exists. |
| 143 | + expect(screen.queryByText('Name', { selector: 'dt' })).toBeNull(); |
| 144 | + expect(screen.queryByText('Annual Revenue', { selector: 'dt' })).toBeNull(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('a picklist value shows its authored option label and colour, one per value when multiple', async () => { |
| 148 | + shareServer(ACCOUNT, FIELDS); |
| 149 | + renderLink(); |
| 150 | + |
| 151 | + await screen.findByText('Shared account'); |
| 152 | + const relationship = cellOf('Relationship'); |
| 153 | + const badge = within(relationship).getByText('Paying customer'); |
| 154 | + // The option label, not the stored code. |
| 155 | + expect(relationship.textContent).toBe('Paying customer'); |
| 156 | + // The option's declared colour reaches the badge the detail view draws. |
| 157 | + expect(badge.closest('[class*="green"]')).not.toBeNull(); |
| 158 | + |
| 159 | + const regions = cellOf('Sales Regions'); |
| 160 | + expect(within(regions).getByText('Europe & Africa')).toBeInTheDocument(); |
| 161 | + expect(within(regions).getByText('Asia Pacific')).toBeInTheDocument(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('a currency value is formatted in the currency resolveFieldCurrency answers for its currencyConfig', async () => { |
| 165 | + shareServer(ACCOUNT, FIELDS); |
| 166 | + renderLink(); |
| 167 | + |
| 168 | + await screen.findByText('Shared account'); |
| 169 | + // Fixed mode: the declared code. |
| 170 | + const eur = resolveFieldCurrency({ currencyConfig: FIXED_EUR }); |
| 171 | + expect(eur).toBe('EUR'); |
| 172 | + expect(cellOf('Yearly Revenue')).toHaveTextContent(formatCurrency(1250000, eur, 'en')); |
| 173 | + expect(cellOf('Yearly Revenue').textContent).toContain('€'); |
| 174 | + |
| 175 | + // Dynamic mode with no tenant default on this page: whatever the shared |
| 176 | + // resolution answers today (no code), so a plain amount and never CNY. |
| 177 | + const dynamic = resolveFieldCurrency({ currencyConfig: DYNAMIC }); |
| 178 | + expect(dynamic).toBeUndefined(); |
| 179 | + expect(cellOf('Open Pipeline')).toHaveTextContent(formatCurrency(480000, dynamic, 'en')); |
| 180 | + expect(cellOf('Open Pipeline').textContent).not.toMatch(/CN¥|¥|CNY/); |
| 181 | + }); |
| 182 | + |
| 183 | + it('a lookup shows its stored id under its authored label, and nothing but resolve is fetched', async () => { |
| 184 | + const requested = shareServer(ACCOUNT, FIELDS); |
| 185 | + renderLink(); |
| 186 | + |
| 187 | + await screen.findByText('Shared account'); |
| 188 | + expect(cellOf('Parent Account').textContent).toBe('acc_parent_9'); |
| 189 | + // One request, to resolve: no lookup-name read, no /meta, no /data. |
| 190 | + expect(requested).toEqual([RESOLVE_PATH]); |
| 191 | + }); |
| 192 | + |
| 193 | + it('a lookup never reads its target, even under a host that holds a data source', async () => { |
| 194 | + // The `/s/:token` route mounts no data source today; this pins the page's |
| 195 | + // own guard, so hoisting one above the route cannot turn an anonymous |
| 196 | + // visit into a read of the referenced record. |
| 197 | + const dataSource = { |
| 198 | + find: vi.fn(async () => ({ data: [{ id: 'acc_parent_9', name: 'Globex' }] })), |
| 199 | + findOne: vi.fn(async () => ({ id: 'acc_parent_9', name: 'Globex' })), |
| 200 | + getObjectSchema: vi.fn(async () => ({ name: 'account', fields: {} })), |
| 201 | + }; |
| 202 | + const requested = shareServer(ACCOUNT, FIELDS); |
| 203 | + renderLink(dataSource); |
| 204 | + |
| 205 | + await screen.findByText('Shared account'); |
| 206 | + // Let any effect a renderer scheduled settle before reading the spies. |
| 207 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 208 | + expect(cellOf('Parent Account').textContent).toBe('acc_parent_9'); |
| 209 | + expect(dataSource.find).not.toHaveBeenCalled(); |
| 210 | + expect(dataSource.findOne).not.toHaveBeenCalled(); |
| 211 | + expect(dataSource.getObjectSchema).not.toHaveBeenCalled(); |
| 212 | + expect(requested).toEqual([RESOLVE_PATH]); |
| 213 | + }); |
| 214 | + |
| 215 | + it('a key with no fields entry keeps the humanized label and the JSON-shape value (control)', async () => { |
| 216 | + shareServer(ACCOUNT, FIELDS); |
| 217 | + renderLink(); |
| 218 | + |
| 219 | + await screen.findByText('Shared account'); |
| 220 | + const founded = cellOf('Founded On'); |
| 221 | + expect(founded.textContent).not.toBe('2026-03-15'); |
| 222 | + expect(founded.textContent).not.toBe(''); |
| 223 | + }); |
| 224 | + |
| 225 | + it('an answer without fields (an older server) renders exactly as before the projection', async () => { |
| 226 | + const requested = shareServer(ACCOUNT); |
| 227 | + const { container } = renderLink(); |
| 228 | + |
| 229 | + await screen.findByText('Shared account'); |
| 230 | + expect(cellOf('Name').textContent).toBe('Acme Corporation'); |
| 231 | + expect(cellOf('Account Type').textContent).toBe('customer'); |
| 232 | + expect(cellOf('Annual Revenue').textContent).toBe('1,250,000'); |
| 233 | + expect(cellOf('Parent Id').textContent).toBe('acc_parent_9'); |
| 234 | + expect(screen.queryByText('Account Name', { selector: 'dt' })).toBeNull(); |
| 235 | + expect(container.querySelectorAll('dt')).toHaveLength(Object.keys(ACCOUNT).length); |
| 236 | + expect(requested).toEqual([RESOLVE_PATH]); |
| 237 | + }); |
| 238 | +}); |
0 commit comments