|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectui#3229 — the Page block inspector's conditional-visibility control |
| 5 | + * must author `visibleWhen`, and must SAY "visible when" while doing it. |
| 6 | + * |
| 7 | + * The control used to read/write `block.hidden`. `PageComponentSchema` |
| 8 | + * (`ui/page.zod.ts`) is `.strict()` and has no `hidden` key at all, so the |
| 9 | + * designer — a PRODUCER — was emitting drafts guaranteed to be rejected on |
| 10 | + * save, with a parse error naming a key the author never typed. The canonical |
| 11 | + * key is `visibleWhen` (ADR-0089); `visibility` survives as a deprecated alias. |
| 12 | + * |
| 13 | + * ## Why direction, not just the key name, is pinned here |
| 14 | + * |
| 15 | + * `hidden` and `visibleWhen` are INVERSES (`hidden` true ⇒ gone, |
| 16 | + * `visibleWhen` true ⇒ shown). Renaming the key while leaving "hidden"-flavoured |
| 17 | + * copy on screen would have produced metadata that PARSES and means the |
| 18 | + * opposite — the objectui#3276 class, which #3257's guard is structurally blind |
| 19 | + * to because it only asks whether a draft parses. So these tests assert the |
| 20 | + * rendered OUTCOME of the inspector's own output, in both directions, rather |
| 21 | + * than just `expect(patch).toHaveProperty('visibleWhen')`. |
| 22 | + * |
| 23 | + * FIXTURE DISCIPLINE (objectui#3216's method): no envelope is hand-written. |
| 24 | + * Every fixture is the AUTHORED input fed through `PageSchema.parse`, so a |
| 25 | + * fixture cannot drift from the spec — `visibleWhen` is `ExpressionInputSchema`, |
| 26 | + * which normalizes a bare string into `{ dialect: 'cel', source }`. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; |
| 30 | +import { render, screen, fireEvent, cleanup } from '@testing-library/react'; |
| 31 | +import { PageSchema } from '@objectstack/spec/ui'; |
| 32 | +import { ComponentRegistry } from '@object-ui/core'; |
| 33 | +import { SchemaRenderer, PredicateScopeProvider } from '@object-ui/react'; |
| 34 | +import { PageBlockInspector } from './PageBlockInspector'; |
| 35 | + |
| 36 | +afterEach(cleanup); |
| 37 | + |
| 38 | +/** Selection id for the single block in the fixture page. */ |
| 39 | +const BLOCK_PATH = 'regions[0].components[0]'; |
| 40 | + |
| 41 | +/** |
| 42 | + * Author a page the way a user does, parse it with the spec, and hand the |
| 43 | + * RESULT to the inspector — exactly what the metadata editor loads. |
| 44 | + */ |
| 45 | +function pageDraft(block: Record<string, unknown>): Record<string, unknown> { |
| 46 | + return PageSchema.parse({ |
| 47 | + name: 'home', |
| 48 | + label: 'Home', |
| 49 | + type: 'home', |
| 50 | + template: 'default', |
| 51 | + regions: [{ name: 'main', components: [{ type: 'text', id: 'b1', ...block }] }], |
| 52 | + }) as unknown as Record<string, unknown>; |
| 53 | +} |
| 54 | + |
| 55 | +function renderInspector( |
| 56 | + draft: Record<string, unknown>, |
| 57 | + { locale = 'en-US', onPatch = vi.fn() }: { locale?: string; onPatch?: ReturnType<typeof vi.fn> } = {}, |
| 58 | +) { |
| 59 | + render( |
| 60 | + <PageBlockInspector |
| 61 | + type="page" |
| 62 | + name="home" |
| 63 | + draft={draft} |
| 64 | + selection={{ kind: 'block', id: BLOCK_PATH }} |
| 65 | + onPatch={onPatch} |
| 66 | + onClearSelection={() => {}} |
| 67 | + readOnly={false} |
| 68 | + locale={locale as never} |
| 69 | + />, |
| 70 | + ); |
| 71 | + return onPatch; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * The block as the inspector last wrote it. `onPatch` carries a SHALLOW patch |
| 76 | + * (`{ regions }`), not the whole draft. |
| 77 | + */ |
| 78 | +function committedBlock(onPatch: ReturnType<typeof vi.fn>): Record<string, unknown> { |
| 79 | + const patch = onPatch.mock.calls.at(-1)![0] as any; |
| 80 | + return patch.regions[0].components[0] as Record<string, unknown>; |
| 81 | +} |
| 82 | + |
| 83 | +/** What the editor would actually save: the shallow patch applied to the draft. */ |
| 84 | +function savedDraft( |
| 85 | + draft: Record<string, unknown>, |
| 86 | + onPatch: ReturnType<typeof vi.fn>, |
| 87 | +): Record<string, unknown> { |
| 88 | + return { ...draft, ...(onPatch.mock.calls.at(-1)![0] as Record<string, unknown>) }; |
| 89 | +} |
| 90 | + |
| 91 | +/** The no-code builder's value box for the single parsed row. */ |
| 92 | +const rowValueInput = () => screen.getByPlaceholderText('value') as HTMLInputElement; |
| 93 | +/** The raw-expression editor (CelPredicateField renders a combobox TEXTAREA). */ |
| 94 | +const rawEditor = () => |
| 95 | + screen.getAllByRole('combobox').find((el) => el.tagName === 'TEXTAREA') as HTMLTextAreaElement; |
| 96 | + |
| 97 | +/* ───────────────────────── the key that is committed ───────────────────── */ |
| 98 | + |
| 99 | +describe('PageBlockInspector — conditional visibility authors `visibleWhen` (#3229)', () => { |
| 100 | + it('reads the `source` of a persisted envelope into the control', () => { |
| 101 | + const draft = pageDraft({ visibleWhen: 'record.amount > 10' }); |
| 102 | + // Pin what the platform actually stores, so this fails loudly if the spec |
| 103 | + // stops normalizing rather than passing on a stale assumption. |
| 104 | + expect((draft as any).regions[0].components[0].visibleWhen).toEqual({ |
| 105 | + dialect: 'cel', |
| 106 | + source: 'record.amount > 10', |
| 107 | + }); |
| 108 | + |
| 109 | + renderInspector(draft); |
| 110 | + |
| 111 | + // The builder adopted the predicate: its value box carries `10` and the |
| 112 | + // compiled preview echoes the whole thing. |
| 113 | + expect(rowValueInput().value).toBe('10'); |
| 114 | + expect(screen.getByText('record.amount > 10')).toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('commits `visibleWhen` — and never `hidden`', () => { |
| 118 | + const onPatch = renderInspector(pageDraft({ visibleWhen: 'record.amount > 10' })); |
| 119 | + |
| 120 | + fireEvent.change(rowValueInput(), { target: { value: '20' } }); |
| 121 | + |
| 122 | + const block = committedBlock(onPatch); |
| 123 | + expect(block).toHaveProperty('visibleWhen'); |
| 124 | + // The whole point of the issue: the designer must not type a key the |
| 125 | + // `.strict()` page schema does not have. |
| 126 | + expect(block).not.toHaveProperty('hidden'); |
| 127 | + expect(block.visibleWhen).toEqual({ dialect: 'cel', source: 'record.amount > 20' }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('the committed draft PARSES — the shape the old `hidden` key could not', () => { |
| 131 | + const draft = pageDraft({ visibleWhen: 'record.amount > 10' }); |
| 132 | + const onPatch = renderInspector(draft); |
| 133 | + fireEvent.change(rowValueInput(), { target: { value: '20' } }); |
| 134 | + |
| 135 | + // What the author would be saving. Before #3229 this threw |
| 136 | + // `unrecognized_keys … ["hidden"]`. |
| 137 | + expect(() => PageSchema.parse(savedDraft(draft, onPatch))).not.toThrow(); |
| 138 | + |
| 139 | + // And the counter-proof that the strictness under test is real. |
| 140 | + expect(() => pageDraft({ hidden: 'record.amount > 20' })).toThrow(/hidden/); |
| 141 | + }); |
| 142 | + |
| 143 | + it('authoring from scratch commits the bare-string shorthand under `visibleWhen`', () => { |
| 144 | + const draft = pageDraft({}); |
| 145 | + expect((draft as any).regions[0].components[0].visibleWhen).toBeUndefined(); |
| 146 | + const onPatch = renderInspector(draft); |
| 147 | + |
| 148 | + // No prior value ⇒ the builder opens in row mode; switch to the raw editor |
| 149 | + // to author a predicate without driving Radix selects. |
| 150 | + fireEvent.click(screen.getByText('Expression')); |
| 151 | + fireEvent.change(rawEditor(), { target: { value: 'record.amount > 20' } }); |
| 152 | + |
| 153 | + const block = committedBlock(onPatch); |
| 154 | + expect(block.visibleWhen).toBe('record.amount > 20'); |
| 155 | + expect(block).not.toHaveProperty('hidden'); |
| 156 | + // The spec's pipe normalizes the shorthand to exactly the envelope. |
| 157 | + expect((PageSchema.parse(savedDraft(draft, onPatch)) as any).regions[0].components[0].visibleWhen) |
| 158 | + .toEqual({ dialect: 'cel', source: 'record.amount > 20' }); |
| 159 | + }); |
| 160 | + |
| 161 | + it('preserves a non-`cel` dialect and `meta` across an edit (#3218 write rule)', () => { |
| 162 | + const draft = pageDraft({ |
| 163 | + visibleWhen: { dialect: 'cel', source: 'record.amount > 10', meta: { rationale: 'Large deals only' } }, |
| 164 | + }); |
| 165 | + const onPatch = renderInspector(draft); |
| 166 | + |
| 167 | + fireEvent.change(rowValueInput(), { target: { value: '20' } }); |
| 168 | + |
| 169 | + expect(committedBlock(onPatch).visibleWhen).toEqual({ |
| 170 | + dialect: 'cel', |
| 171 | + source: 'record.amount > 20', |
| 172 | + meta: { rationale: 'Large deals only' }, |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + it('clears the predicate to `undefined` when the author empties it', () => { |
| 177 | + const onPatch = renderInspector(pageDraft({ visibleWhen: 'record.amount > 10' })); |
| 178 | + |
| 179 | + fireEvent.click(screen.getByLabelText('Remove condition')); |
| 180 | + |
| 181 | + expect(committedBlock(onPatch).visibleWhen).toBeUndefined(); |
| 182 | + }); |
| 183 | +}); |
| 184 | + |
| 185 | +/* ─────────────────────────── the UI copy (semantics) ───────────────────── */ |
| 186 | + |
| 187 | +describe('PageBlockInspector — the label says "visible", not "hidden" (#3229)', () => { |
| 188 | + it('labels the control "Visible when" in English', () => { |
| 189 | + renderInspector(pageDraft({})); |
| 190 | + expect(screen.getByText(/Visible when/i)).toBeInTheDocument(); |
| 191 | + // A "hide when" label over a show-when-truthy key is how authors come to |
| 192 | + // write the predicate backwards (the #3276 defect class). |
| 193 | + expect(screen.queryByText(/^Hidden/i)).not.toBeInTheDocument(); |
| 194 | + }); |
| 195 | + |
| 196 | + it('labels the control 显示条件 in Chinese', () => { |
| 197 | + renderInspector(pageDraft({}), { locale: 'zh-CN' }); |
| 198 | + expect(screen.getByText(/显示条件/)).toBeInTheDocument(); |
| 199 | + expect(screen.queryByText(/隐藏条件/)).not.toBeInTheDocument(); |
| 200 | + }); |
| 201 | +}); |
| 202 | + |
| 203 | +/* ──────────────────────────────── DIRECTION ────────────────────────────── */ |
| 204 | + |
| 205 | +/** |
| 206 | + * The acceptance criterion the issue calls a hard requirement: what the |
| 207 | + * inspector writes must MEAN "visible when". These render the inspector's own |
| 208 | + * committed block through `SchemaRenderer` — the renderer `PageBlockCanvas` |
| 209 | + * hands blocks to — and assert both directions. |
| 210 | + */ |
| 211 | +describe('PageBlockInspector — `visibleWhen` direction is show-when-truthy (#3229)', () => { |
| 212 | + const Block = () => <div data-testid="block">Block</div>; |
| 213 | + |
| 214 | + beforeEach(() => { |
| 215 | + ComponentRegistry.register('text', Block); |
| 216 | + }); |
| 217 | + afterEach(() => { |
| 218 | + ComponentRegistry.unregister?.('text'); |
| 219 | + }); |
| 220 | + |
| 221 | + /** Author `record.amount > 20` through the inspector and return the block. */ |
| 222 | + function authoredBlock(): Record<string, unknown> { |
| 223 | + const onPatch = renderInspector(pageDraft({ visibleWhen: 'record.amount > 10' })); |
| 224 | + fireEvent.change(rowValueInput(), { target: { value: '20' } }); |
| 225 | + const block = committedBlock(onPatch); |
| 226 | + cleanup(); |
| 227 | + return block; |
| 228 | + } |
| 229 | + |
| 230 | + function renderBlock(block: Record<string, unknown>, record: Record<string, unknown>) { |
| 231 | + return render( |
| 232 | + <PredicateScopeProvider scope={{ record }}> |
| 233 | + <SchemaRenderer schema={block as never} /> |
| 234 | + </PredicateScopeProvider>, |
| 235 | + ); |
| 236 | + } |
| 237 | + |
| 238 | + it('renders the block when the authored predicate is TRUE', () => { |
| 239 | + renderBlock(authoredBlock(), { amount: 30 }); |
| 240 | + expect(screen.getByTestId('block')).toBeInTheDocument(); |
| 241 | + }); |
| 242 | + |
| 243 | + it('does NOT render the block when the authored predicate is FALSE', () => { |
| 244 | + const { container } = renderBlock(authoredBlock(), { amount: 5 }); |
| 245 | + expect(screen.queryByTestId('block')).not.toBeInTheDocument(); |
| 246 | + expect(container.innerHTML).toBe(''); |
| 247 | + }); |
| 248 | + |
| 249 | + it('renders the block when there is no predicate at all', () => { |
| 250 | + renderBlock({ type: 'text', id: 'b1' }, { amount: 5 }); |
| 251 | + expect(screen.getByTestId('block')).toBeInTheDocument(); |
| 252 | + }); |
| 253 | +}); |
0 commit comments