|
| 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#10616 — the three view event-name keys work through the REAL |
| 11 | + * `SchemaRenderer`, which is the path an authored document takes. |
| 12 | + * |
| 13 | + * ## The defect this pins |
| 14 | + * |
| 15 | + * objectui#6124 declared `FilterUISchema.onChange`, `SortUISchema.onChange` |
| 16 | + * and `ViewSwitcherSchema.onViewChange` as EVENT NAMES: the authored string is |
| 17 | + * the name of a `CustomEvent` dispatched on `window`. But `SchemaRenderer` |
| 18 | + * spreads every non-metadata node key as a React prop, so the same string also |
| 19 | + * lands in the component's same-named CALLBACK prop. Each control called that |
| 20 | + * prop before dispatching, so the first interaction threw |
| 21 | + * `TypeError: onChange is not a function` (`onViewChange` on the switcher) and |
| 22 | + * the window event after it never fired. |
| 23 | + * |
| 24 | + * ## Why this file does not mock `SchemaRenderer` |
| 25 | + * |
| 26 | + * The objectui#6124 pins (`handlerEventNameLiveness.6124.test.tsx`) render the |
| 27 | + * components DIRECTLY and mock `SchemaRenderer`, so the callback prop is always |
| 28 | + * `undefined` there and they stayed green over this defect. Those pins still |
| 29 | + * own what they own — the keys are declared, and the directly rendered control |
| 30 | + * dispatches — and this file owns the other entry: a registered node rendered |
| 31 | + * through the real renderer and registry, with and without a host function. |
| 32 | + * |
| 33 | + * ## What each leg asserts |
| 34 | + * |
| 35 | + * - AUTHORED string, no host prop: the documented event fires once with the |
| 36 | + * documented `detail`, and nothing is reported (no `TypeError`). |
| 37 | + * - CONTROL — a host FUNCTION passed through `SchemaRenderer` next to the same |
| 38 | + * authored string: the function is called once with the new value AND the |
| 39 | + * event still fires. It was green before the fix too; it is here so the fix |
| 40 | + * cannot pass by dropping the host channel. |
| 41 | + */ |
| 42 | + |
| 43 | +import { describe, it, expect, vi } from 'vitest'; |
| 44 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 45 | +import { SchemaRenderer } from '@object-ui/react'; |
| 46 | +// Module scope, not a hook: this import IS the registration of |
| 47 | +// `filter-ui`, `sort-ui` and `view-switcher` in the real registry. |
| 48 | +import '../index'; |
| 49 | + |
| 50 | +interface Reading<D> { |
| 51 | + /** Every error React or the runtime reported while the interaction ran. */ |
| 52 | + reported: string[]; |
| 53 | + /** The `detail` of every `eventName` event dispatched on `window`. */ |
| 54 | + details: D[]; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Run `act` and record what it reported and dispatched. |
| 59 | + * |
| 60 | + * Errors are read from THREE places because React 19 does not rethrow an |
| 61 | + * error thrown inside an event handler out of `fireEvent` — it REPORTS it |
| 62 | + * (window `error` event and `console.error`). A bare `expect(...).toThrow()` |
| 63 | + * would read a crashing control as green. |
| 64 | + */ |
| 65 | +function interact<D>(eventName: string, act: () => void): Reading<D> { |
| 66 | + const reported: string[] = []; |
| 67 | + const details: D[] = []; |
| 68 | + const onEvent = (e: Event) => details.push((e as CustomEvent<D>).detail); |
| 69 | + const onError = (e: Event) => { |
| 70 | + reported.push(String((e as ErrorEvent).error ?? e)); |
| 71 | + e.preventDefault(); |
| 72 | + }; |
| 73 | + const spy = vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => { |
| 74 | + reported.push(args.map((a) => String(a)).join(' ')); |
| 75 | + }); |
| 76 | + window.addEventListener(eventName, onEvent); |
| 77 | + window.addEventListener('error', onError); |
| 78 | + try { |
| 79 | + act(); |
| 80 | + } catch (err) { |
| 81 | + reported.push(String(err)); |
| 82 | + } finally { |
| 83 | + window.removeEventListener(eventName, onEvent); |
| 84 | + window.removeEventListener('error', onError); |
| 85 | + spy.mockRestore(); |
| 86 | + } |
| 87 | + return { reported, details }; |
| 88 | +} |
| 89 | + |
| 90 | +describe('filter-ui: an authored onChange event name through SchemaRenderer (objectui#10616)', () => { |
| 91 | + const node = { |
| 92 | + type: 'filter-ui', |
| 93 | + layout: 'inline', |
| 94 | + filters: [{ field: 'qty', label: 'Qty', type: 'number' }], |
| 95 | + onChange: 'zz10616:filter', |
| 96 | + }; |
| 97 | + const typeSeven = (container: HTMLElement) => { |
| 98 | + const input = container.querySelector('input[type="number"]') as HTMLInputElement | null; |
| 99 | + expect(input, 'the registered filter-ui rendered no number input').toBeTruthy(); |
| 100 | + fireEvent.change(input!, { target: { value: '7' } }); |
| 101 | + }; |
| 102 | + |
| 103 | + it('dispatches the named window event with detail { values } and reports nothing', () => { |
| 104 | + const { container } = render(<SchemaRenderer schema={node} />); |
| 105 | + const { reported, details } = interact<{ values: Record<string, unknown> }>( |
| 106 | + 'zz10616:filter', |
| 107 | + () => typeSeven(container), |
| 108 | + ); |
| 109 | + expect(reported).toEqual([]); |
| 110 | + expect(details).toHaveLength(1); |
| 111 | + expect(details[0]).toEqual({ values: { qty: 7 } }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('CONTROL: a host function passed through SchemaRenderer is called, and the event still fires', () => { |
| 115 | + const host = vi.fn(); |
| 116 | + const { container } = render(<SchemaRenderer schema={node} onChange={host} />); |
| 117 | + const { reported, details } = interact<{ values: Record<string, unknown> }>( |
| 118 | + 'zz10616:filter', |
| 119 | + () => typeSeven(container), |
| 120 | + ); |
| 121 | + expect(reported).toEqual([]); |
| 122 | + expect(host).toHaveBeenCalledTimes(1); |
| 123 | + expect(host).toHaveBeenCalledWith({ qty: 7 }); |
| 124 | + expect(details).toEqual([{ values: { qty: 7 } }]); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe('sort-ui: an authored onChange event name through SchemaRenderer (objectui#10616)', () => { |
| 129 | + const node = { |
| 130 | + type: 'sort-ui', |
| 131 | + variant: 'buttons', |
| 132 | + fields: [{ field: 'name', label: 'Name' }], |
| 133 | + onChange: 'zz10616:sort', |
| 134 | + }; |
| 135 | + const clickName = () => fireEvent.click(screen.getByRole('button', { name: /name/i })); |
| 136 | + |
| 137 | + it('dispatches the named window event with detail { sort } and reports nothing', () => { |
| 138 | + render(<SchemaRenderer schema={node} />); |
| 139 | + const { reported, details } = interact<{ sort: unknown }>('zz10616:sort', clickName); |
| 140 | + expect(reported).toEqual([]); |
| 141 | + expect(details).toEqual([{ sort: [{ field: 'name', direction: 'asc' }] }]); |
| 142 | + }); |
| 143 | + |
| 144 | + it('CONTROL: a host function passed through SchemaRenderer is called, and the event still fires', () => { |
| 145 | + const host = vi.fn(); |
| 146 | + render(<SchemaRenderer schema={node} onChange={host} />); |
| 147 | + const { reported, details } = interact<{ sort: unknown }>('zz10616:sort', clickName); |
| 148 | + expect(reported).toEqual([]); |
| 149 | + expect(host).toHaveBeenCalledTimes(1); |
| 150 | + expect(host).toHaveBeenCalledWith([{ field: 'name', direction: 'asc' }]); |
| 151 | + expect(details).toEqual([{ sort: [{ field: 'name', direction: 'asc' }] }]); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +describe('view-switcher: an authored onViewChange event name through SchemaRenderer (objectui#10616)', () => { |
| 156 | + const node = { |
| 157 | + type: 'view-switcher', |
| 158 | + variant: 'buttons', |
| 159 | + views: [{ type: 'list' }, { type: 'grid' }], |
| 160 | + onViewChange: 'zz10616:view', |
| 161 | + }; |
| 162 | + const clickGrid = () => fireEvent.click(screen.getByRole('button', { name: /grid/i })); |
| 163 | + |
| 164 | + it('dispatches the named window event with detail { view } and reports nothing', () => { |
| 165 | + render(<SchemaRenderer schema={node} />); |
| 166 | + const { reported, details } = interact<{ view: string }>('zz10616:view', clickGrid); |
| 167 | + expect(reported).toEqual([]); |
| 168 | + expect(details).toEqual([{ view: 'grid' }]); |
| 169 | + }); |
| 170 | + |
| 171 | + it('CONTROL: a host function passed through SchemaRenderer is called, and the event still fires', () => { |
| 172 | + const host = vi.fn(); |
| 173 | + render(<SchemaRenderer schema={node} onViewChange={host} />); |
| 174 | + const { reported, details } = interact<{ view: string }>('zz10616:view', clickGrid); |
| 175 | + expect(reported).toEqual([]); |
| 176 | + expect(host).toHaveBeenCalledTimes(1); |
| 177 | + expect(host).toHaveBeenCalledWith('grid'); |
| 178 | + expect(details).toEqual([{ view: 'grid' }]); |
| 179 | + }); |
| 180 | +}); |
0 commit comments