|
| 1 | +/** |
| 2 | + * objectstack#27188 — a shared `record`-mode amount (ADR-0144 D2) shows the |
| 3 | + * currency its own record names: the resolve answer projects the amount's |
| 4 | + * `currencyField` pointer, and the page hands the shared record to the |
| 5 | + * currency face. A code field the link hides leaves a plain number. |
| 6 | + */ |
| 7 | +import '@testing-library/jest-dom/vitest'; |
| 8 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 9 | +import { cleanup, render, screen } from '@testing-library/react'; |
| 10 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 11 | +import { SchemaRendererProvider } from '@object-ui/react'; |
| 12 | +import SharedRecordPage from './SharedRecordPage'; |
| 13 | + |
| 14 | +const TOKEN = 'tok_27188'; |
| 15 | +const RESOLVE_PATH = `/api/v1/share-links/${TOKEN}/resolve`; |
| 16 | + |
| 17 | +const RECORD_MODE = { currencyMode: 'record', currencyField: 'currency_code' } as const; |
| 18 | + |
| 19 | +const DEAL = { id: 'deal_1', name: 'Shared deal', amount: 1250, currency_code: 'EUR' }; |
| 20 | +const FIELDS = { |
| 21 | + name: { label: 'Deal Name', type: 'text' }, |
| 22 | + amount: { label: 'Amount', type: 'currency', currencyConfig: RECORD_MODE }, |
| 23 | + currency_code: { label: 'Currency', type: 'text' }, |
| 24 | +}; |
| 25 | + |
| 26 | +const json = (status: number, body: unknown) => |
| 27 | + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); |
| 28 | + |
| 29 | +/** Stub the resolve route; answer `fields` only when given. Returns every requested path. */ |
| 30 | +function shareServer(record: Record<string, unknown>, fields?: Record<string, unknown>): string[] { |
| 31 | + const requested: string[] = []; |
| 32 | + vi.stubGlobal( |
| 33 | + 'fetch', |
| 34 | + vi.fn(async (input: string | URL | Request) => { |
| 35 | + const raw = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; |
| 36 | + const { pathname } = new URL(raw, 'http://localhost'); |
| 37 | + requested.push(pathname); |
| 38 | + if (pathname === RESOLVE_PATH) { |
| 39 | + return json(200, { |
| 40 | + success: true, |
| 41 | + data: { |
| 42 | + record, |
| 43 | + link: { |
| 44 | + id: 'sl_1', |
| 45 | + token: TOKEN, |
| 46 | + object_name: 'deal', |
| 47 | + record_id: String(record.id), |
| 48 | + permission: 'view', |
| 49 | + audience: 'anyone', |
| 50 | + }, |
| 51 | + redactFields: ['ssn'], |
| 52 | + ...(fields ? { fields } : {}), |
| 53 | + }, |
| 54 | + }); |
| 55 | + } |
| 56 | + return new Response(null, { status: 404 }); |
| 57 | + }), |
| 58 | + ); |
| 59 | + return requested; |
| 60 | +} |
| 61 | + |
| 62 | +function renderLink(dataSource?: Record<string, unknown>) { |
| 63 | + const routed = ( |
| 64 | + <MemoryRouter initialEntries={[`/s/${TOKEN}`]}> |
| 65 | + <Routes> |
| 66 | + <Route path="/s/:token" element={<SharedRecordPage />} /> |
| 67 | + </Routes> |
| 68 | + </MemoryRouter> |
| 69 | + ); |
| 70 | + return render( |
| 71 | + dataSource ? <SchemaRendererProvider dataSource={dataSource as never}>{routed}</SchemaRendererProvider> : routed, |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +/** The value cell beside the row whose label reads `label`. */ |
| 76 | +function cellOf(label: string): HTMLElement { |
| 77 | + const term = screen.getByText(label, { selector: 'dt' }); |
| 78 | + const value = term.parentElement?.querySelector('dd'); |
| 79 | + expect(value).not.toBeNull(); |
| 80 | + return value as HTMLElement; |
| 81 | +} |
| 82 | + |
| 83 | +afterEach(() => { |
| 84 | + cleanup(); |
| 85 | + vi.unstubAllGlobals(); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('shared record — a record-mode amount', () => { |
| 89 | + it('shows the currency the record\'s code field names', async () => { |
| 90 | + shareServer(DEAL, FIELDS); |
| 91 | + renderLink(); |
| 92 | + await screen.findByText('Amount', { selector: 'dt' }); |
| 93 | + expect(cellOf('Amount').textContent).toContain('€'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('follows each record\'s own code', async () => { |
| 97 | + shareServer({ ...DEAL, currency_code: 'JPY', amount: 1250 }, FIELDS); |
| 98 | + renderLink(); |
| 99 | + await screen.findByText('Amount', { selector: 'dt' }); |
| 100 | + expect(cellOf('Amount').textContent).toMatch(/¥|JP¥/); |
| 101 | + expect(cellOf('Amount').textContent).not.toContain('€'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('a hidden code field (no pointer, no code) leaves a plain number', async () => { |
| 105 | + const { currency_code: _hidden, ...record } = DEAL; |
| 106 | + shareServer(record, { ...FIELDS, amount: { ...FIELDS.amount, currencyConfig: { currencyMode: 'record' } } }); |
| 107 | + renderLink(); |
| 108 | + await screen.findByText('Amount', { selector: 'dt' }); |
| 109 | + expect(cellOf('Amount').textContent).not.toMatch(/€|\$|¥/); |
| 110 | + expect(cellOf('Amount').textContent).toMatch(/1,?250/); |
| 111 | + }); |
| 112 | +}); |
0 commit comments