|
| 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 | + * One card click runs an authored `onCardClick` EXACTLY ONCE, and the call it |
| 11 | + * survives as carries the modifier event (objectui#9341, maintainer ruling on |
| 12 | + * the card, 2026-09-13). |
| 13 | + * |
| 14 | + * ## The defect |
| 15 | + * |
| 16 | + * `ObjectKanban` handed the same function to `useNavigationOverlay` as its |
| 17 | + * `onRowClick` and then called it again itself: |
| 18 | + * |
| 19 | + * const externalClick = onRowClick ?? onCardClick; |
| 20 | + * const navigation = useNavigationOverlay({ …, onRowClick: externalClick }); |
| 21 | + * … |
| 22 | + * onCardClick: (card, event) => { |
| 23 | + * navigation.handleClick(card, event); |
| 24 | + * onCardClick?.(card); // ⛔ the second call |
| 25 | + * } |
| 26 | + * |
| 27 | + * `handleClick` gives `onRowClick` FULL PRIORITY — it calls it and RETURNS — so |
| 28 | + * for a host that supplies `onCardClick` and no `onRowClick`, that one function |
| 29 | + * is both the value of `externalClick` and the one the next line calls. One |
| 30 | + * click, two calls. Measured 2 by objectui#9338's pin, which relaxed its own |
| 31 | + * control to "it RAN" so that this repair would not redden a file that is not |
| 32 | + * about it; that control is tightened back to the exact count in the same diff |
| 33 | + * as this file. |
| 34 | + * |
| 35 | + * ## The ruling, and why the two calls are NOT interchangeable |
| 36 | + * |
| 37 | + * RULING: drop the wrapper's second call. `externalClick` keeps its |
| 38 | + * `onCardClick` arm. |
| 39 | + * |
| 40 | + * `handleClick` forwards `onRowClick(record, event)`, so a host can implement |
| 41 | + * Cmd/Ctrl/middle-click; the wrapper's second call passed the record ONLY. The |
| 42 | + * surviving channel is therefore the RICHER one, which is leg 2's subject — and |
| 43 | + * the reason the published `ObjectKanbanSchema.onCardClick` declaration grows a |
| 44 | + * second optional parameter in this same change. |
| 45 | + * |
| 46 | + * The same shape one surface over is already the ruling's spelling: |
| 47 | + * `ObjectGallery` (`packages/plugin-list/src/ObjectGallery.tsx`) writes |
| 48 | + * `onRowClick: props.onRowClick ?? props.onCardClick` into the identical hook |
| 49 | + * and has NO second call. |
| 50 | + * |
| 51 | + * ## ⚠️ The consequence the card's text did not state — measured in leg 3 |
| 52 | + * |
| 53 | + * The ruling's rationale says precedence is left alone, so a board inside an |
| 54 | + * `ObjectView` "behaves exactly as today". That is true of the PRECEDENCE |
| 55 | + * EXPRESSION and FALSE of the authored handler's call count: when a parent |
| 56 | + * supplies `onRowClick` AND the document carries `onCardClick`, the authored |
| 57 | + * one ran ONCE before this change (the wrapper called it) and runs ZERO times |
| 58 | + * after it — the parent's handler wins OUTRIGHT, which is what |
| 59 | + * `onRowClick ?? onCardClick` has always said and what nothing enforced. Leg 3 |
| 60 | + * pins that reading rather than leaving it to be discovered. |
| 61 | + * |
| 62 | + * ## Every control here can fire |
| 63 | + * |
| 64 | + * A count of 1 is only a reading if the instrument can report something else. |
| 65 | + * Leg 1 carries the sibling `onCardMove` spy at 0 on the SAME render, so the 1 |
| 66 | + * is about this key and not a recorder that counts every key once; leg 4 drives |
| 67 | + * the same handler TWICE and reads 2, so the counter demonstrably moves. |
| 68 | + * |
| 69 | + * ⚠️ No leg here depends on a throw. React 19 REPORTS a handler error out of |
| 70 | + * the dispatch rather than rethrowing it, so an `expect(...).toThrow()` leg on |
| 71 | + * this path could never fire. |
| 72 | + */ |
| 73 | + |
| 74 | +import { describe, it, expect, vi } from 'vitest'; |
| 75 | +import React from 'react'; |
| 76 | +import { render, waitFor } from '@testing-library/react'; |
| 77 | +import { SchemaRenderer, SchemaRendererProvider } from '@object-ui/react'; |
| 78 | +import type { ObjectKanbanSchema } from '@object-ui/types'; |
| 79 | +import '../index'; |
| 80 | + |
| 81 | +/** Every props object the board implementation was rendered with, in order. */ |
| 82 | +const recorded = vi.hoisted(() => ({ impl: [] as Array<Record<string, unknown>> })); |
| 83 | + |
| 84 | +// The lazy board chunk is a prop recorder: the question is how many times the |
| 85 | +// handler the board was HANDED runs the authored one, not what the board draws. |
| 86 | +vi.mock('../KanbanImpl', () => ({ |
| 87 | + default: (props: Record<string, unknown>) => { |
| 88 | + recorded.impl.push(props); |
| 89 | + return null; |
| 90 | + }, |
| 91 | +})); |
| 92 | + |
| 93 | +const STATIC_COLUMNS = [{ id: 'todo', title: 'To Do', cards: [{ id: '1', title: 'One' }] }]; |
| 94 | + |
| 95 | +/** |
| 96 | + * Author a document on the PRODUCTION path — `SchemaRenderer` resolves the |
| 97 | + * registry key and spreads every non-metadata schema key as a React prop — and |
| 98 | + * return the props the board implementation was handed last. |
| 99 | + */ |
| 100 | +async function boardPropsFor(schemaKeys: Record<string, unknown>) { |
| 101 | + const before = recorded.impl.length; |
| 102 | + const { unmount } = render( |
| 103 | + <SchemaRendererProvider dataSource={undefined}> |
| 104 | + <SchemaRenderer |
| 105 | + schema={ |
| 106 | + { |
| 107 | + type: 'object-kanban', |
| 108 | + columns: STATIC_COLUMNS, |
| 109 | + ...schemaKeys, |
| 110 | + } as never |
| 111 | + } |
| 112 | + /> |
| 113 | + </SchemaRendererProvider>, |
| 114 | + ); |
| 115 | + await waitFor(() => expect(recorded.impl.length).toBeGreaterThan(before)); |
| 116 | + const received = recorded.impl[recorded.impl.length - 1]; |
| 117 | + unmount(); |
| 118 | + return received; |
| 119 | +} |
| 120 | + |
| 121 | +/** The handler the board implementation was handed, driven as a card click. */ |
| 122 | +type BoardClick = (card: unknown, event?: unknown) => void; |
| 123 | + |
| 124 | +describe('objectui#9341 — an authored `onCardClick` runs ONCE per card click', () => { |
| 125 | + it('⭐ ONE click, ONE call — with the sibling `onCardMove` spy at 0 on the same render', async () => { |
| 126 | + const onCardClick = vi.fn(); |
| 127 | + const onCardMove = vi.fn(); |
| 128 | + const card = { id: '1', title: 'One' }; |
| 129 | + |
| 130 | + // ONE document, ONE render. The sibling key is the firing control: a |
| 131 | + // recorder that counted every authored handler once would show 1 here too, |
| 132 | + // and `onCardMove` reaches nothing on this arm (objectui#7804), so a 0 |
| 133 | + // beside the 1 is what makes the 1 a reading about THIS key. |
| 134 | + const props = await boardPropsFor({ onCardClick, onCardMove }); |
| 135 | + (props.onCardClick as BoardClick)(card); |
| 136 | + |
| 137 | + expect( |
| 138 | + { cardClick: onCardClick.mock.calls.length, cardMove: onCardMove.mock.calls.length }, |
| 139 | + 'the authored `onCardClick` must run exactly once per card click', |
| 140 | + ).toEqual({ cardClick: 1, cardMove: 0 }); |
| 141 | + }); |
| 142 | + |
| 143 | + it('⭐ the surviving call carries the MODIFIER EVENT — the whole reason it is the one kept', async () => { |
| 144 | + const onCardClick = vi.fn(); |
| 145 | + const card = { id: '1', title: 'One' }; |
| 146 | + // What `KanbanImpl` forwards is the DOM click event |
| 147 | + // (`onClick={(e) => onCardClick?.(card, e)}`); only the three modifier |
| 148 | + // fields are ever read, so a structural stand-in measures the channel. |
| 149 | + const event = { metaKey: true, ctrlKey: false, button: 0 }; |
| 150 | + |
| 151 | + const props = await boardPropsFor({ onCardClick }); |
| 152 | + (props.onCardClick as BoardClick)(card, event); |
| 153 | + |
| 154 | + expect( |
| 155 | + onCardClick.mock.calls.map((args) => ({ arity: args.length, record: args[0], event: args[1] })), |
| 156 | + 'the kept channel is `handleClick`’s `onRowClick(record, event)` — the dropped one passed the record only', |
| 157 | + ).toEqual([{ arity: 2, record: card, event }]); |
| 158 | + }); |
| 159 | + |
| 160 | + it('⚠️ a parent `onRowClick` wins OUTRIGHT — the authored `onCardClick` no longer also runs', async () => { |
| 161 | + const onRowClick = vi.fn(); |
| 162 | + const onCardClick = vi.fn(); |
| 163 | + const card = { id: '1', title: 'One' }; |
| 164 | + |
| 165 | + // Both reach `ObjectKanban` as React props on this path — `SchemaRenderer` |
| 166 | + // spreads every non-metadata key and `ObjectKanbanRenderer` forwards its |
| 167 | + // rest — which is the shape an `ObjectView`-embedded board has, with the |
| 168 | + // parent supplying `onRowClick`. |
| 169 | + const props = await boardPropsFor({ onRowClick, onCardClick }); |
| 170 | + (props.onCardClick as BoardClick)(card); |
| 171 | + |
| 172 | + // ⛔ NOT a regression smuggled in: `externalClick = onRowClick ?? onCardClick` |
| 173 | + // is untouched by this card and has always said the parent's handler wins. |
| 174 | + // What changed is that the loser no longer ALSO fires. |
| 175 | + expect( |
| 176 | + { rowClick: onRowClick.mock.calls.length, cardClick: onCardClick.mock.calls.length }, |
| 177 | + 'precedence is `onRowClick ?? onCardClick`; exactly one handler answers one click', |
| 178 | + ).toEqual({ rowClick: 1, cardClick: 0 }); |
| 179 | + }); |
| 180 | + |
| 181 | + it('CONTROL — the counter moves: two clicks read 2', async () => { |
| 182 | + const onCardClick = vi.fn(); |
| 183 | + const card = { id: '1', title: 'One' }; |
| 184 | + |
| 185 | + const props = await boardPropsFor({ onCardClick }); |
| 186 | + (props.onCardClick as BoardClick)(card); |
| 187 | + (props.onCardClick as BoardClick)(card); |
| 188 | + |
| 189 | + expect(onCardClick.mock.calls.length, 'a count leg whose count cannot move measures nothing').toBe(2); |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
| 193 | +/* ── The published face, judged by `tsc -p tsconfig.test.json` ───────────── */ |
| 194 | + |
| 195 | +/** |
| 196 | + * ⭐ The published-surface half of this card, MEASURED rather than asserted in |
| 197 | + * prose. `ObjectKanbanSchema.onCardClick` grew a second optional parameter |
| 198 | + * because the surviving channel delivers `(record, event)`; the prediction |
| 199 | + * offered with the ruling was that such a widening is source-compatible. These |
| 200 | + * four lines are that prediction handed to `tsc`, and the last one is the |
| 201 | + * control that keeps the other three from being vacuously true. |
| 202 | + * |
| 203 | + * ⛔ `event` is `any`, not `HandleClickModifiers`: that interface lives in |
| 204 | + * `@object-ui/react`, which depends on `@object-ui/types` and is named in no |
| 205 | + * dependency field of it — `check:phantom-deps` rejects the import and it would |
| 206 | + * close a cycle. `BaseSchema`'s own `onClick` / `onChange` / `onSubmit` |
| 207 | + * already spell this exact situation the same way. |
| 208 | + */ |
| 209 | +type Equal<A, B> = |
| 210 | + (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; |
| 211 | +type Expect<T extends true> = T; |
| 212 | +type Assignable<From, To> = [From] extends [To] ? true : false; |
| 213 | +type CardClick = NonNullable<ObjectKanbanSchema['onCardClick']>; |
| 214 | + |
| 215 | +export type assertion9341PublishedSignatureIsSourceCompatible = [ |
| 216 | + /** WIDENING direction: a host's existing one-argument handler still fits. */ |
| 217 | + Expect<Assignable<(card: any) => void, CardClick>>, |
| 218 | + /** The reverse: code that stores the member in a one-argument slot still compiles. */ |
| 219 | + Expect<Assignable<CardClick, (card: any) => void>>, |
| 220 | + /** The arity the channel actually delivers is now declared. */ |
| 221 | + Expect<Assignable<CardClick, (card: any, event?: any) => void>>, |
| 222 | + /** CONTROL — `Assignable` can answer `false`: a REQUIRED second parameter does not fit. */ |
| 223 | + Expect<Equal<Assignable<(card: any, event: any) => void, (card: any) => void>, false>>, |
| 224 | +]; |
0 commit comments