Skip to content

Commit 70cf28c

Browse files
committed
test(app-shell): pin the held field-value row and the stored script type
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jVG55iUTTWRd5tR6Di
1 parent 8c8d967 commit 70cf28c

2 files changed

Lines changed: 311 additions & 0 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* objectstack#26735 — "Add field value" adds a row the author can fill, and
5+
* the row reaches the update action's `patch` once it names a field.
6+
*
7+
* Measured before the fix: the button committed `['', '']`, the write dropped
8+
* the blank key, so no row appeared and `patch` stayed `{}` — an update action
9+
* made in Studio could never be given a field to set.
10+
*
11+
* The pins drive the real inspector in a host that merges each patch
12+
* shallowly, the way the action hosts do, and parse the sent draft with the
13+
* spec's `ActionSchema`.
14+
*/
15+
16+
import '@testing-library/jest-dom/vitest';
17+
import * as React from 'react';
18+
import { describe, it, expect, vi, afterEach } from 'vitest';
19+
import { render, screen, cleanup, fireEvent } from '@testing-library/react';
20+
import { ActionSchema } from '@objectstack/spec/ui';
21+
22+
const state = vi.hoisted(() => ({
23+
metadataClient: { get: vi.fn(async () => undefined), list: vi.fn(async () => [] as unknown[]), withPreviewDrafts() { return this; } },
24+
}));
25+
vi.mock('../useMetadata', () => ({ useMetadataClient: () => state.metadataClient }));
26+
27+
import { ActionDefaultInspector } from './ActionDefaultInspector';
28+
29+
afterEach(cleanup);
30+
31+
type Draft = Record<string, unknown>;
32+
33+
function mountHost(initial: Draft) {
34+
const seen: { draft: Draft; writes: Draft[]; setDraft: (d: Draft) => void } = {
35+
draft: initial,
36+
writes: [],
37+
setDraft: () => {},
38+
};
39+
function Host() {
40+
const [draft, setDraft] = React.useState<Draft>(initial);
41+
seen.draft = draft;
42+
seen.setDraft = (d) => setDraft(d);
43+
return (
44+
<ActionDefaultInspector
45+
type="action"
46+
name={String(initial.name)}
47+
locale={'en-US' as never}
48+
readOnly={false}
49+
draft={draft}
50+
onPatch={(patch) => {
51+
seen.writes.push(patch);
52+
setDraft((d) => ({ ...d, ...patch }));
53+
}}
54+
/>
55+
);
56+
}
57+
render(<Host />);
58+
return seen;
59+
}
60+
61+
const sent = (draft: Draft): Draft => JSON.parse(JSON.stringify(draft));
62+
63+
function refusals(draft: Draft): string[] {
64+
const parsed = ActionSchema.safeParse(sent(draft));
65+
return parsed.success ? [] : parsed.error.issues.map((i) => `${i.path.join('.')}:${i.code}`);
66+
}
67+
68+
/** The text inputs under every visible label `label`, in row order. */
69+
function inputsUnder(label: string): HTMLInputElement[] {
70+
return screen
71+
.queryAllByText(label, { selector: 'label' })
72+
.map((lab) => lab.parentElement!.querySelector('input') as HTMLInputElement);
73+
}
74+
75+
const addRow = () => fireEvent.click(screen.getByRole('button', { name: 'Add field value' }));
76+
77+
/** The skeleton Studio's New starts from (objectui#11820): `type` left to the spec. */
78+
const NEW_UPDATE_ACTION: Draft = {
79+
name: 'invoice_action_2',
80+
label: 'New action',
81+
objectName: 'invoice',
82+
locations: ['record_header'],
83+
operation: 'update',
84+
patch: {},
85+
};
86+
87+
describe('fieldValueRowsPin — a new field-value row is held until it names a field (objectstack#26735)', () => {
88+
it('add row → field → value: the row shows at once, and `patch` holds it once it names a field', () => {
89+
const seen = mountHost(NEW_UPDATE_ACTION);
90+
expect(inputsUnder('Field')).toHaveLength(0);
91+
92+
addRow();
93+
// The row is on screen, and nothing was written: a blank row never
94+
// reaches the draft.
95+
expect(inputsUnder('Field')).toHaveLength(1);
96+
expect(seen.writes).toStrictEqual([]);
97+
expect(sent(seen.draft).patch).toStrictEqual({});
98+
99+
const field = inputsUnder('Field')[0];
100+
fireEvent.change(field, { target: { value: 'status' } });
101+
expect(sent(seen.draft).patch).toStrictEqual({ status: '' });
102+
// The row was written in place: the same input, so the focus stays.
103+
expect(inputsUnder('Field')[0]).toBe(field);
104+
expect(field).toHaveValue('status');
105+
106+
fireEvent.change(inputsUnder('Value')[0], { target: { value: 'done' } });
107+
expect(sent(seen.draft).patch).toStrictEqual({ status: 'done' });
108+
expect(refusals(seen.draft)).toEqual([]);
109+
});
110+
111+
it('a row whose field is cleared stays on screen, held, and leaves `patch`', () => {
112+
const seen = mountHost({ ...NEW_UPDATE_ACTION, type: 'script', patch: { status: 'done', priority: 'high' } });
113+
expect(inputsUnder('Field').map((i) => i.value)).toEqual(['status', 'priority']);
114+
115+
fireEvent.change(inputsUnder('Field')[0], { target: { value: '' } });
116+
expect(sent(seen.draft).patch).toStrictEqual({ priority: 'high' });
117+
expect(inputsUnder('Field').map((i) => i.value)).toEqual(['', 'priority']);
118+
expect(inputsUnder('Value').map((i) => i.value)).toEqual(['done', 'high']);
119+
120+
fireEvent.change(inputsUnder('Field')[0], { target: { value: 'stage' } });
121+
expect(sent(seen.draft).patch).toStrictEqual({ priority: 'high', stage: 'done' });
122+
expect(refusals(seen.draft)).toEqual([]);
123+
});
124+
125+
it('removing a held row writes nothing; removing a written one takes it out of `patch`', () => {
126+
const seen = mountHost({ ...NEW_UPDATE_ACTION, type: 'script', patch: { status: 'done' } });
127+
addRow();
128+
expect(inputsUnder('Field')).toHaveLength(2);
129+
130+
const removes = () => screen.getAllByRole('button', { name: 'Remove field value' });
131+
fireEvent.click(removes()[1]);
132+
expect(seen.writes).toStrictEqual([]);
133+
expect(inputsUnder('Field')).toHaveLength(1);
134+
135+
fireEvent.click(removes()[0]);
136+
expect(seen.writes).toStrictEqual([{ patch: {} }]);
137+
expect(inputsUnder('Field')).toHaveLength(0);
138+
});
139+
140+
it('control: another write to `patch` shows the draft\'s rows, not the held ones', () => {
141+
const seen = mountHost({ ...NEW_UPDATE_ACTION, type: 'script' });
142+
addRow();
143+
expect(inputsUnder('Field')).toHaveLength(1);
144+
145+
React.act(() => seen.setDraft({ ...seen.draft, patch: { owner: 'me' } }));
146+
expect(inputsUnder('Field').map((i) => i.value)).toEqual(['owner']);
147+
});
148+
149+
it('control: an action that holds a `type` is written with `patch` alone', () => {
150+
const seen = mountHost({ ...NEW_UPDATE_ACTION, type: 'script', patch: { status: 'done' } });
151+
addRow();
152+
fireEvent.change(inputsUnder('Field')[1], { target: { value: 'stage' } });
153+
expect(seen.writes).toStrictEqual([{ patch: { status: 'done', stage: '' } }]);
154+
});
155+
});
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* objectstack#26734 — a script action authored in Studio is stored with
5+
* `type: 'script'`, and the Type control shows only a `type` the draft holds.
6+
*
7+
* Measured before the fix: New → "Run a script" → a body left the draft as
8+
* `{ name, body }` with no `type`, while the Type control read Script. The
9+
* console reads the stored row, finds no `type`, and runs nothing ("is not
10+
* executable: no handler is registered under that name …"); the same action
11+
* with `type: 'script'` added through the API ran.
12+
*
13+
* The pins drive the real inspector in a host that merges each patch
14+
* shallowly, and parse the sent draft with the spec's `ActionSchema`.
15+
*/
16+
17+
import '@testing-library/jest-dom/vitest';
18+
import * as React from 'react';
19+
import { describe, it, expect, vi, afterEach } from 'vitest';
20+
import { render, screen, cleanup, fireEvent, waitFor } from '@testing-library/react';
21+
import { ActionSchema } from '@objectstack/spec/ui';
22+
23+
const state = vi.hoisted(() => ({
24+
metadataClient: { get: vi.fn(async () => undefined), list: vi.fn(async () => [] as unknown[]), withPreviewDrafts() { return this; } },
25+
}));
26+
vi.mock('../useMetadata', () => ({ useMetadataClient: () => state.metadataClient }));
27+
28+
import { ActionDefaultInspector } from './ActionDefaultInspector';
29+
30+
afterEach(cleanup);
31+
32+
type Draft = Record<string, unknown>;
33+
34+
function mountHost(initial: Draft) {
35+
const seen: { draft: Draft; writes: Draft[] } = { draft: initial, writes: [] };
36+
function Host() {
37+
const [draft, setDraft] = React.useState<Draft>(initial);
38+
seen.draft = draft;
39+
return (
40+
<ActionDefaultInspector
41+
type="action"
42+
name={String(initial.name)}
43+
locale={'en-US' as never}
44+
readOnly={false}
45+
draft={draft}
46+
onPatch={(patch) => {
47+
seen.writes.push(patch);
48+
setDraft((d) => ({ ...d, ...patch }));
49+
}}
50+
/>
51+
);
52+
}
53+
render(<Host />);
54+
return seen;
55+
}
56+
57+
const sent = (draft: Draft): Draft => JSON.parse(JSON.stringify(draft));
58+
59+
function refusals(draft: Draft): string[] {
60+
const parsed = ActionSchema.safeParse(sent(draft));
61+
return parsed.success ? [] : parsed.error.issues.map((i) => `${i.path.join('.')}:${i.code}`);
62+
}
63+
64+
async function pick(label: string, option: string) {
65+
fireEvent.keyDown(screen.getByRole('combobox', { name: label }), { key: 'ArrowDown' });
66+
await waitFor(() => expect(screen.queryAllByRole('option').length).toBeGreaterThan(0));
67+
fireEvent.click(screen.getByRole('option', { name: option }));
68+
}
69+
70+
const typeControl = () => screen.getByRole('combobox', { name: 'Type' });
71+
const scriptBody = () => screen.getByText('Script body', { selector: 'label' }).parentElement!.querySelector('textarea')!;
72+
73+
const SCRIPT = 'Script — run an expression / sandboxed JS';
74+
const RUN_A_SCRIPT = 'Run a script — sandboxed JS / expression body';
75+
76+
/** The skeleton Studio's New starts from (objectui#11820): `type` left to the spec. */
77+
const NEW_UPDATE_ACTION: Draft = {
78+
name: 'invoice_action_2',
79+
label: 'New action',
80+
objectName: 'invoice',
81+
locations: ['record_header'],
82+
operation: 'update',
83+
patch: {},
84+
};
85+
86+
describe('scriptTypePin — a script authored here is stored with its type (objectstack#26734)', () => {
87+
it('New → "Run a script" → a body: the draft carries `type: \'script\'` and parses', async () => {
88+
const seen = mountHost(NEW_UPDATE_ACTION);
89+
// The draft holds no `type`, so the Type control shows none.
90+
expect(typeControl()).not.toHaveTextContent(SCRIPT);
91+
92+
await pick('What it does', RUN_A_SCRIPT);
93+
expect(sent(seen.draft).type).toBe('script');
94+
expect(typeControl()).toHaveTextContent(SCRIPT);
95+
96+
fireEvent.change(scriptBody(), { target: { value: 'true' } });
97+
const stored = sent(seen.draft);
98+
expect(stored).toMatchObject({ type: 'script', body: { language: 'expression', source: 'true' } });
99+
expect(stored).not.toHaveProperty('operation');
100+
expect(stored).not.toHaveProperty('patch');
101+
expect(refusals(seen.draft)).toEqual([]);
102+
});
103+
104+
it('a stored script with no `type` shows none in Type, and its first body edit stores it', () => {
105+
const seen = mountHost({
106+
name: 'run_script',
107+
label: 'Run',
108+
objectName: 'invoice',
109+
locations: ['record_more'],
110+
body: { language: 'expression', source: 'true' },
111+
});
112+
expect(typeControl()).not.toHaveTextContent(SCRIPT);
113+
// The editor for what it will run stays on screen.
114+
expect(scriptBody()).toHaveValue('true');
115+
116+
fireEvent.change(scriptBody(), { target: { value: 'false' } });
117+
expect(seen.writes).toStrictEqual([{ type: 'script', body: { language: 'expression', source: 'false' } }]);
118+
expect(typeControl()).toHaveTextContent(SCRIPT);
119+
expect(refusals(seen.draft)).toEqual([]);
120+
});
121+
122+
it('the first field value of an update action from New stores `type` too', () => {
123+
const seen = mountHost(NEW_UPDATE_ACTION);
124+
fireEvent.click(screen.getByRole('button', { name: 'Add field value' }));
125+
const field = screen.getByText('Field', { selector: 'label' }).parentElement!.querySelector('input')!;
126+
fireEvent.change(field, { target: { value: 'status' } });
127+
expect(seen.writes).toStrictEqual([{ type: 'script', patch: { status: '' } }]);
128+
expect(refusals(seen.draft)).toEqual([]);
129+
});
130+
131+
it('why the panels still show the script editor: the spec gives a draft with no `type` the script route', () => {
132+
const parsed = ActionSchema.safeParse({ name: 'run_script', label: 'Run', body: { language: 'expression', source: 'true' } });
133+
expect(parsed.success && parsed.data.type).toBe('script');
134+
});
135+
136+
it('control: an action that holds a `type` is written without one', async () => {
137+
const seen = mountHost({
138+
name: 'invoice_sync',
139+
label: 'Sync',
140+
objectName: 'invoice',
141+
locations: ['record_more'],
142+
type: 'script',
143+
body: { language: 'expression', source: 'true' },
144+
});
145+
expect(typeControl()).toHaveTextContent(SCRIPT);
146+
fireEvent.change(scriptBody(), { target: { value: 'false' } });
147+
await pick('Script language', 'Sandboxed JS (L2)');
148+
for (const write of seen.writes) expect(write).not.toHaveProperty('type');
149+
expect(refusals(seen.draft)).toEqual([]);
150+
});
151+
152+
it('control: an api action keeps its type, and Type shows it', () => {
153+
mountHost({ name: 'invoice_api', label: 'Call', type: 'api', target: '/api/v1/sync' });
154+
expect(typeControl()).toHaveTextContent('API — call an endpoint');
155+
});
156+
});

0 commit comments

Comments
 (0)