|
| 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#27494: a formula declaring `returnType: 'currency'` reads as |
| 11 | + * money on the grid cell, the same face as the currency field beside it. |
| 12 | + * |
| 13 | + * The formula cell (`FormulaCellRenderer`) picks its face from the declared |
| 14 | + * `returnType`, and a `currency` one draws through `CurrencyCellRenderer` with |
| 15 | + * the formula's own `currencyConfig`. The three configured-column paths built |
| 16 | + * the cell's `fieldMeta` bag from a picked key list that carried |
| 17 | + * `currencyConfig` and `scale` but not `returnType`, so the cell saw an |
| 18 | + * undeclared formula holding a JS number and drew the number face: the |
| 19 | + * showcase's Budget Remaining read `90,000.00` beside Budget's `$150,000.00`. |
| 20 | + * |
| 21 | + * The tenant is EUR on purpose: the formula's `$` can then only come from its |
| 22 | + * own `fixed` `currencyConfig`, never from a tenant fallback. |
| 23 | + * |
| 24 | + * ── Directions on the base tree (predicted before the first run) ───────── |
| 25 | + * currency formula, each of the three paths RED (`90,000.00`) |
| 26 | + * agreement with the whole-def formula cell RED (the currency row) |
| 27 | + * number formula / undeclared formula GREEN (the number face is right) |
| 28 | + * auto-generated columns (whole def, no bag) GREEN (the reference face) |
| 29 | + */ |
| 30 | + |
| 31 | +import React from 'react'; |
| 32 | +import { describe, it, expect, beforeAll, afterEach, vi } from 'vitest'; |
| 33 | +import { render, screen, waitFor, cleanup } from '@testing-library/react'; |
| 34 | +import '@testing-library/jest-dom'; |
| 35 | +import { I18nProvider, LocalizationProvider } from '@object-ui/i18n'; |
| 36 | +import { FormulaCellRenderer, registerAllFields } from '@object-ui/fields'; |
| 37 | +import { ActionProvider, SchemaRendererProvider } from '@object-ui/react'; |
| 38 | +import { ObjectGrid } from '../ObjectGrid'; |
| 39 | + |
| 40 | +registerAllFields(); |
| 41 | + |
| 42 | +beforeAll(() => { |
| 43 | + if (!Element.prototype.scrollIntoView) { |
| 44 | + Element.prototype.scrollIntoView = vi.fn() as unknown as Element['scrollIntoView']; |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +afterEach(cleanup); |
| 49 | + |
| 50 | +/** A EUR tenant: a `$` in a cell is the field's own fixed currency. */ |
| 51 | +const TENANT = { currency: 'EUR', locale: 'en' }; |
| 52 | + |
| 53 | +const OBJECT = 'os_27494_project'; |
| 54 | + |
| 55 | +const USD_FIXED = { currencyMode: 'fixed', defaultCurrency: 'USD' }; |
| 56 | + |
| 57 | +const FIELDS: Record<string, Record<string, unknown>> = { |
| 58 | + id: { type: 'text' }, |
| 59 | + name: { type: 'text', label: 'Project Name' }, |
| 60 | + budget: { type: 'currency', label: 'Budget', currencyConfig: USD_FIXED }, |
| 61 | + // The showcase's Budget Remaining, as `project.object.ts` declares it. |
| 62 | + remaining: { |
| 63 | + type: 'formula', |
| 64 | + label: 'Budget Remaining', |
| 65 | + returnType: 'currency', |
| 66 | + currencyConfig: USD_FIXED, |
| 67 | + scale: 2, |
| 68 | + }, |
| 69 | + // Controls: a declared number, and a formula declaring nothing. |
| 70 | + ratio: { type: 'formula', label: 'Burn Ratio', returnType: 'number', scale: 2 }, |
| 71 | + bare: { type: 'formula', label: 'Bare Result' }, |
| 72 | +}; |
| 73 | + |
| 74 | +const ROW = { id: 'p1', name: 'Tower', budget: 150000, remaining: 90000, ratio: 1234.5, bare: 4321 }; |
| 75 | +const FORMULA_FIELDS = ['remaining', 'ratio', 'bare'] as const; |
| 76 | +const SHOWN = ['budget', ...FORMULA_FIELDS] as const; |
| 77 | + |
| 78 | +function Providers({ children }: { children: React.ReactNode }) { |
| 79 | + return ( |
| 80 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }} persistLanguage={false}> |
| 81 | + <LocalizationProvider value={TENANT}>{children}</LocalizationProvider> |
| 82 | + </I18nProvider> |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +/** What the formula cell renders for `field` when handed the WHOLE def. */ |
| 87 | +function wholeDefCell(field: string): string { |
| 88 | + const { container, unmount } = render( |
| 89 | + <Providers> |
| 90 | + <FormulaCellRenderer value={ROW[field as keyof typeof ROW]} field={{ name: field, ...FIELDS[field] } as any} /> |
| 91 | + </Providers>, |
| 92 | + ); |
| 93 | + const text = container.textContent ?? ''; |
| 94 | + unmount(); |
| 95 | + return text; |
| 96 | +} |
| 97 | + |
| 98 | +function makeDataSource() { |
| 99 | + return { |
| 100 | + find: vi.fn(async () => ({ data: [ROW], total: 1, hasMore: false, pageSize: 50 })), |
| 101 | + getObjectSchema: async (name: string) => ({ name, fields: structuredClone(FIELDS) }), |
| 102 | + } as never; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Render the grid and wait for the ENRICHED columns: the header reads the |
| 107 | + * def's label in the same memo pass that builds the cell bag, so it is the |
| 108 | + * signal that the bag was built from the object schema. |
| 109 | + */ |
| 110 | +async function renderGrid(schemaExtra: Record<string, unknown>) { |
| 111 | + const ds = makeDataSource(); |
| 112 | + const utils = render( |
| 113 | + <Providers> |
| 114 | + <ActionProvider> |
| 115 | + <SchemaRendererProvider dataSource={ds}> |
| 116 | + <ObjectGrid |
| 117 | + schema={{ |
| 118 | + type: 'object-grid', |
| 119 | + objectName: OBJECT, |
| 120 | + data: { provider: 'value', items: [ROW] }, |
| 121 | + pagination: { pageSize: 50 }, |
| 122 | + ...schemaExtra, |
| 123 | + } as never} |
| 124 | + dataSource={ds} |
| 125 | + /> |
| 126 | + </SchemaRendererProvider> |
| 127 | + </ActionProvider> |
| 128 | + </Providers>, |
| 129 | + ); |
| 130 | + await waitFor(() => expect(screen.getAllByText('Budget Remaining').length).toBeGreaterThan(0)); |
| 131 | + return utils; |
| 132 | +} |
| 133 | + |
| 134 | +function cellTexts(container: HTMLElement): string[] { |
| 135 | + return Array.from(container.querySelectorAll('tbody td')).map((td) => (td.textContent ?? '').trim()); |
| 136 | +} |
| 137 | + |
| 138 | +/** The three configured-column paths that build a `fieldMeta` bag. */ |
| 139 | +const PATHS: Array<[string, Record<string, unknown>]> = [ |
| 140 | + ['ListColumn objects', { columns: [{ field: 'name' }, ...SHOWN.map((field) => ({ field }))] }], |
| 141 | + ['string columns', { columns: ['name', ...SHOWN] }], |
| 142 | + ['inline rows + fields projection', { fields: ['name', ...SHOWN] }], |
| 143 | +]; |
| 144 | + |
| 145 | +describe('a returnType currency formula reads as money on the grid cell (objectstack#27494)', () => { |
| 146 | + it.each(PATHS)('%s: the currency formula reads as money beside the currency field', async (_path, schemaExtra) => { |
| 147 | + const { container } = await renderGrid(schemaExtra); |
| 148 | + const cells = cellTexts(container); |
| 149 | + expect(cells, `cells: ${JSON.stringify(cells)}`).toContain('$150,000.00'); |
| 150 | + expect(cells).toContain('$90,000.00'); |
| 151 | + expect(cells).not.toContain('90,000.00'); |
| 152 | + }); |
| 153 | + |
| 154 | + it.each(PATHS)('%s: control, a number formula and an undeclared one keep the bare number', async (_path, schemaExtra) => { |
| 155 | + const { container } = await renderGrid(schemaExtra); |
| 156 | + const cells = cellTexts(container); |
| 157 | + expect(cells, `cells: ${JSON.stringify(cells)}`).toContain('1,234.50'); |
| 158 | + expect(cells).toContain('4,321'); |
| 159 | + }); |
| 160 | + |
| 161 | + it.each(PATHS)('%s: every formula cell reads what the whole-def formula cell reads', async (_path, schemaExtra) => { |
| 162 | + const { container } = await renderGrid(schemaExtra); |
| 163 | + const cells = cellTexts(container); |
| 164 | + cleanup(); |
| 165 | + for (const field of FORMULA_FIELDS) { |
| 166 | + const expected = wholeDefCell(field); |
| 167 | + expect(cells, `${field}: whole-def cell reads ${expected}; grid cells ${JSON.stringify(cells)}`).toContain(expected); |
| 168 | + } |
| 169 | + }); |
| 170 | +}); |
| 171 | + |
| 172 | +describe('the reference the configured paths now match (objectstack#27494)', () => { |
| 173 | + // The auto-generated columns hand the cell the WHOLE field def and build no |
| 174 | + // bag, so they read the declared face on the base tree. GREEN there by |
| 175 | + // design: it names the face the three configured paths disagreed with. |
| 176 | + it('auto-generated columns: the currency formula reads as money', async () => { |
| 177 | + const { container } = await renderGrid({}); |
| 178 | + const cells = cellTexts(container); |
| 179 | + expect(cells).toContain('$90,000.00'); |
| 180 | + expect(cells).toContain('1,234.50'); |
| 181 | + }); |
| 182 | +}); |
0 commit comments