Skip to content

Commit ec75844

Browse files
committed
test(ui): pin the param widget through resolver, adapter and dialog
Claude-Session: https://claude.ai/code/session_01L6bhgXcNPmrQhpwYYn7fEq Co-authored-by: Claude <noreply@anthropic.com>
1 parent 57b06e3 commit ec75844

3 files changed

Lines changed: 191 additions & 0 deletions

File tree

‎ui/packages/app-shell/src/utils/paramToField.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,45 @@ describe('paramDegradesWithoutTarget — one answer to "did this param degrade?"
380380
}
381381
});
382382
});
383+
384+
describe('paramToField — an authored `widget` draws the param (#30260)', () => {
385+
// `ActionParamDef.widget` is the spec's `ActionParamSchema.widget`, which
386+
// shares `FieldSchema.widget`'s meaning: a registered name wins over `type`,
387+
// an unregistered one degrades to the `type` widget.
388+
389+
it('draws the registered widget a text param names, in place of the text widget', () => {
390+
expect(paramToField(p({ type: 'text', widget: 'permission-ref' })).type).toBe('permission-ref');
391+
expect(paramToField(p({ type: 'text', widget: 'position-ref' })).type).toBe('position-ref');
392+
});
393+
394+
it('CONTROL: the same param without a widget is the text widget', () => {
395+
expect(paramToField(p({ type: 'text' })).type).toBe('text');
396+
});
397+
398+
it('falls back to the `type` widget when the name is not registered', () => {
399+
expect(paramToField(p({ type: 'text', widget: 'no_such_widget' })).type).toBe('text');
400+
// Not the text fallback: the param's OWN type widget.
401+
expect(paramToField(p({ type: 'select', widget: 'no_such_widget' })).type).toBe('select');
402+
expect(paramToField(p({ type: 'textarea', widget: 'no_such_widget' })).type).toBe('textarea');
403+
});
404+
405+
it('an authored `text` widget is honoured as the text widget, not read as "unregistered"', () => {
406+
expect(paramToField(p({ type: 'textarea', widget: 'text' })).type).toBe('text');
407+
});
408+
409+
it('a registered widget on a targetless picker param is drawn, and the param does not degrade', () => {
410+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
411+
try {
412+
const param = p({ type: 'lookup', widget: 'permission-ref' });
413+
expect(paramToField(param).type).toBe('permission-ref');
414+
expect(paramDegradesWithoutTarget(param)).toBe(false);
415+
expect(warn).not.toHaveBeenCalled();
416+
// CONTROL: an unregistered widget leaves the degradation exactly as before.
417+
const unregistered = p({ type: 'lookup', widget: 'no_such_widget' });
418+
expect(paramToField(unregistered).type).toBe('text');
419+
expect(paramDegradesWithoutTarget(unregistered)).toBe(true);
420+
} finally {
421+
warn.mockRestore();
422+
}
423+
});
424+
});

‎ui/packages/app-shell/src/utils/resolveActionParams.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,86 @@ describe('resolveActionParams — field-inherited option keys (objectui#3559)',
513513
expect([noValue, noLabel]).toHaveLength(2);
514514
});
515515
});
516+
517+
describe('resolveActionParams — the param `widget` reaches the resolved param (#30260)', () => {
518+
// `ActionParamSchema.widget` shares `FieldSchema.widget`'s key and meaning.
519+
// The resolver builds its output key by key, so a key it does not copy never
520+
// reaches the dialog — the state before this card, measured: every branch
521+
// below answered `widget: undefined`.
522+
const grantCtx = (): ResolveActionParamsContext =>
523+
ctx({
524+
objects: [
525+
{
526+
name: 'sys_user',
527+
fields: {
528+
permission_set: { type: 'text', label: 'Permission set', widget: 'permission-ref' } as any,
529+
plain: { type: 'text', label: 'Plain' },
530+
},
531+
},
532+
],
533+
});
534+
535+
it('copies an inline param widget onto the resolved param', () => {
536+
const [out] = resolveActionParams(
537+
[{ name: 'permissionSet', type: 'text', widget: 'permission-ref' }],
538+
grantCtx(),
539+
);
540+
expect(out.widget).toBe('permission-ref');
541+
// `type` stays the value's type: the widget only changes how it is picked.
542+
expect(out.type).toBe('text');
543+
});
544+
545+
it('inherits the field widget on a field-backed param that inherits the type', () => {
546+
const [out] = resolveActionParams([{ field: 'permission_set' }], grantCtx());
547+
expect(out.widget).toBe('permission-ref');
548+
expect(out.type).toBe('text');
549+
});
550+
551+
it('inherits it too when the param restates the field type', () => {
552+
const [out] = resolveActionParams([{ field: 'permission_set', type: 'text' }], grantCtx());
553+
expect(out.widget).toBe('permission-ref');
554+
});
555+
556+
it('does NOT inherit the field widget onto a param that overrides the type', () => {
557+
const [out] = resolveActionParams([{ field: 'permission_set', type: 'textarea' }], grantCtx());
558+
expect(out.widget).toBeUndefined();
559+
expect(out.type).toBe('textarea');
560+
});
561+
562+
it('lets the param widget win over the field widget', () => {
563+
const [out] = resolveActionParams(
564+
[{ field: 'permission_set', widget: 'position-ref' }],
565+
grantCtx(),
566+
);
567+
expect(out.widget).toBe('position-ref');
568+
});
569+
570+
it('CONTROL: a param and field that declare no widget resolve none', () => {
571+
const [inline, backed] = resolveActionParams(
572+
[{ name: 'note', type: 'text' }, { field: 'plain' }],
573+
grantCtx(),
574+
);
575+
expect(inline.widget).toBeUndefined();
576+
expect(backed.widget).toBeUndefined();
577+
});
578+
579+
it('keeps the param widget on the missing-field fallback branch', () => {
580+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
581+
try {
582+
const [out] = resolveActionParams(
583+
[{ field: 'no_such_field', widget: 'permission-ref' }],
584+
grantCtx(),
585+
);
586+
expect(out.unresolvedField).toBe('sys_user.no_such_field');
587+
expect(out.widget).toBe('permission-ref');
588+
} finally {
589+
warn.mockRestore();
590+
}
591+
});
592+
593+
it('accepts the widget through the public ActionParam authoring type', () => {
594+
const authored: ActionParam[] = [{ name: 'permissionSet', type: 'text', widget: 'permission-ref' }];
595+
const [out] = resolveActionParams(authored as RawActionParam[], grantCtx());
596+
expect(paramToField(out).type).toBe('permission-ref');
597+
});
598+
});

