|
| 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#28255: a percent field's authored `percentDisplay` reaches the |
| 11 | + * grid cell on every configured-column path. |
| 12 | + * |
| 13 | + * `PercentCellRenderer` reads `field.percentDisplay`, but the three |
| 14 | + * configured-column paths (ListColumn objects, a string array, inline rows |
| 15 | + * with a `fields` projection) each build a `fieldMeta` bag key by key, the |
| 16 | + * way `useGrouping` rides (objectui#11026). So each path is driven here. |
| 17 | + * |
| 18 | + * ── Directions with the cell's read in place and the grid's relay removed ── |
| 19 | + * `plain` probability, each configured path RED (two bars per row) |
| 20 | + * the control field (no key) GREEN (one bar either way) |
| 21 | + * auto-generated columns (whole def, no bag) GREEN (the reference face) |
| 22 | + */ |
| 23 | + |
| 24 | +import React from 'react'; |
| 25 | +import { describe, it, expect, beforeAll, afterEach, vi } from 'vitest'; |
| 26 | +import { render, screen, waitFor, cleanup } from '@testing-library/react'; |
| 27 | +import '@testing-library/jest-dom'; |
| 28 | +import { I18nProvider, LocalizationProvider } from '@object-ui/i18n'; |
| 29 | +import { registerAllFields } from '@object-ui/fields'; |
| 30 | +import { ActionProvider, SchemaRendererProvider } from '@object-ui/react'; |
| 31 | +import { ObjectGrid } from '../ObjectGrid'; |
| 32 | + |
| 33 | +registerAllFields(); |
| 34 | + |
| 35 | +beforeAll(() => { |
| 36 | + if (!Element.prototype.scrollIntoView) { |
| 37 | + Element.prototype.scrollIntoView = vi.fn() as unknown as Element['scrollIntoView']; |
| 38 | + } |
| 39 | +}); |
| 40 | + |
| 41 | +afterEach(cleanup); |
| 42 | + |
| 43 | +const OBJECT = 'os_28255_deal'; |
| 44 | + |
| 45 | +const FIELDS: Record<string, Record<string, unknown>> = { |
| 46 | + id: { type: 'text' }, |
| 47 | + name: { type: 'text', label: 'Deal Name' }, |
| 48 | + // A probability, not progress: the author asks for the plain face. |
| 49 | + probability: { type: 'percent', label: 'Win Chance', percentDisplay: 'plain' }, |
| 50 | + // The control: no key, so the bar stays. |
| 51 | + completion: { type: 'percent', label: 'Completion' }, |
| 52 | +}; |
| 53 | + |
| 54 | +const ROW = { id: 'd1', name: 'Tower', probability: 0.25, completion: 0.6 }; |
| 55 | +const PERCENT_FIELDS = ['probability', 'completion'] as const; |
| 56 | + |
| 57 | +function Providers({ children }: { children: React.ReactNode }) { |
| 58 | + return ( |
| 59 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }} persistLanguage={false}> |
| 60 | + <LocalizationProvider value={{ locale: 'en-US' }}>{children}</LocalizationProvider> |
| 61 | + </I18nProvider> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +function makeDataSource() { |
| 66 | + return { |
| 67 | + find: vi.fn(async () => ({ data: [ROW], total: 1, hasMore: false, pageSize: 50 })), |
| 68 | + getObjectSchema: async (name: string) => ({ name, fields: structuredClone(FIELDS) }), |
| 69 | + } as never; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Render the grid and wait for the ENRICHED columns: until the object schema |
| 74 | + * arrives a column with no type is drawn withheld (masked). The def's label, |
| 75 | + * which differs from the humanized field name, arrives in the same pass. |
| 76 | + */ |
| 77 | +async function renderGrid(schemaExtra: Record<string, unknown>) { |
| 78 | + const ds = makeDataSource(); |
| 79 | + const utils = render( |
| 80 | + <Providers> |
| 81 | + <ActionProvider> |
| 82 | + <SchemaRendererProvider dataSource={ds}> |
| 83 | + <ObjectGrid |
| 84 | + schema={{ |
| 85 | + type: 'object-grid', |
| 86 | + objectName: OBJECT, |
| 87 | + data: { provider: 'value', items: [ROW] }, |
| 88 | + pagination: { pageSize: 50 }, |
| 89 | + ...schemaExtra, |
| 90 | + } as never} |
| 91 | + dataSource={ds} |
| 92 | + /> |
| 93 | + </SchemaRendererProvider> |
| 94 | + </ActionProvider> |
| 95 | + </Providers>, |
| 96 | + ); |
| 97 | + await waitFor(() => expect(screen.getAllByText('Win Chance').length).toBeGreaterThan(0)); |
| 98 | + return utils; |
| 99 | +} |
| 100 | + |
| 101 | +/** Each body cell's text and whether it draws a progress bar. */ |
| 102 | +function cells(container: HTMLElement): Array<{ text: string; bar: boolean }> { |
| 103 | + return Array.from(container.querySelectorAll('tbody td')).map((td) => ({ |
| 104 | + text: (td.textContent ?? '').trim(), |
| 105 | + bar: td.querySelector('[role="progressbar"]') !== null, |
| 106 | + })); |
| 107 | +} |
| 108 | + |
| 109 | +function expectDeclaredFaces(container: HTMLElement) { |
| 110 | + const all = cells(container); |
| 111 | + const probability = all.find((c) => c.text === '25%'); |
| 112 | + const completion = all.find((c) => c.text === '60%'); |
| 113 | + expect(probability, `cells: ${JSON.stringify(all)}`).toBeDefined(); |
| 114 | + expect(completion, `cells: ${JSON.stringify(all)}`).toBeDefined(); |
| 115 | + expect(probability!.bar, '`plain` draws no bar').toBe(false); |
| 116 | + expect(completion!.bar, 'CONTROL: no key keeps the bar').toBe(true); |
| 117 | +} |
| 118 | + |
| 119 | +/** The three configured-column paths that build a `fieldMeta` bag. */ |
| 120 | +const PATHS: Array<[string, Record<string, unknown>]> = [ |
| 121 | + ['ListColumn objects', { columns: [{ field: 'name' }, ...PERCENT_FIELDS.map((field) => ({ field }))] }], |
| 122 | + ['string columns', { columns: ['name', ...PERCENT_FIELDS] }], |
| 123 | + ['inline rows + fields projection', { fields: ['name', ...PERCENT_FIELDS] }], |
| 124 | +]; |
| 125 | + |
| 126 | +describe('a percent field\'s authored percentDisplay reaches the grid cell (objectstack#28255)', () => { |
| 127 | + it.each(PATHS)('%s: `plain` prints the percentage with no bar', async (_path, schemaExtra) => { |
| 128 | + const { container } = await renderGrid(schemaExtra); |
| 129 | + expectDeclaredFaces(container); |
| 130 | + }); |
| 131 | + |
| 132 | + it('auto-generated columns (the whole def, no bag): the reference face', async () => { |
| 133 | + const { container } = await renderGrid({}); |
| 134 | + expectDeclaredFaces(container); |
| 135 | + }); |
| 136 | +}); |
0 commit comments