|
| 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#27430 row 1: a formula's declared `returnType` reaches the two |
| 11 | + * remaining places this package hand-copies field metadata for its cells |
| 12 | + * (the record page's key list and the grid were objectstack#27494): |
| 13 | + * |
| 14 | + * - `RelatedList`'s `makeCell` bag; |
| 15 | + * - `buildRecordDetailFields` (`RecordDetailPanel`), the quick-look overlay. |
| 16 | + * |
| 17 | + * The formula cell picks its face from `returnType`, and a `currency` one |
| 18 | + * draws through the currency cell with the formula's own `currencyConfig`. |
| 19 | + * Both copies carried `currencyConfig` and `scale` but not `returnType`, so |
| 20 | + * a money formula read as a bare `90,000.00`. |
| 21 | + * |
| 22 | + * ── Directions on the base tree (predicted before the first run) ───────── |
| 23 | + * currency formula, related list and panel RED (`90,000.00`) |
| 24 | + * number formula control GREEN (the number face is right) |
| 25 | + * |
| 26 | + * The viewport is pinned to desktop: `RelatedList` swaps its table for a |
| 27 | + * gallery under `useIsMobile()`, and the gallery draws no table cells. |
| 28 | + */ |
| 29 | + |
| 30 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 31 | +import { render, cleanup, waitFor } from '@testing-library/react'; |
| 32 | +import '@testing-library/jest-dom'; |
| 33 | +import * as React from 'react'; |
| 34 | +import { FieldSchema } from '@objectstack/spec/data'; |
| 35 | + |
| 36 | +import '@object-ui/components'; |
| 37 | +import { RecordDetailPanel, buildRecordDetailFields } from '../RecordDetailPanel'; |
| 38 | +import { RelatedList } from '../RelatedList'; |
| 39 | + |
| 40 | +// `RecordDetailPanel` mounts `DetailView`, whose `useRecordEditable` probes |
| 41 | +// `POST /api/v1/security/explain`; one double at module scope, never torn down. |
| 42 | +vi.stubGlobal( |
| 43 | + 'fetch', |
| 44 | + vi.fn(async () => ({ ok: true, json: async () => ({ record: { visible: true } }) })), |
| 45 | +); |
| 46 | + |
| 47 | +beforeEach(() => { |
| 48 | + Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 }); |
| 49 | +}); |
| 50 | +afterEach(cleanup); |
| 51 | + |
| 52 | +const USD_FIXED = { currencyMode: 'fixed', defaultCurrency: 'USD' } as const; |
| 53 | + |
| 54 | +const FIELDS = { |
| 55 | + budget: { type: 'currency', label: 'Budget', currencyConfig: USD_FIXED }, |
| 56 | + // The showcase's Budget Remaining, as `project.object.ts` declares it. |
| 57 | + remaining: { type: 'formula', label: 'Budget Remaining', returnType: 'currency', currencyConfig: USD_FIXED, scale: 2 }, |
| 58 | + // The control: a declared number keeps the bare grouped number. |
| 59 | + ratio: { type: 'formula', label: 'Burn Ratio', returnType: 'number', scale: 2 }, |
| 60 | +}; |
| 61 | + |
| 62 | +const RECORD = { id: 'p1', budget: 150000, remaining: 90000, ratio: 1234.5 }; |
| 63 | + |
| 64 | +function expectMoneyFormula(text: string) { |
| 65 | + expect(text).toContain('$150,000.00'); |
| 66 | + expect(text, 'the currency formula draws as money').toContain('$90,000.00'); |
| 67 | + expect(text, 'CONTROL: a number formula keeps the bare grouped number').toContain('1,234.50'); |
| 68 | + expect(text).not.toContain('$1,234.50'); |
| 69 | +} |
| 70 | + |
| 71 | +describe('a formula returnType reaches the related list and the quick-look panel (objectstack#27430)', () => { |
| 72 | + it('CONTROL: the fixture is a spec-valid formula field def', () => { |
| 73 | + expect(FieldSchema.safeParse({ name: 'remaining', expression: 'record.budget', ...FIELDS.remaining }).success).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('buildRecordDetailFields carries returnType from the object field', () => { |
| 77 | + const built = buildRecordDetailFields(RECORD, { fields: FIELDS }, new Set(['id'])); |
| 78 | + expect(built.find((f) => f.name === 'remaining')?.returnType).toBe('currency'); |
| 79 | + expect(built.find((f) => f.name === 'ratio')?.returnType).toBe('number'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('RecordDetailPanel: the currency formula reads as money; a number formula stays bare', async () => { |
| 83 | + const { container } = render( |
| 84 | + <RecordDetailPanel record={RECORD} objectName="project" recordId="p1" objectSchema={{ fields: FIELDS }} />, |
| 85 | + ); |
| 86 | + await waitFor(() => expect(container.textContent).toContain('1,234.5')); |
| 87 | + expectMoneyFormula(container.textContent ?? ''); |
| 88 | + }); |
| 89 | + |
| 90 | + it('RelatedList: the currency formula cell reads as money; a number formula cell stays bare', async () => { |
| 91 | + const dataSource = { |
| 92 | + getObjectSchema: vi.fn(async () => ({ name: 'project', fields: { name: { type: 'text', label: 'Name' }, ...FIELDS } })), |
| 93 | + find: vi.fn(async () => ({ data: [{ ...RECORD, name: 'Tower' }], total: 1 })), |
| 94 | + }; |
| 95 | + const { container } = render( |
| 96 | + <RelatedList |
| 97 | + title="Projects" |
| 98 | + type="table" |
| 99 | + api="project" |
| 100 | + objectName="project" |
| 101 | + referenceField="parent" |
| 102 | + parentId="P-1" |
| 103 | + dataSource={dataSource as any} |
| 104 | + />, |
| 105 | + ); |
| 106 | + await waitFor(() => expect(container.querySelector('table')).not.toBeNull()); |
| 107 | + await waitFor(() => expect(container.textContent).toContain('Tower')); |
| 108 | + const cells = Array.from(container.querySelectorAll('tbody td')).map((td) => (td.textContent ?? '').trim()); |
| 109 | + expect(cells, `cells: ${JSON.stringify(cells)}`).toContain('$90,000.00'); |
| 110 | + expect(cells.some((c) => c.includes('1,234.50') && !c.includes('$'))).toBe(true); |
| 111 | + }); |
| 112 | +}); |
0 commit comments