‎ui/packages/app-shell/src/views/ActionParamDialog.test.tsx‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { describe, it, expect, vi } from 'vitest';
3030
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
3131
import type { ActionParamDef } from '@object-ui/core';
3232
import { validateActionParams } from '@objectstack/spec/ui';
33+
import { SchemaRendererProvider } from '@object-ui/react';
3334
import { ActionParamDialog, filterVisibleParams, serializeParamValues } from './ActionParamDialog';
3435
import { expectedParamShape, classifyValueShape } from '../utils/paramValueShape';
3536

@@ -815,3 +816,68 @@ describe('serializeParamValues', () => {
815816
});
816817
});
817818
});
819+
820+
describe('ActionParamDialog — a param `widget` draws the named widget (#30260)', () => {
821+
// The Create User shape once its `permissionSet` param declares the picker
822+
// (`ActionParamSchema.widget`, ADR-0131 D4): a `text` param whose value is a
823+
// permission-set NAME. The registry pickers read their catalog through the
824+
// data source of `SchemaRendererContext`, so the dialog is mounted inside the
825+
// provider, as the console mounts it.
826+
function openWithCatalog(params: ActionParamDef[]) {
827+
const resolve = vi.fn();
828+
const ds = {
829+
listMetadataItems: vi.fn(async (_type: string) => [
830+
{ name: 'sales_user', label: 'Sales User' },
831+
{ name: 'support_agent', label: 'Support Agent' },
832+
]),
833+
};
834+
const view = render(
835+
<SchemaRendererProvider dataSource={ds as any}>
836+
<ActionParamDialog state={{ open: true, params, resolve }} onOpenChange={() => {}} />
837+
</SchemaRendererProvider>,
838+
);
839+
return { resolve, ds, ...view };
840+
}
841+
842+
it('draws the permission-set picker: labels listed, the machine name submitted', async () => {
843+
const param = def({ name: 'permissionSet', label: 'Permission set', type: 'text', widget: 'permission-ref' });
844+
const { resolve, ds } = openWithCatalog([param]);
845+
846+
const trigger = await screen.findByRole('combobox');
847+
await waitFor(() => expect(ds.listMetadataItems).toHaveBeenCalledWith('permission'));
848+
fireEvent.click(trigger);
849+
fireEvent.click(await screen.findByText('Support Agent (support_agent)'));
850+
await waitFor(() => expect(trigger).toHaveTextContent('Support Agent (support_agent)'));
851+
852+
confirm();
853+
await waitFor(() => expect(resolve).toHaveBeenCalledWith({ permissionSet: 'support_agent' }));
854+
// The emitted value matches the declared shape contract for the widget.
855+
expect(expectedParamShape(param)).toBe('string');
856+
expect(classifyValueShape(resolve.mock.calls[0][0].permissionSet)).toBe('string');
857+
});
858+
859+
it('CONTROL: the same param without a widget is a text box, and lists nothing', async () => {
860+
const { resolve, ds } = openWithCatalog([
861+
def({ name: 'permissionSet', label: 'Permission set', type: 'text' }),
862+
]);
863+
const input = await screen.findByLabelText('Permission set');
864+
expect(input.tagName).toBe('INPUT');
865+
expect(screen.queryByRole('combobox')).toBeNull();
866+
expect(ds.listMetadataItems).not.toHaveBeenCalled();
867+
fireEvent.change(input, { target: { value: 'typed_name' } });
868+
confirm();
869+
await waitFor(() => expect(resolve).toHaveBeenCalledWith({ permissionSet: 'typed_name' }));
870+
});
871+
872+
it('an unregistered widget name falls back to the `type` control', async () => {
873+
const { resolve, ds } = openWithCatalog([
874+
def({ name: 'reason', label: 'Reason', type: 'textarea', widget: 'no_such_widget' }),
875+
]);
876+
const box = await screen.findByLabelText('Reason');
877+
expect(box.tagName).toBe('TEXTAREA');
878+
expect(ds.listMetadataItems).not.toHaveBeenCalled();
879+
fireEvent.change(box, { target: { value: 'because' } });
880+
confirm();
881+
await waitFor(() => expect(resolve).toHaveBeenCalledWith({ reason: 'because' }));
882+
});
883+
});

0 commit comments

Comments
 (0)