|
| 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 | + * `ui:header-bar`'s `rightContent` slot no longer leaks a stray `"0"` |
| 11 | + * (objectui#9033) — the numeric-falsy JSX trap objectui#8331 measured and |
| 12 | + * closed one slot over on `DataTableSchema.emptyAction`. |
| 13 | + * |
| 14 | + * ## The mechanism, and why it is not objectui#8908 |
| 15 | + * |
| 16 | + * `HeaderBarSchema.rightContent` is declared `SchemaNode` on BOTH published |
| 17 | + * faces — the interface in `@object-ui/types`, and its zod mirror, whose node |
| 18 | + * union carries a `z.number()` arm — so `0` is an authorable value for it. |
| 19 | + * `theValidatorAcceptsZero` below asserts that from the published validator |
| 20 | + * rather than asserting it in prose, because every row here is worthless if |
| 21 | + * the value is unreachable. |
| 22 | + * |
| 23 | + * The renderer guarded the slot with an `&&` chain whose left operand is the |
| 24 | + * raw slot value. With `rightContent: 0` that chain evaluates to the NUMBER |
| 25 | + * `0`, and React renders numbers — so the header painted a stray `"0"`. |
| 26 | + * |
| 27 | + * objectui#8908 repaired `toRenderableSchema` so a falsy primitive becomes |
| 28 | + * nothing rather than its `String` form. That repair does ⛔ NOT reach this |
| 29 | + * line and never could: `&&` short-circuits, so the bridge is not called at |
| 30 | + * all for a falsy value — the chain has already produced the raw `0` before |
| 31 | + * any bridge runs. Same symptom from the user's seat, different mechanism, |
| 32 | + * different fix. `theBridgeIsNotWhatFixesThis` pins that distinction directly |
| 33 | + * so a future reader cannot conclude the bridge covers this slot. |
| 34 | + * |
| 35 | + * ## Why `false` and `''` are present but are ⛔ NOT evidence |
| 36 | + * |
| 37 | + * React ignores `false` as a child, and `''` contributes no characters. Both |
| 38 | + * rows were GREEN before the repair and are green after it, so neither can |
| 39 | + * discriminate the two worlds. They sit in `rowsThatCannotDiscriminate` under |
| 40 | + * that name, kept only so a later reader does not "extend coverage" by adding |
| 41 | + * them as if they proved something. |
| 42 | + * |
| 43 | + * The rows that DO discriminate are the falsy NUMBERS, and only they: |
| 44 | + * `0` and `-0` (both of which React prints as the single character `0`) and |
| 45 | + * `NaN` (which prints the three characters `NaN`). |
| 46 | + * |
| 47 | + * ## Why the assertions read whole text rather than query for a node |
| 48 | + * |
| 49 | + * The leak is a bare text node with no element of its own — there is nothing |
| 50 | + * to `querySelector`. The instrument is therefore the header's entire |
| 51 | + * `textContent`, compared against the baseline the same harness produces for |
| 52 | + * `rightContent: undefined`. `theInstrumentSeesSlotText` is the lit control on |
| 53 | + * that instrument: with `rightContent: 42` the text MUST grow by `42`, so a |
| 54 | + * "no stray characters" row cannot pass because the instrument went blind. |
| 55 | + * |
| 56 | + * ## The `SidebarProvider` host |
| 57 | + * |
| 58 | + * `header-bar` renders `SidebarTrigger`, which calls `useSidebar()` and THROWS |
| 59 | + * without a provider. A caught throw renders markup that reads as a clean pass |
| 60 | + * for every row below, so `theHarnessRendersTheRealHeader` asserts the real |
| 61 | + * header is mounted rather than assuming it. |
| 62 | + */ |
| 63 | + |
| 64 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 65 | +import { render, cleanup } from '@testing-library/react'; |
| 66 | +import { ComponentRegistry } from '@object-ui/core'; |
| 67 | +// Registered at module scope, NOT in a `beforeAll`: there the cold transform is |
| 68 | +// billed to `hookTimeout`, which is narrower than the timeout it replaces |
| 69 | +// (objectui#3010 / #3021, and `object-ui/no-dynamic-import-in-test-hook`). |
| 70 | +import '../renderers'; |
| 71 | +import { SidebarProvider } from '../ui'; |
| 72 | +import { toRenderableSchema } from '@object-ui/react'; |
| 73 | +import { HeaderBarSchema as HeaderBarSchemaZod } from '@object-ui/types/zod'; |
| 74 | + |
| 75 | +afterEach(() => cleanup()); |
| 76 | + |
| 77 | +/** The header's own chrome text, contributed by `SidebarTrigger`'s sr-only label. */ |
| 78 | +const CHROME = 'Toggle Sidebar'; |
| 79 | + |
| 80 | +function renderRightContent(rightContent: unknown) { |
| 81 | + const C = ComponentRegistry.get('header-bar') as React.ComponentType<any>; |
| 82 | + const { container } = render( |
| 83 | + <SidebarProvider> |
| 84 | + <C schema={{ type: 'header-bar', title: 'H', rightContent }} /> |
| 85 | + </SidebarProvider>, |
| 86 | + ); |
| 87 | + const header = container.querySelector('header'); |
| 88 | + if (!header) throw new Error('no <header> — the harness did not mount the real renderer'); |
| 89 | + return header.textContent ?? ''; |
| 90 | +} |
| 91 | + |
| 92 | +/** `rightContent` omitted entirely: the chrome, and nothing else. */ |
| 93 | +function renderBaseline() { |
| 94 | + const C = ComponentRegistry.get('header-bar') as React.ComponentType<any>; |
| 95 | + const { container } = render( |
| 96 | + <SidebarProvider> |
| 97 | + <C schema={{ type: 'header-bar', title: 'H' }} /> |
| 98 | + </SidebarProvider>, |
| 99 | + ); |
| 100 | + const header = container.querySelector('header'); |
| 101 | + if (!header) throw new Error('no <header> — the harness did not mount the real renderer'); |
| 102 | + return header.textContent ?? ''; |
| 103 | +} |
| 104 | + |
| 105 | +describe('ui:header-bar rightContent numeric-falsy leak (objectui#9033)', () => { |
| 106 | + describe('harness controls', () => { |
| 107 | + it('theHarnessRendersTheRealHeader — a real <header>, not error-boundary markup', () => { |
| 108 | + // `useSidebar()` throws without `SidebarProvider`, and the caught throw is |
| 109 | + // clean markup that would make every "no stray characters" row below pass |
| 110 | + // for entirely the wrong reason. |
| 111 | + expect(renderBaseline()).toContain(CHROME); |
| 112 | + }); |
| 113 | + |
| 114 | + it('theBaselineIsTheChromeAlone', () => { |
| 115 | + expect(renderBaseline()).toBe(CHROME); |
| 116 | + }); |
| 117 | + |
| 118 | + it('theInstrumentSeesSlotText — LIT CONTROL: a truthy number DOES reach the text', () => { |
| 119 | + // Green in both worlds BY DESIGN. Without it, a slot that rendered |
| 120 | + // nothing at all — or a `textContent` read that stopped working — would |
| 121 | + // make every leak row below green while measuring nothing. |
| 122 | + expect(renderRightContent(42)).toBe(`${CHROME}42`); |
| 123 | + }); |
| 124 | + |
| 125 | + it('theValidatorAcceptsZero — the leaked value is AUTHORABLE, not hypothetical', () => { |
| 126 | + // The published zod face for this slot is `SchemaNodeSchema.optional()`, |
| 127 | + // and that union carries a `z.number()` arm. If this row ever goes red the |
| 128 | + // slot stopped admitting numbers and the rows below stopped being about |
| 129 | + // anything — ⛔ that is a declaration change, not a licence to delete them |
| 130 | + // (objectui#7105: node slots relax the RENDERER). |
| 131 | + const parsed = HeaderBarSchemaZod.safeParse({ type: 'header-bar', title: 'H', rightContent: 0 }); |
| 132 | + expect(parsed.success).toBe(true); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('the falsy NUMBERS — the only rows that discriminate', () => { |
| 137 | + it('rightContent: 0 renders the chrome and no stray "0"', () => { |
| 138 | + // RED before the repair: `"Toggle Sidebar0"`. |
| 139 | + expect(renderRightContent(0)).toBe(CHROME); |
| 140 | + }); |
| 141 | + |
| 142 | + it('rightContent: -0 renders the chrome and no stray "0"', () => { |
| 143 | + // React prints `-0` as the single character `0`, same as `0`. |
| 144 | + expect(renderRightContent(-0)).toBe(CHROME); |
| 145 | + }); |
| 146 | + |
| 147 | + it('rightContent: NaN renders the chrome and no stray "NaN"', () => { |
| 148 | + // The loudest row of the three: `NaN` paints three characters, not one. |
| 149 | + expect(renderRightContent(NaN)).toBe(CHROME); |
| 150 | + expect(renderRightContent(NaN)).not.toContain('NaN'); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + describe('live controls — unchanged by the repair, asserted in the SAME run', () => { |
| 155 | + it('rightContent: 42 still renders 42', () => { |
| 156 | + expect(renderRightContent(42)).toBe(`${CHROME}42`); |
| 157 | + }); |
| 158 | + |
| 159 | + it("rightContent: 'txt' still renders txt", () => { |
| 160 | + expect(renderRightContent('txt')).toBe(`${CHROME}txt`); |
| 161 | + }); |
| 162 | + |
| 163 | + it('rightContent: undefined renders the baseline chrome only', () => { |
| 164 | + expect(renderRightContent(undefined)).toBe(CHROME); |
| 165 | + }); |
| 166 | + |
| 167 | + it('an authored NODE still renders — the slot is still a node slot', () => { |
| 168 | + // The repair must not have narrowed the slot to primitives-only. |
| 169 | + expect(renderRightContent({ type: 'text', content: 'node' })).toContain('node'); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + describe('rowsThatCannotDiscriminate — ⛔ green before the repair too, kept only to say so', () => { |
| 174 | + it('rightContent: false was ALREADY correct — React ignores `false` as a child', () => { |
| 175 | + expect(renderRightContent(false)).toBe(CHROME); |
| 176 | + }); |
| 177 | + |
| 178 | + it("rightContent: '' was ALREADY correct — an empty string contributes no characters", () => { |
| 179 | + expect(renderRightContent('')).toBe(CHROME); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + describe('the mechanism, pinned so it is not confused with objectui#8908', () => { |
| 184 | + it('theBridgeIsNotWhatFixesThis — `toRenderableSchema` already mapped 0 onto nothing', () => { |
| 185 | + // The bridge has been behaviour-preserving for falsy primitives since |
| 186 | + // objectui#8908. It was ALREADY correct while this slot leaked, because |
| 187 | + // `&&` short-circuits and never calls it for a falsy value. So a reader |
| 188 | + // who finds the bridge correct must ⛔ not conclude this slot is covered: |
| 189 | + // the guard shape is what covers it. |
| 190 | + expect(toRenderableSchema(0)).toBeUndefined(); |
| 191 | + expect(toRenderableSchema(NaN)).toBeUndefined(); |
| 192 | + expect(toRenderableSchema(42)).toBe('42'); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments