|
| 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 | + * `object-master-detail-form.title`, `.submitText` and `.cancelText` through |
| 9 | + * the manifest's PUBLIC door (objectui#10935). |
| 10 | + * |
| 11 | + * `@objectstack/spec` types the three as `I18nLabel`: a plain string or an |
| 12 | + * inline per-locale map. `MasterDetailForm` resolves a map against the active |
| 13 | + * UI language (`MasterDetailForm.i18nLabels.test.tsx` pins the render), so the |
| 14 | + * registration declares both arms, `['string', 'object']`, as the |
| 15 | + * `ComponentInput` docblock asks of a key whose render site resolves the map. |
| 16 | + * While the three declared only `'string'`, `validateTree` reported |
| 17 | + * `type-mismatch` on a legal map: the manifest gate contradicting the spec row, |
| 18 | + * the validator and the renderer on the same write. |
| 19 | + * |
| 20 | + * The door is the production one, `ComponentRegistry.getPublicConfigs()` → |
| 21 | + * `manifestFromConfigs` → `validateTree`, where the block's key is the bare |
| 22 | + * `object-master-detail-form`. (`getAllConfigs()` keys it by its namespace, |
| 23 | + * `plugin-form:object-master-detail-form`, so a bare-typed node there reads as |
| 24 | + * `unknown-component` and every "no type-mismatch" row would pass vacuously.) |
| 25 | + */ |
| 26 | +import { describe, it, expect } from 'vitest'; |
| 27 | +import { ComponentRegistry } from '@object-ui/core'; |
| 28 | +import { manifestFromConfigs, validateTree } from '@object-ui/sdui-parser'; |
| 29 | +import type { Diagnostic, SchemaElement } from '@object-ui/sdui-parser'; |
| 30 | +import '@object-ui/components'; |
| 31 | +import '../register-plugins'; |
| 32 | + |
| 33 | +const manifest = manifestFromConfigs( |
| 34 | + ComponentRegistry.getPublicConfigs() as unknown as Parameters<typeof manifestFromConfigs>[0], |
| 35 | +); |
| 36 | + |
| 37 | +const BLOCK = 'object-master-detail-form'; |
| 38 | +const LABELS = ['title', 'submitText', 'cancelText'] as const; |
| 39 | + |
| 40 | +/** `en` first, as in the render pin. */ |
| 41 | +const MAP = { en: 'Purchase order', 'zh-CN': '采购单' }; |
| 42 | + |
| 43 | +const diagnose = (props: Record<string, unknown>): Diagnostic[] => |
| 44 | + validateTree( |
| 45 | + { |
| 46 | + type: BLOCK, |
| 47 | + objectName: 'po', |
| 48 | + details: [{ childObject: 'po_line', relationshipField: 'po' }], |
| 49 | + ...props, |
| 50 | + } as unknown as SchemaElement, |
| 51 | + manifest, |
| 52 | + ).diagnostics; |
| 53 | + |
| 54 | +/** The `type-mismatch` diagnostics that name one prop. */ |
| 55 | +const mismatchesOn = (props: Record<string, unknown>, key: string): Diagnostic[] => |
| 56 | + diagnose(props).filter((d) => d.code === 'type-mismatch' && d.message.includes(key)); |
| 57 | + |
| 58 | +describe('object-master-detail-form — the I18nLabel labels at the manifest public door (objectui#10935)', () => { |
| 59 | + it('the block is in the public manifest under its bare type, and a minimal node is clean', () => { |
| 60 | + // Reachability before absence: an unknown block would satisfy every |
| 61 | + // "no type-mismatch" row below with `unknown-component` instead. |
| 62 | + expect(manifest.components[BLOCK], `${BLOCK} is not in the public manifest`).toBeDefined(); |
| 63 | + expect(diagnose({})).toEqual([]); |
| 64 | + }); |
| 65 | + |
| 66 | + it.each(LABELS)('`%s` declares the string arm and the locale-map arm', (key) => { |
| 67 | + const input = manifest.components[BLOCK].inputs.find((i) => i.name === key); |
| 68 | + expect(input, `${key} is not a declared input`).toBeDefined(); |
| 69 | + const arms = Array.isArray(input?.type) ? [...input.type].sort() : [input?.type]; |
| 70 | + expect(arms).toEqual(['object', 'string']); |
| 71 | + }); |
| 72 | + |
| 73 | + it.each(LABELS)('a locale map on `%s` is not reported', (key) => { |
| 74 | + expect(mismatchesOn({ [key]: MAP }, key)).toEqual([]); |
| 75 | + }); |
| 76 | + |
| 77 | + it.each(LABELS)('CONTROL: a plain string on `%s` is not reported', (key) => { |
| 78 | + expect(mismatchesOn({ [key]: 'Purchase order' }, key)).toEqual([]); |
| 79 | + }); |
| 80 | + |
| 81 | + it.each(LABELS)('CONTROL: a value matching neither arm on `%s` is still reported', (key) => { |
| 82 | + // The union is not a way out of the gate: a number is neither a string nor |
| 83 | + // a map, and must still be a `type-mismatch`. |
| 84 | + expect(mismatchesOn({ [key]: 42 }, key)).toHaveLength(1); |
| 85 | + }); |
| 86 | +}); |
0 commit comments