|
| 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 | + * ONE record, TWO places, ONE number (objectui#9071). |
| 11 | + * |
| 12 | + * ## What this pins, and why it is not "the chip's new number is right" |
| 13 | + * |
| 14 | + * The chip beside the record H1 scaled a stored `percent` by a rule of its |
| 15 | + * own — a pass-through above 1, a scale-by-100 at or below it — while the |
| 16 | + * declared single source of truth (`percentDisplayValue` in |
| 17 | + * `@object-ui/core`, which the list cell reaches through `formatPercent`) |
| 18 | + * uses the symmetric `value > -1 && value < 1`. The two rules disagree at |
| 19 | + * EXACTLY 1 and at every value at or below -1. |
| 20 | + * |
| 21 | + * Triage's judgement is the acceptance, and it is about DISAGREEMENT, not |
| 22 | + * about the chip: |
| 23 | + * |
| 24 | + * "a stored `1` renders `100%` in the chip and `1%` everywhere else" — the |
| 25 | + * same record showing two different numbers in two places is the kind of |
| 26 | + * disagreement a user reports as a data bug rather than a formatting one. |
| 27 | + * |
| 28 | + * So every case here renders BOTH surfaces from the SAME stored value and the |
| 29 | + * SAME field, and asserts they agree. A pin that only checked the chip could |
| 30 | + * not fail for the reason this card exists: it would go green on a chip that |
| 31 | + * had simply moved to a fourth rule of its own. |
| 32 | + * |
| 33 | + * ## Two instruments, because neither alone covers the disagreement set |
| 34 | + * |
| 35 | + * - **The stated text.** The only instrument that reaches a value at or below |
| 36 | + * -1: both bars clamp a negative to an empty track, so the bar cannot tell |
| 37 | + * `-5%` from `-500%`. ⚠️ Valid only where the two surfaces' CONVENTIONS |
| 38 | + * coincide — the chip appends a bare `%` to the full number, the cell renders |
| 39 | + * through the locale's percent affix at the field's precision (0 by default). |
| 40 | + * Every row asserted by text below scales to an integral count of percentage |
| 41 | + * points under four digits, where the two conventions are byte-identical. |
| 42 | + * The last case pins a value where they are NOT, so the half this card does |
| 43 | + * not fix is a recorded fact rather than a silence. |
| 44 | + * - **The drawn bar.** Read off each surface's own fill width, before any |
| 45 | + * rounding, so it states agreement on the scaled MAGNITUDE independently of |
| 46 | + * how either side spells it. |
| 47 | + * |
| 48 | + * ## The live control |
| 49 | + * |
| 50 | + * `0.25` is inside the band where the two rules already agreed before this |
| 51 | + * change. It renders identically in both places on both legs of the ablation — |
| 52 | + * if it ever moves, the repair reached further than the boundary. |
| 53 | + */ |
| 54 | + |
| 55 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 56 | +import { render, cleanup } from '@testing-library/react'; |
| 57 | +import * as React from 'react'; |
| 58 | +import { I18nProvider, LocalizationProvider } from '@object-ui/i18n'; |
| 59 | +import { PercentCellRenderer } from '@object-ui/fields'; |
| 60 | +import { DetailView } from '../DetailView'; |
| 61 | +import type { DetailViewSchema, FieldMetadata } from '@object-ui/types'; |
| 62 | + |
| 63 | +/** |
| 64 | + * `useRecordEditable` falls back to the GLOBAL fetch with no |
| 65 | + * `SchemaRendererProvider` in the tree; under happy-dom that is a real request. |
| 66 | + * Served from a double so no case here depends on the network. |
| 67 | + */ |
| 68 | +beforeEach(() => { |
| 69 | + vi.stubGlobal( |
| 70 | + 'fetch', |
| 71 | + vi.fn(async () => ({ ok: true, json: async () => ({ record: { visible: true } }) })), |
| 72 | + ); |
| 73 | +}); |
| 74 | +afterEach(() => { |
| 75 | + vi.unstubAllGlobals(); |
| 76 | + cleanup(); |
| 77 | +}); |
| 78 | + |
| 79 | +/** |
| 80 | + * The field both surfaces are handed. `ratio` is deliberate: it does NOT match |
| 81 | + * the cell renderer's whole-percent name pattern (`progress` / `completion`), |
| 82 | + * so both surfaces are on the fraction-inferring path this card is about. |
| 83 | + */ |
| 84 | +const FIELD: FieldMetadata = { name: 'ratio', label: 'Ratio', type: 'percent' }; |
| 85 | + |
| 86 | +interface Rendered { |
| 87 | + /** What the surface SAYS, whitespace-collapsed. */ |
| 88 | + text: string; |
| 89 | + /** What the surface DRAWS, read off its own fill width. */ |
| 90 | + bar: number; |
| 91 | +} |
| 92 | + |
| 93 | +/** Place one: the `summaryFields` chip beside the record H1. */ |
| 94 | +function renderChip(stored: number): Rendered { |
| 95 | + const container = render( |
| 96 | + <DetailView |
| 97 | + schema={ |
| 98 | + { |
| 99 | + type: 'record:details', |
| 100 | + objectName: 'account', |
| 101 | + summaryFields: ['ratio'], |
| 102 | + fields: [{ ...FIELD }], |
| 103 | + data: { id: 'A9', name: 'Acme', ratio: stored }, |
| 104 | + } as unknown as DetailViewSchema |
| 105 | + } |
| 106 | + />, |
| 107 | + ).container; |
| 108 | + const chip = container.querySelector<HTMLElement>('[data-summary-chip="ratio"]'); |
| 109 | + expect(chip, 'a summary chip for "ratio" is beside the H1').not.toBeNull(); |
| 110 | + const fill = chip!.querySelector<HTMLElement>('[aria-hidden] span[style]'); |
| 111 | + expect(fill, 'the chip draws its own bar fill').not.toBeNull(); |
| 112 | + return { text: (chip!.textContent ?? '').replace(/\s+/g, ' ').trim(), bar: pct(fill!.style.width) }; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Place two: the list cell — `PercentCellRenderer`, the surface that reaches |
| 117 | + * `percentDisplayValue` through `formatPercent`. Mounted under an explicit |
| 118 | + * `en` session so the comparison is a property of the SCALING and not of |
| 119 | + * whatever locale the runner happens to carry. |
| 120 | + */ |
| 121 | +function renderCell(stored: number): Rendered { |
| 122 | + const container = render( |
| 123 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }} persistLanguage={false}> |
| 124 | + <LocalizationProvider value={{ locale: 'en' }}> |
| 125 | + <PercentCellRenderer value={stored} field={{ ...FIELD }} /> |
| 126 | + </LocalizationProvider> |
| 127 | + </I18nProvider>, |
| 128 | + ).container; |
| 129 | + const bar = container.querySelector<HTMLElement>('[role="progressbar"] div[style]'); |
| 130 | + expect(bar, 'the cell draws its own bar fill').not.toBeNull(); |
| 131 | + const value = container.querySelector<HTMLElement>('span.tabular-nums'); |
| 132 | + expect(value, 'the cell states its own percentage').not.toBeNull(); |
| 133 | + return { text: (value!.textContent ?? '').replace(/\s+/g, ' ').trim(), bar: pct(bar!.style.width) }; |
| 134 | +} |
| 135 | + |
| 136 | +function pct(width: string): number { |
| 137 | + expect(width, 'the fill width is a percentage').toMatch(/%$/); |
| 138 | + return Number(width.slice(0, -1)); |
| 139 | +} |
| 140 | + |
| 141 | +interface Row { |
| 142 | + what: string; |
| 143 | + stored: number; |
| 144 | + /** The one percentage BOTH surfaces state. */ |
| 145 | + text: string; |
| 146 | + /** The one magnitude BOTH surfaces draw, after each clamps to its track. */ |
| 147 | + bar: number; |
| 148 | +} |
| 149 | + |
| 150 | +const ROWS: Row[] = [ |
| 151 | + // LIVE CONTROL — inside the band where the two rules already agreed. Green |
| 152 | + // before this change and green after; it is the row that says the repair did |
| 153 | + // not reach past the boundary. |
| 154 | + { what: 'CONTROL — inside the band both rules already agreed on', stored: 0.25, text: '25%', bar: 25 }, |
| 155 | + // THE FORK objectui#8728 pinned and handed here. The chip read a stored 1 as |
| 156 | + // a ratio and said `100%` beside a full bar; every other band reads it as one |
| 157 | + // percentage point. |
| 158 | + { what: 'EXACTLY 1 — one percentage point, not one hundred', stored: 1, text: '1%', bar: 1 }, |
| 159 | + // The far edge of the same boundary. `percentDisplayValue` passes -1 through; |
| 160 | + // the chip's rule scaled it to -100. |
| 161 | + { what: 'EXACTLY -1 — the symmetric edge the chip did not have', stored: -1, text: '-1%', bar: 0 }, |
| 162 | + // Below -1. Bars are useless here (both clamp to an empty track), which is |
| 163 | + // why the text instrument exists. |
| 164 | + { what: 'below -1 — the half the bar cannot see', stored: -5, text: '-5%', bar: 0 }, |
| 165 | +]; |
| 166 | + |
| 167 | +describe('the summary chip reads the declared percent source (objectui#9071)', () => { |
| 168 | + it.each(ROWS)('$what: a stored $stored reads $text in BOTH places', ({ stored, text, bar }) => { |
| 169 | + const chip = renderChip(stored); |
| 170 | + cleanup(); |
| 171 | + const cell = renderCell(stored); |
| 172 | + |
| 173 | + expect( |
| 174 | + chip.text, |
| 175 | + 'THE PIN: the chip and the list cell state the same percentage for the same stored value', |
| 176 | + ).toBe(cell.text); |
| 177 | + expect(chip.text, 'and it is this percentage').toBe(text); |
| 178 | + expect(chip.bar, 'the chip draws what the cell draws').toBe(cell.bar); |
| 179 | + expect(chip.bar, 'and it is this magnitude').toBe(bar); |
| 180 | + }); |
| 181 | + |
| 182 | + /** |
| 183 | + * The relation stated once over the whole table rather than row by row, so a |
| 184 | + * repair that fixed one row and broke another is red here even if someone |
| 185 | + * edited that row's expected string to match. |
| 186 | + */ |
| 187 | + it('never states one percentage in the chip and another in the cell', () => { |
| 188 | + const disagreements = ROWS.filter(({ stored }) => { |
| 189 | + const chip = renderChip(stored); |
| 190 | + cleanup(); |
| 191 | + const cell = renderCell(stored); |
| 192 | + cleanup(); |
| 193 | + return chip.text !== cell.text || chip.bar !== cell.bar; |
| 194 | + }); |
| 195 | + expect(disagreements.map((r) => r.stored), 'every stored value reads the same in both places').toEqual([]); |
| 196 | + }); |
| 197 | + |
| 198 | + /** |
| 199 | + * ⚠️ THE HALF THIS CARD DOES NOT FIX, pinned so it cannot go quiet. |
| 200 | + * |
| 201 | + * `percentDisplayValue`'s doc comment requires a third surface to take BOTH |
| 202 | + * halves — the scaling AND the convention. This card moves the SCALING only, |
| 203 | + * because taking the convention half would change what the chip prints for |
| 204 | + * values it renders correctly today: the cell rounds to the field's precision |
| 205 | + * (0 by default) and groups through `Intl`, the chip states the full number |
| 206 | + * with a bare `%`. So a stored `12.3` still reads `12.3%` on the chip and |
| 207 | + * `12%` in the cell — DIFFERENT SPELLINGS OF THE SAME MAGNITUDE, which the |
| 208 | + * bars prove by agreeing exactly. |
| 209 | + */ |
| 210 | + it('agrees on the magnitude even where the two surfaces spell it differently', () => { |
| 211 | + const chip = renderChip(12.3); |
| 212 | + cleanup(); |
| 213 | + const cell = renderCell(12.3); |
| 214 | + |
| 215 | + expect(chip.bar, 'the scaling agrees — this is what objectui#9071 repaired').toBe(cell.bar); |
| 216 | + expect(chip.bar, 'both draw 12.3 points').toBe(12.3); |
| 217 | + expect(chip.text, 'the chip states the full number with a bare percent sign').toBe('12.3%'); |
| 218 | + expect(cell.text, "the cell rounds to the field's precision and renders the locale's affix").toBe('12%'); |
| 219 | + }); |
| 220 | +}); |
0 commit comments