|
| 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#33542 — the footer total of a roll-up (`summary`) column is |
| 11 | + * formatted as the list cell draws the same field. |
| 12 | + * |
| 13 | + * HotCRM's All Invoices footer read "Paid 230,000" for a `Field.summary` with |
| 14 | + * `scale: 2`, beside "Open 215,000.00" for a currency field: the footer's |
| 15 | + * formatter keyed on the column type, and a `summary` fell to the plain |
| 16 | + * default, which never reads `scale`. The served object metadata carries the |
| 17 | + * summary's `scale` (measured on the showcase's `showcase_expense_report`). |
| 18 | + * |
| 19 | + * The cell draws a summary through `FormulaCellRenderer`: as money when the |
| 20 | + * summary declares its currency (`currencyConfig`, `summaryDeclaresCurrency`), |
| 21 | + * else as a number at its `resolveFieldScale` width. The footer now takes the |
| 22 | + * same two arms, the ones a plain `currency` / `number` column takes. |
| 23 | + * |
| 24 | + * Pinned through the real `ObjectGrid` footer, its field catalogue read from |
| 25 | + * the data source's object schema, under a real `I18nProvider` in en and zh-CN: |
| 26 | + * |
| 27 | + * - a summary with `scale: 2` totals to two decimals; |
| 28 | + * - a summary declaring a `fixed` currency totals exactly as a plain currency |
| 29 | + * column with the same config does; |
| 30 | + * - CONTROL: a summary with no `scale` and no currency keeps today's face. |
| 31 | + */ |
| 32 | +import { describe, it, expect, vi, beforeAll, beforeEach, afterEach } from 'vitest'; |
| 33 | +import { render, screen, cleanup, waitFor } from '@testing-library/react'; |
| 34 | +import '@testing-library/jest-dom'; |
| 35 | +import React from 'react'; |
| 36 | + |
| 37 | +import { ObjectGrid } from '../ObjectGrid'; |
| 38 | +import { __clearRecordCrudVerdictCache } from '../hooks/useRecordCrudVerdicts'; |
| 39 | +import { installExplainDouble } from './explainDouble'; |
| 40 | +import { registerAllFields } from '@object-ui/fields'; |
| 41 | +import { ActionProvider, I18nProvider } from '@object-ui/react'; |
| 42 | + |
| 43 | +beforeAll(() => { |
| 44 | + registerAllFields(); |
| 45 | + if (!Element.prototype.scrollIntoView) { |
| 46 | + Element.prototype.scrollIntoView = vi.fn() as unknown as Element['scrollIntoView']; |
| 47 | + } |
| 48 | +}); |
| 49 | +beforeEach(() => { |
| 50 | + __clearRecordCrudVerdictCache(); |
| 51 | + installExplainDouble(); |
| 52 | +}); |
| 53 | +afterEach(() => { |
| 54 | + cleanup(); |
| 55 | + vi.unstubAllGlobals(); |
| 56 | +}); |
| 57 | + |
| 58 | +const OBJECT = 'invoice_33542'; |
| 59 | + |
| 60 | +const CNY = { currencyMode: 'fixed', defaultCurrency: 'CNY' } as const; |
| 61 | + |
| 62 | +/** The served field catalogue: three roll-ups and a plain currency field. */ |
| 63 | +const FIELDS = { |
| 64 | + id: { type: 'text', label: 'Id' }, |
| 65 | + invoice_number: { type: 'text', label: 'Invoice Number' }, |
| 66 | + paid_amount: { |
| 67 | + type: 'summary', label: 'Paid', scale: 2, |
| 68 | + summaryOperations: { object: 'payment', field: 'amount', function: 'sum' }, |
| 69 | + }, |
| 70 | + paid_cny: { |
| 71 | + type: 'summary', label: 'Paid (CNY)', scale: 2, currencyConfig: CNY, |
| 72 | + summaryOperations: { object: 'payment', field: 'amount', function: 'sum' }, |
| 73 | + }, |
| 74 | + billed_cny: { type: 'currency', label: 'Billed (CNY)', currencyConfig: CNY }, |
| 75 | + payment_count: { |
| 76 | + type: 'summary', label: 'Payments', |
| 77 | + summaryOperations: { object: 'payment', function: 'count' }, |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +/** Totals: 230,000 for every amount column, 7 payments. */ |
| 82 | +const ROWS = [ |
| 83 | + { id: 'i1', invoice_number: 'INV-1', paid_amount: 100000, paid_cny: 100000, billed_cny: 100000, payment_count: 3 }, |
| 84 | + { id: 'i2', invoice_number: 'INV-2', paid_amount: 130000, paid_cny: 130000, billed_cny: 130000, payment_count: 4 }, |
| 85 | +]; |
| 86 | + |
| 87 | +function renderGrid(language: 'en' | 'zh-CN') { |
| 88 | + const dataSource = { |
| 89 | + find: vi.fn(async () => ({ data: ROWS.map((r) => ({ ...r })), total: ROWS.length, hasMore: false, pageSize: 50 })), |
| 90 | + getObjectSchema: vi.fn(async (name: string) => ({ name, fields: FIELDS })), |
| 91 | + } as any; |
| 92 | + const schema: any = { |
| 93 | + type: 'object-grid', |
| 94 | + objectName: OBJECT, |
| 95 | + columns: [ |
| 96 | + { field: 'invoice_number', label: 'Invoice Number' }, |
| 97 | + { field: 'paid_amount', label: 'Paid', summary: 'sum' }, |
| 98 | + { field: 'paid_cny', label: 'Paid (CNY)', summary: 'sum' }, |
| 99 | + { field: 'billed_cny', label: 'Billed (CNY)', summary: 'sum' }, |
| 100 | + { field: 'payment_count', label: 'Payments', summary: 'sum' }, |
| 101 | + ], |
| 102 | + pagination: { pageSize: 50 }, |
| 103 | + }; |
| 104 | + return render( |
| 105 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }} persistLanguage={false}> |
| 106 | + <ActionProvider> |
| 107 | + <ObjectGrid schema={schema} dataSource={dataSource} /> |
| 108 | + </ActionProvider> |
| 109 | + </I18nProvider>, |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +/** The footer text of a column once the grid has its field catalogue. */ |
| 114 | +async function footerOf(field: string): Promise<string> { |
| 115 | + const cell = await screen.findByTestId(`summary-${field}`); |
| 116 | + return cell.textContent ?? ''; |
| 117 | +} |
| 118 | + |
| 119 | +describe('a summary column footer total is formatted as its cell (objectstack#33542)', () => { |
| 120 | + it('en: a summary with scale 2 totals to two decimals', async () => { |
| 121 | + renderGrid('en'); |
| 122 | + await waitFor(async () => expect(await footerOf('paid_amount')).toContain('Sum: 230,000.00')); |
| 123 | + }); |
| 124 | + |
| 125 | + it('zh-CN: a summary with scale 2 totals to two decimals, under the zh prefix', async () => { |
| 126 | + renderGrid('zh-CN'); |
| 127 | + await waitFor(async () => expect(await footerOf('paid_amount')).toContain('合计:230,000.00')); |
| 128 | + }); |
| 129 | + |
| 130 | + it.each(['en', 'zh-CN'] as const)('%s: a summary declaring a fixed currency totals as a plain currency column does', async (language) => { |
| 131 | + renderGrid(language); |
| 132 | + await waitFor(async () => expect(await footerOf('billed_cny')).toMatch(/230,000\.00/)); |
| 133 | + const currencyTotal = (await footerOf('billed_cny')).replace(/^.*?:/, ''); |
| 134 | + // The plain currency column draws money (a currency sign beside the amount). |
| 135 | + expect(currencyTotal).not.toBe('230,000.00'); |
| 136 | + expect((await footerOf('paid_cny')).replace(/^.*?:/, '')).toBe(currencyTotal); |
| 137 | + }); |
| 138 | + |
| 139 | + it('CONTROL: a summary with no scale and no currency keeps the plain total', async () => { |
| 140 | + renderGrid('en'); |
| 141 | + // The catalogue has landed once the plain currency column draws money. |
| 142 | + await waitFor(async () => expect(await footerOf('billed_cny')).toMatch(/230,000\.00/)); |
| 143 | + expect(await footerOf('payment_count')).toContain('Sum: 7'); |
| 144 | + expect(await footerOf('payment_count')).not.toContain('7.0'); |
| 145 | + }); |
| 146 | +}); |
0 commit comments