Skip to content

Commit 794c315

Browse files
committed
test(ui): pin the state-machine select narrowing through the edit dialog and the simple form
Claude-Session: https://claude.ai/code/session_015Kzn5VKHAGKe251waHoUqj Co-authored-by: Claude <noreply@anthropic.com>
1 parent e07456b commit 794c315

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@object-ui/core': patch
3+
'@object-ui/plugin-form': patch
4+
'@object-ui/plugin-grid': patch
5+
---
6+
7+
feat(ui): an edit form's `state_machine` select offers only the moves the server accepts
8+
9+
Clause-②: yes
10+
Metadata-protocol: none
11+
12+
A record whose `status` is governed by an error-severity `state_machine` validation used to offer every option in the edit form, and the server refused the invalid ones only after submit, without naming the allowed next states. The form now offers the persisted value plus the states the machine declares as its next moves.
13+
14+
- **`@object-ui/core`:** new export `stateMachineNextValues(objectSchema, fieldName, currentValue)`. It returns the set of values a field may move to from its persisted value, or `null` for "don't constrain". It narrows only where the server refuses: rules with `severity: 'error'` (the default), `active` not `false`, and `events` covering `update` (the default covers it). A from-state the transition map does not declare stays unconstrained, and several applicable rules on one field intersect. It is a read of the declared transition map for choice controls, not a client-side rule engine.
15+
- **`@object-ui/plugin-form`:** `gateFormFields` takes an optional `previousValues` (the persisted record) and narrows each single-value select governed by such a machine, on both the entry's own `options` and its `field.options`. Every layout passes its persisted record: the simple, modal, drawer, tabbed, split and wizard forms. The from-state is the stored row, never the live form value. A create form is unconstrained, and the current value always stays offered. Options outside the reachable set are left out rather than drawn disabled.
16+
- **`@object-ui/plugin-grid`:** `stateMachineNextValues` is now re-exported from `@object-ui/core`. The grid's inline editor therefore stops narrowing on a `warning` / `info` machine, an inactive one, or one whose `events` leave out `update`. The server never refuses those moves, so the editor now offers every option for them.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
* #25933 — the edit form's `status` select offers only the moves the object's
11+
* error-severity `state_machine` allows from the stored value.
12+
*
13+
* Measured on the showcase task before the fix: a `backlog` task's edit dialog
14+
* listed all five states, and picking `done` came back from the server as
15+
* "Invalid task status transition." Pinned through the console's edit dialog
16+
* (the global `ModalForm`) and the in-page simple form, and read off the
17+
* hidden native `<select>` Radix renders inside a form, which lists exactly
18+
* the options the widget was handed.
19+
*/
20+
21+
import React from 'react';
22+
import { describe, it, expect, vi, afterEach } from 'vitest';
23+
import { render, waitFor, cleanup } from '@testing-library/react';
24+
import { registerAllFields } from '@object-ui/fields';
25+
import type { DataSource } from '@object-ui/types';
26+
import { ModalForm } from '../ModalForm';
27+
import { ObjectForm } from '../ObjectForm';
28+
29+
registerAllFields();
30+
31+
const STATUS_OPTIONS = [
32+
{ label: 'Backlog', value: 'backlog' },
33+
{ label: 'To Do', value: 'todo' },
34+
{ label: 'In Progress', value: 'in_progress' },
35+
{ label: 'In Review', value: 'in_review' },
36+
{ label: 'Done', value: 'done' },
37+
];
38+
39+
/** The showcase task (`examples/app-showcase` task.object.ts), as `/meta` serves it. */
40+
const TASK = (rule: Record<string, unknown> = {}) => ({
41+
name: 'showcase_task',
42+
fields: {
43+
title: { type: 'text', label: 'Title' },
44+
status: { type: 'select', label: 'Status', options: STATUS_OPTIONS },
45+
},
46+
validations: [
47+
{
48+
type: 'state_machine',
49+
name: 'task_status_flow',
50+
field: 'status',
51+
events: ['update'],
52+
message: 'Invalid task status transition.',
53+
transitions: {
54+
backlog: ['todo'],
55+
todo: ['in_progress', 'backlog'],
56+
in_progress: ['in_review', 'todo'],
57+
in_review: ['done', 'in_progress'],
58+
done: ['in_progress'],
59+
},
60+
...rule,
61+
},
62+
],
63+
});
64+
65+
const makeDS = (objectSchema: unknown, record: Record<string, unknown>): DataSource =>
66+
({
67+
getObjectSchema: vi.fn().mockResolvedValue(objectSchema),
68+
create: vi.fn().mockResolvedValue({ id: 't1' }),
69+
update: vi.fn().mockResolvedValue({ id: 't1' }),
70+
findOne: vi.fn().mockResolvedValue(record),
71+
}) as unknown as DataSource;
72+
73+
/** The values the hidden native select offers, empty placeholder left out. */
74+
const offered = () =>
75+
Array.from(
76+
(document.body.querySelector('select[name="status"]') as HTMLSelectElement | null)?.options ?? [],
77+
)
78+
.map((o) => o.value)
79+
.filter((v) => v !== '');
80+
81+
const ALL = STATUS_OPTIONS.map((o) => o.value);
82+
83+
afterEach(() => cleanup());
84+
85+
const renderModal = (mode: 'edit' | 'create', ds: DataSource) =>
86+
render(
87+
<ModalForm
88+
schema={{
89+
type: 'object-form',
90+
formType: 'modal',
91+
objectName: 'showcase_task',
92+
mode,
93+
...(mode === 'edit' ? { recordId: 't1' } : {}),
94+
title: 'Task',
95+
open: true,
96+
onOpenChange: vi.fn(),
97+
fields: ['title', 'status'],
98+
} as any}
99+
dataSource={ds}
100+
/>,
101+
);
102+
103+
describe('state-machine select narrowing (#25933)', () => {
104+
it('the console edit dialog offers a backlog task only backlog and todo', async () => {
105+
renderModal('edit', makeDS(TASK(), { id: 't1', title: 'T', status: 'backlog' }));
106+
await waitFor(() => expect(offered()).toEqual(['backlog', 'todo']));
107+
});
108+
109+
it('narrows from the stored value of an in-review task', async () => {
110+
renderModal('edit', makeDS(TASK(), { id: 't1', title: 'T', status: 'in_review' }));
111+
await waitFor(() => expect(offered()).toEqual(['in_progress', 'in_review', 'done']));
112+
});
113+
114+
it('CONTROL: a create dialog offers every option', async () => {
115+
renderModal('create', makeDS(TASK(), {}));
116+
await waitFor(() => expect(offered()).toEqual(ALL));
117+
});
118+
119+
it('CONTROL: a warning-severity machine keeps every option', async () => {
120+
renderModal('edit', makeDS(TASK({ severity: 'warning' }), { id: 't1', title: 'T', status: 'backlog' }));
121+
await waitFor(() => expect(document.body.querySelector('select[name="status"]')).toBeTruthy());
122+
// Let the record load settle before reading the list.
123+
await waitFor(() => expect(offered()).toEqual(ALL));
124+
});
125+
126+
it('the in-page simple edit form narrows the same way', async () => {
127+
render(
128+
<ObjectForm
129+
schema={{
130+
type: 'object-form',
131+
objectName: 'showcase_task',
132+
mode: 'edit',
133+
recordId: 't1',
134+
fields: ['title', 'status'],
135+
} as any}
136+
dataSource={makeDS(TASK(), { id: 't1', title: 'T', status: 'done' })}
137+
/>,
138+
);
139+
await waitFor(() => expect(offered()).toEqual(['in_progress', 'done']));
140+
});
141+
});

0 commit comments

Comments
 (0)