|
| 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 | + * objectui#11365 — `input-otp` draws the separator its docs page and two |
| 11 | + * catalog entries author. |
| 12 | + * |
| 13 | + * The docs page's "With Separator" section shows |
| 14 | + * `components-form-input-otp/with-visual-separator`, and |
| 15 | + * `components-form-input-otp/verification-form` authors the same key on an |
| 16 | + * `input-otp` nested in a `flex` node's `properties` bag. Before this card no |
| 17 | + * type declared `separator` on `InputOTPSchema` and the renderer read nothing |
| 18 | + * for it, so both entries rendered ZERO `role="separator"` elements through the |
| 19 | + * real `SchemaRenderer`. Triage ruled the enforce arm (ADR-0049): declare the |
| 20 | + * key on both faces and draw the primitive's `InputOTPSeparator`. |
| 21 | + * |
| 22 | + * ## What each block pins |
| 23 | + * |
| 24 | + * - The render block counts the `role="separator"` elements the renderer |
| 25 | + * actually emitted, on the two authoring entries and on the entry that does |
| 26 | + * NOT author the key (the control: without `separator` nothing changes). |
| 27 | + * - The parse block asks both faces. `safeValidateSchema` is `.passthrough()`, |
| 28 | + * so a green there says nothing about an UNDECLARED key; the strict authoring |
| 29 | + * face closes unknown keys, so a green there is the declaration's evidence. |
| 30 | + * The value probe (a non-boolean `separator`) is what proves the declared |
| 31 | + * member judges its value rather than merely being tolerated. |
| 32 | + */ |
| 33 | +import { describe, it, expect } from 'vitest'; |
| 34 | +import { render } from '@testing-library/react'; |
| 35 | +// Module scope, not a hook: registering the renderers is an unbounded module |
| 36 | +// load (AGENTS.md, test discipline — flaky tests: find the race). |
| 37 | +import '@object-ui/components'; |
| 38 | +import { ComponentRegistry } from '@object-ui/core'; |
| 39 | +import { SchemaRenderer, toRenderableSchema } from '@object-ui/react'; |
| 40 | +import { safeValidateSchema, StrictAnyComponentSchema } from '@object-ui/types/zod'; |
| 41 | +import { getExample } from '../src/index.js'; |
| 42 | + |
| 43 | +const AUTHORING = [ |
| 44 | + 'components-form-input-otp/with-visual-separator', |
| 45 | + 'components-form-input-otp/verification-form', |
| 46 | +] as const; |
| 47 | + |
| 48 | +/** Every issue as `code path: message`, so a red run says what broke. */ |
| 49 | +function issuesOf(result: { success: boolean; error?: { issues: Array<{ code: string; path: PropertyKey[]; message: string }> } }): string[] { |
| 50 | + return result.success || !result.error |
| 51 | + ? [] |
| 52 | + : result.error.issues.map((i) => `${i.code} ${i.path.map(String).join('.')}: ${i.message}`); |
| 53 | +} |
| 54 | + |
| 55 | +function renderNode(schema: unknown): HTMLElement { |
| 56 | + const { container } = render(<SchemaRenderer schema={toRenderableSchema(schema as never) as never} />); |
| 57 | + return container; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * The slot count of each `InputOTPGroup`, in document order. The container's |
| 62 | + * other children are the separator and the library's own wrapper around the |
| 63 | + * hidden `<input>`, which is not a group. |
| 64 | + */ |
| 65 | +function groupSizes(container: HTMLElement): number[] { |
| 66 | + const host = container.querySelector('[data-input-otp-container]'); |
| 67 | + expect(host, 'no input-otp container rendered: nothing below would be a reading').not.toBeNull(); |
| 68 | + return Array.from((host as Element).children) |
| 69 | + .filter((el) => el.getAttribute('role') !== 'separator' && !el.querySelector('input')) |
| 70 | + .map((group) => group.children.length); |
| 71 | +} |
| 72 | + |
| 73 | +describe('objectui#11365 — the authored `separator` draws one InputOTPSeparator', () => { |
| 74 | + it.each(AUTHORING)('%s renders exactly one role="separator"', (id) => { |
| 75 | + const container = renderNode(getExample(id).schema); |
| 76 | + expect(container.querySelector('[data-input-otp-container]'), 'no input-otp rendered').not.toBeNull(); |
| 77 | + expect(container.querySelectorAll('[role="separator"]')).toHaveLength(1); |
| 78 | + }); |
| 79 | + |
| 80 | + it('the separator splits the six slots at the midpoint, between two groups', () => { |
| 81 | + const container = renderNode(getExample('components-form-input-otp/with-visual-separator').schema); |
| 82 | + expect(groupSizes(container)).toEqual([3, 3]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('an odd `length` puts the extra slot in the first group; one slot draws no separator', () => { |
| 86 | + const five = renderNode({ type: 'input-otp', length: 5, separator: true }); |
| 87 | + expect(groupSizes(five)).toEqual([3, 2]); |
| 88 | + expect(five.querySelectorAll('[role="separator"]')).toHaveLength(1); |
| 89 | + |
| 90 | + const one = renderNode({ type: 'input-otp', length: 1, separator: true }); |
| 91 | + expect(groupSizes(one)).toEqual([1]); |
| 92 | + expect(one.querySelectorAll('[role="separator"]')).toHaveLength(0); |
| 93 | + }); |
| 94 | + |
| 95 | + it('the control: the entry without `separator` draws none, in one group', () => { |
| 96 | + const container = renderNode(getExample('components-form-input-otp/6-digit-otp').schema); |
| 97 | + expect(container.querySelectorAll('[role="separator"]')).toHaveLength(0); |
| 98 | + expect(groupSizes(container)).toEqual([6]); |
| 99 | + }); |
| 100 | + |
| 101 | + it('the registration publishes `separator` as a boolean input', () => { |
| 102 | + // `inputs` is the html/jsx page tier's prop whitelist: without this row, |
| 103 | + // `separator` on an `input-otp` tag draws an `unknown-prop` warning on a |
| 104 | + // key the renderer honours (measured once on objectui#11365's PR). |
| 105 | + const inputs = ComponentRegistry.getMeta('input-otp')?.inputs ?? []; |
| 106 | + expect(inputs.filter((i) => i.name === 'separator')).toEqual([ |
| 107 | + expect.objectContaining({ name: 'separator', type: 'boolean' }), |
| 108 | + ]); |
| 109 | + }); |
| 110 | + |
| 111 | + it('`separator: false` renders the same markup as no `separator` at all', () => { |
| 112 | + const absent = renderNode({ type: 'input-otp', length: 6 }).innerHTML; |
| 113 | + const off = renderNode({ type: 'input-otp', length: 6, separator: false }).innerHTML; |
| 114 | + expect(off).toBe(absent); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe('objectui#11365 — both authoring entries validate, on both faces', () => { |
| 119 | + it.each(AUTHORING)('%s validates under safeValidateSchema', (id) => { |
| 120 | + expect(issuesOf(safeValidateSchema(getExample(id).schema))).toEqual([]); |
| 121 | + }); |
| 122 | + |
| 123 | + it.each(AUTHORING)('%s validates under the strict authoring face (no unrecognized `separator`)', (id) => { |
| 124 | + expect(issuesOf(StrictAnyComponentSchema.safeParse(getExample(id).schema))).toEqual([]); |
| 125 | + }); |
| 126 | + |
| 127 | + it('a non-boolean `separator` is refused by value, at its path, in a bag child too', () => { |
| 128 | + // Counter-probe: a passthrough hole would accept this; a declared boolean |
| 129 | + // member refuses it with `invalid_type` at the key. |
| 130 | + const flat = safeValidateSchema({ type: 'input-otp', separator: 'yes' }); |
| 131 | + expect(issuesOf(flat).map((s) => s.split(':')[0])).toEqual(['invalid_type separator']); |
| 132 | + |
| 133 | + const nested = safeValidateSchema({ |
| 134 | + type: 'flex', |
| 135 | + properties: { children: [{ type: 'input-otp', separator: 'yes' }] }, |
| 136 | + }); |
| 137 | + expect(nested.success).toBe(false); |
| 138 | + expect(issuesOf(nested).map((s) => s.split(':')[0])).toEqual(['invalid_type properties.children.0.separator']); |
| 139 | + }); |
| 140 | +}); |
0 commit comments