|
| 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#11465 — `SidebarSchema.variant` takes the `sidebar` registration's |
| 11 | + * own enum, held against the LIVE registration rather than a copied list. |
| 12 | + * |
| 13 | + * Before this card the two disagreed: the registration offered shadcn's |
| 14 | + * `sidebar` / `floating` / `inset`, while both `@object-ui/types` faces |
| 15 | + * declared `default` / `bordered` / `floating`. So `objectui validate` refused |
| 16 | + * two of the three values the registration offers, and accepted two values |
| 17 | + * shadcn does not know (they drew exactly what `sidebar` draws). This file |
| 18 | + * reads the registration's `variant` input and asks both validator faces |
| 19 | + * about every value in it, so a registration that grows a value the schema |
| 20 | + * does not take turns this red. |
| 21 | + * |
| 22 | + * The render half holds what the declaration says about the three values, |
| 23 | + * measured through the real `SchemaRenderer` and registry: on the collapsible |
| 24 | + * form each draws differently, and on the in-flow form (`collapsible: false`) |
| 25 | + * shadcn ignores `variant`, so every value draws the same column. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 29 | +import { render, cleanup } from '@testing-library/react'; |
| 30 | +import React from 'react'; |
| 31 | +import { ComponentRegistry } from '@object-ui/core'; |
| 32 | +import { SchemaRenderer } from '@object-ui/react'; |
| 33 | +import { safeValidateSchema, StrictAnyComponentSchema } from '@object-ui/types/zod'; |
| 34 | +import '../../renderers'; |
| 35 | + |
| 36 | +afterEach(cleanup); |
| 37 | + |
| 38 | +const ITEMS = [{ type: 'text', content: 'Dashboard' }]; |
| 39 | + |
| 40 | +type Parse = { success: boolean; error?: { issues: { path: PropertyKey[] }[] } }; |
| 41 | +const strict = StrictAnyComponentSchema as unknown as { safeParse: (v: unknown) => Parse }; |
| 42 | +const refusedAt = (result: Parse) => (result.success ? [] : (result.error?.issues ?? []).map((i) => i.path.join('.'))); |
| 43 | + |
| 44 | +/** The `variant` enum the live `sidebar` registration offers. */ |
| 45 | +function registeredVariants(): string[] { |
| 46 | + const inputs = (ComponentRegistry.getMeta('sidebar')?.inputs ?? []) as { name: string; enum?: unknown[] }[]; |
| 47 | + const variant = inputs.find((i) => i.name === 'variant'); |
| 48 | + if (!variant || !Array.isArray(variant.enum)) throw new Error('the `sidebar` registration offers no `variant` enum'); |
| 49 | + return variant.enum.map(String); |
| 50 | +} |
| 51 | + |
| 52 | +/** The markup a node draws in a bare host (the node mounts its own provider there). */ |
| 53 | +const markup = (node: object) => { |
| 54 | + const { container } = render(<SchemaRenderer schema={node as never} />); |
| 55 | + const html = container.innerHTML; |
| 56 | + cleanup(); |
| 57 | + return html; |
| 58 | +}; |
| 59 | + |
| 60 | +describe('`SidebarSchema.variant` is the registration\'s enum (objectui#11465)', () => { |
| 61 | + it('the registration offers a non-empty enum, without the two retired values', () => { |
| 62 | + const offered = registeredVariants(); |
| 63 | + expect(offered.length).toBeGreaterThan(0); |
| 64 | + expect(offered).not.toContain('default'); |
| 65 | + expect(offered).not.toContain('bordered'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('every value the registration offers validates on the strict face and the tolerant face', () => { |
| 69 | + const refused = registeredVariants().flatMap((variant) => { |
| 70 | + const node = { type: 'sidebar', variant, children: ITEMS }; |
| 71 | + return [ |
| 72 | + ...refusedAt(strict.safeParse(node)).map((p) => `strict ${variant} @${p}`), |
| 73 | + ...refusedAt(safeValidateSchema(node) as Parse).map((p) => `tolerant ${variant} @${p}`), |
| 74 | + ]; |
| 75 | + }); |
| 76 | + expect(refused).toEqual([]); |
| 77 | + }); |
| 78 | + |
| 79 | + it.each(['default', 'bordered'])('the retired `%s` is refused at `variant` on both faces', (variant) => { |
| 80 | + const node = { type: 'sidebar', variant, children: ITEMS }; |
| 81 | + expect(refusedAt(strict.safeParse(node))).toEqual(['variant']); |
| 82 | + expect(refusedAt(safeValidateSchema(node) as Parse)).toEqual(['variant']); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('what each variant draws, through the real SchemaRenderer (objectui#11465)', () => { |
| 87 | + it('on the collapsible form, every offered value draws differently, and `sidebar` is the default', () => { |
| 88 | + const base = { type: 'sidebar', children: ITEMS }; |
| 89 | + const drawn = registeredVariants().map((variant) => markup({ ...base, variant })); |
| 90 | + expect(new Set(drawn).size).toBe(drawn.length); |
| 91 | + expect(markup({ ...base, variant: 'sidebar' })).toBe(markup(base)); |
| 92 | + }); |
| 93 | + |
| 94 | + it('on the in-flow form (`collapsible: false`), every value draws the same column', () => { |
| 95 | + const base = { type: 'sidebar', collapsible: false, children: ITEMS }; |
| 96 | + const absent = markup(base); |
| 97 | + for (const variant of registeredVariants()) { |
| 98 | + expect(markup({ ...base, variant }), variant).toBe(absent); |
| 99 | + } |
| 100 | + }); |
| 101 | +}); |
0 commit comments