|
| 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 2 — a dataset-bound `metric` tile captions its value |
| 11 | + * with the widget's `description` when one is authored, and with the measure |
| 12 | + * label otherwise. |
| 13 | + * |
| 14 | + * The tile drew the measure label ("Total Amount") under every value, and the |
| 15 | + * widget's `description` (the spec's `DashboardWidget.description`, "Widget |
| 16 | + * description text below the header"), which says what the number counts, |
| 17 | + * only in the title's tooltip. Now the description is the caption, in the |
| 18 | + * measure label's place: the tile keeps its two lines of text, and the |
| 19 | + * measure label still rides in the title's tooltip, under the description. |
| 20 | + * |
| 21 | + * Pins, on both dashboard surfaces (`DashboardRenderer`, `DashboardGridLayout`): |
| 22 | + * - a described metric: the caption under the value is the description, drawn |
| 23 | + * once; the measure label is drawn nowhere but the tooltip; |
| 24 | + * - an undescribed metric (and one whose description is empty): the caption is |
| 25 | + * the measure label, as before; |
| 26 | + * - an inline per-locale description map: the caption follows the UI language; |
| 27 | + * - CONTROL: a dataset-bound table draws no metric caption (`DashboardRenderer`'s |
| 28 | + * card-header description paragraph is pinned in |
| 29 | + * `DashboardRenderer.metricTileDensity-12528.test.tsx`). |
| 30 | + * |
| 31 | + * No `dist/` is involved: the root `vitest.config.mts` aliases every |
| 32 | + * `@object-ui/*` specifier to that package's `src/`, and the subjects are |
| 33 | + * imported relatively. |
| 34 | + */ |
| 35 | + |
| 36 | +import * as React from 'react'; |
| 37 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 38 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 39 | +import { I18nProvider } from '@object-ui/i18n'; |
| 40 | +import type { DashboardComponentSchema } from '@object-ui/types'; |
| 41 | +// Module scope, never inside a hook (AGENTS.md 测试纪律): both surfaces resolve |
| 42 | +// non-dataset nodes through the registries these barrels populate. |
| 43 | +import '@object-ui/components'; |
| 44 | +import { DashboardRenderer } from '../DashboardRenderer'; |
| 45 | +import { DashboardGridLayout } from '../DashboardGridLayout'; |
| 46 | + |
| 47 | +afterEach(cleanup); |
| 48 | + |
| 49 | +const dashboard = (widgets: Record<string, unknown>[]): DashboardComponentSchema => |
| 50 | + ({ type: 'dashboard', name: 'sales', widgets }) as unknown as DashboardComponentSchema; |
| 51 | + |
| 52 | +const source = () => ({ |
| 53 | + queryDataset: vi.fn(async () => ({ |
| 54 | + rows: [{ total_amount: 1964500, stage: 'won' }], |
| 55 | + fields: [ |
| 56 | + { name: 'total_amount', label: 'Total Amount', type: 'number' }, |
| 57 | + { name: 'stage', label: 'Stage', type: 'text' }, |
| 58 | + ], |
| 59 | + })), |
| 60 | +}); |
| 61 | + |
| 62 | +const METRIC = { |
| 63 | + id: 'total_pipeline_value', |
| 64 | + type: 'metric', |
| 65 | + title: 'Total Pipeline', |
| 66 | + description: 'Open opportunities, all owners', |
| 67 | + dataset: 'opportunity_metrics', |
| 68 | + values: ['total_amount'], |
| 69 | +}; |
| 70 | + |
| 71 | +type Surface = (schema: DashboardComponentSchema, dataSource: unknown) => React.ReactElement; |
| 72 | + |
| 73 | +const SURFACES: Array<[string, Surface]> = [ |
| 74 | + ['DashboardRenderer', (schema, dataSource) => <DashboardRenderer schema={schema} dataSource={dataSource} />], |
| 75 | + ['DashboardGridLayout', (schema, dataSource) => <DashboardGridLayout schema={schema} dataSource={dataSource} />], |
| 76 | +]; |
| 77 | + |
| 78 | +const renderOn = (surface: Surface, schema: DashboardComponentSchema, language = 'en') => |
| 79 | + render( |
| 80 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }}> |
| 81 | + {surface(schema, source())} |
| 82 | + </I18nProvider>, |
| 83 | + ); |
| 84 | + |
| 85 | +describe.each(SURFACES)('%s — the metric tile caption (objectstack#27430)', (_name, surface) => { |
| 86 | + it('draws an authored description as the caption under the value, once, in place of the measure label', async () => { |
| 87 | + const { container } = renderOn(surface, dashboard([METRIC])); |
| 88 | + const value = await screen.findByText('1964500'); |
| 89 | + const caption = screen.getByTestId('dataset-metric-label'); |
| 90 | + |
| 91 | + expect(value.nextElementSibling).toBe(caption); |
| 92 | + expect(caption).toHaveTextContent('Open opportunities, all owners'); |
| 93 | + expect(container.textContent?.match(/Open opportunities, all owners/g)).toHaveLength(1); |
| 94 | + // The measure label is no longer drawn; the title's tooltip still carries |
| 95 | + // it, under the description. |
| 96 | + expect(container.textContent).not.toContain('Total Amount'); |
| 97 | + expect(screen.getByTestId('dataset-metric-title')).toHaveAttribute( |
| 98 | + 'title', |
| 99 | + 'Open opportunities, all owners\nTotal Amount', |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it.each([ |
| 104 | + ['declares no description', undefined], |
| 105 | + ['declares an empty description', ''], |
| 106 | + ])('falls back to the measure label when the widget %s', async (_case, description) => { |
| 107 | + renderOn(surface, dashboard([{ ...METRIC, description }])); |
| 108 | + const value = await screen.findByText('1964500'); |
| 109 | + |
| 110 | + expect(value.nextElementSibling).toBe(screen.getByTestId('dataset-metric-label')); |
| 111 | + expect(screen.getByTestId('dataset-metric-label')).toHaveTextContent('Total Amount'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('follows the UI language for an inline per-locale description', async () => { |
| 115 | + renderOn( |
| 116 | + surface, |
| 117 | + dashboard([{ ...METRIC, description: { en: 'Open opportunities, all owners', 'zh-CN': '所有负责人的未结商机' } }]), |
| 118 | + 'zh-CN', |
| 119 | + ); |
| 120 | + await screen.findByText('1964500'); |
| 121 | + |
| 122 | + expect(screen.getByTestId('dataset-metric-label')).toHaveTextContent('所有负责人的未结商机'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('CONTROL: a dataset-bound table draws no metric caption', async () => { |
| 126 | + const { container } = renderOn(surface, dashboard([{ |
| 127 | + id: 'by_stage', |
| 128 | + type: 'table', |
| 129 | + title: 'Pipeline by Stage', |
| 130 | + description: 'Open opportunity value at each sales stage', |
| 131 | + dataset: 'opportunity_metrics', |
| 132 | + dimensions: ['stage'], |
| 133 | + values: ['total_amount'], |
| 134 | + }])); |
| 135 | + |
| 136 | + // The answered table: its cell draws the value. |
| 137 | + await screen.findByText('Pipeline by Stage'); |
| 138 | + await screen.findAllByText(/1,?964,?500/); |
| 139 | + expect(container.querySelector('[data-testid="dataset-metric-label"]')).toBeNull(); |
| 140 | + expect(container.querySelector('[data-testid="dataset-metric-heading"]')).toBeNull(); |
| 141 | + }); |
| 142 | +}); |
0 commit comments