|
| 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 | + * `FieldWidgetComponentProps` is a CLOSED contract (objectui#3221). |
| 11 | + * |
| 12 | + * It used to end in `[key: string]: any`, which made every check over it |
| 13 | + * vacuous: a type claiming to have every key can never be reported as missing |
| 14 | + * one (objectstack#4075). Three consequences, one test each below: |
| 15 | + * |
| 16 | + * 1. a key the type does not declare is a compile error, not a silent `any` |
| 17 | + * — which is what makes the `error` / `errorMessage` divergence |
| 18 | + * (objectui#3222) decidable by the compiler at all; |
| 19 | + * 2. a MISSPELLED prop (`readOnly` for `readonly`) is rejected; |
| 20 | + * 3. the pass-through keys hosts genuinely forward still type-check, and |
| 21 | + * still reach the rendered control at runtime — closing the type must not |
| 22 | + * have been paid for by dropping real behaviour. |
| 23 | + * |
| 24 | + * (1) and (2) are compile-time: a violation fails `pnpm --filter |
| 25 | + * @object-ui/fields type-check`, not this run. `@ts-expect-error` inverts |
| 26 | + * that — the line fails to compile if the error it expects ever STOPS |
| 27 | + * happening, i.e. if the index signature comes back. |
| 28 | + */ |
| 29 | + |
| 30 | +import { describe, it, expect } from 'vitest'; |
| 31 | +import { render, screen } from '@testing-library/react'; |
| 32 | + |
| 33 | +import { TextField } from '../widgets/TextField'; |
| 34 | +import { BooleanField } from '../widgets/BooleanField'; |
| 35 | +import type { FieldWidgetComponentProps } from '../widgets/types'; |
| 36 | +import type { FieldMetadata } from '@object-ui/types'; |
| 37 | + |
| 38 | +type Assert<T extends true> = T; |
| 39 | +type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false; |
| 40 | +type Extends<A, B> = [A] extends [B] ? true : false; |
| 41 | + |
| 42 | +const field = { name: 'title', label: 'Title', type: 'text' } as unknown as FieldMetadata; |
| 43 | + |
| 44 | +describe('FieldWidgetComponentProps is closed (objectui#3221)', () => { |
| 45 | + it('has no `[key: string]` index signature', () => { |
| 46 | + // The single assertion the whole change turns on. While the index |
| 47 | + // signature existed this was `true`, and every check below was vacuous. |
| 48 | + type _NotOpen = Assert<Equal<Extends<string, keyof FieldWidgetComponentProps>, false>>; |
| 49 | + expect(true).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('rejects keys it does not declare', () => { |
| 53 | + const props = {} as FieldWidgetComponentProps<string>; |
| 54 | + |
| 55 | + // The spec's `FieldWidgetPropsSchema` declares both; this type declares |
| 56 | + // neither. Reading them used to give `any` and `undefined` forever. |
| 57 | + // Whether to ADD them is objectui#3222 — deliberately not decided here. |
| 58 | + // @ts-expect-error `required` is not part of this contract |
| 59 | + void props.required; |
| 60 | + // @ts-expect-error `error` is not part of this contract — this type's slot is `errorMessage` |
| 61 | + void props.error; |
| 62 | + |
| 63 | + // …and the class of bug the index signature hid best: a typo. |
| 64 | + // @ts-expect-error the prop is `readonly`, not React's DOM `readOnly` |
| 65 | + void props.readOnly; |
| 66 | + // @ts-expect-error the prop is `onChange` |
| 67 | + void props.onchange; |
| 68 | + |
| 69 | + expect(true).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it('still accepts the pass-through keys hosts actually forward', () => { |
| 73 | + // Closing the type is only correct if the real call sites still compile. |
| 74 | + // Each key here has a named producer: the form renderer, the inline-edit |
| 75 | + // host, or the line-item grid. Deleting one from the type breaks this. |
| 76 | + const passThrough: FieldWidgetComponentProps<string> = { |
| 77 | + value: '', |
| 78 | + onChange: () => {}, |
| 79 | + field, |
| 80 | + // form renderer (`renderFieldComponent`) |
| 81 | + schema: field, |
| 82 | + dataSource: {}, |
| 83 | + dependentValues: { country: 'cn' }, |
| 84 | + dependsOn: ['country'], |
| 85 | + emptyHint: 'Select country first', |
| 86 | + name: 'title', |
| 87 | + // inline-edit hosts / line-item grid |
| 88 | + compact: true, |
| 89 | + autoFocus: true, |
| 90 | + onSelectRecord: () => {}, |
| 91 | + onCreateNew: () => {}, |
| 92 | + // DOM / a11y |
| 93 | + id: 'title-input', |
| 94 | + 'aria-label': 'Title', |
| 95 | + 'data-testid': 'field-title', |
| 96 | + }; |
| 97 | + expect(passThrough.compact).toBe(true); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('closing the type kept the pass-through behaviour', () => { |
| 102 | + it('forwards a11y and data attributes to the rendered input', () => { |
| 103 | + render( |
| 104 | + <TextField |
| 105 | + value="hello" |
| 106 | + onChange={() => {}} |
| 107 | + field={field} |
| 108 | + aria-label="Title" |
| 109 | + data-testid="title-input" |
| 110 | + />, |
| 111 | + ); |
| 112 | + const input = screen.getByTestId('title-input'); |
| 113 | + expect(input).toHaveAttribute('aria-label', 'Title'); |
| 114 | + expect(input).toHaveValue('hello'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('forwards `disabled` — a declared key, not an index-signature accident', () => { |
| 118 | + render( |
| 119 | + <BooleanField |
| 120 | + value={false} |
| 121 | + onChange={() => {}} |
| 122 | + field={{ name: 'active', label: 'Active', type: 'boolean' } as unknown as FieldMetadata} |
| 123 | + disabled |
| 124 | + data-testid="active-switch" |
| 125 | + />, |
| 126 | + ); |
| 127 | + expect(screen.getByTestId('active-switch')).toBeDisabled(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments