Repository navigation
Expand file tree
/
Copy pathFlowKeyValueField.valueEnvelope.test.tsx
More file actions
141 lines (126 loc) · 6.67 KB
/
Copy pathFlowKeyValueField.valueEnvelope.test.tsx
File metadata and controls
141 lines (126 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* The assignment node's per-value text / expression choice (objectui#7588),
* rendered through `FlowNodeConfigField`, the editor's only caller, so the
* pins cover how the editor learns which map it is serving: the node type and
* field path read against the spec's expression ledger.
*/
// Paid at collection, not inside a clocked case (check:test-source-alias).
import '@objectstack/lint';
import { describe, it, expect, afterEach, vi } from 'vitest';
import { render, screen, cleanup, fireEvent } from '@testing-library/react';
import {
ASSIGNMENT_ARRAY_FORM_PRESCRIPTION,
ASSIGNMENT_VALUE_ENVELOPE_REFUSAL,
VALUE_SLOT_TEMPLATE_REFUSAL,
} from '@objectstack/spec/automation';
import { FlowNodeConfigField } from './FlowNodeConfigField';
import type { FlowConfigField } from './flow-node-config';
import { t } from '../i18n';
afterEach(cleanup);
const TOGGLE = 'Write as a formula';
const ASSIGNMENTS: FlowConfigField = {
id: 'assignments',
path: ['config', 'assignments'],
label: 'Assignments',
kind: 'keyValue',
};
const FIELD_VALUES: FlowConfigField = {
id: 'fields',
path: ['config', 'fields'],
label: 'Field values',
kind: 'keyValue',
};
function renderField(field: FlowConfigField, nodeType: string, value: unknown) {
const onCommit = vi.fn();
render(
<FlowNodeConfigField
field={field}
value={value}
onCommit={onCommit}
context={{ draft: {}, node: { id: 'n1', type: nodeType } }}
/>,
);
return onCommit;
}
describe('assignment value cell — text or CEL expression (objectui#7588)', () => {
it('offers the toggle on the assignment map, on the other value-role maps the ledger names, and on no other key/value map', () => {
renderField(ASSIGNMENTS, 'assignment', {
label: 'Weekly digest',
digest: { dialect: 'cel', source: 'joinNonEmpty(names, ", ")' },
});
const toggles = screen.getAllByRole('button', { name: TOGGLE });
expect(toggles.map((b) => b.getAttribute('aria-pressed'))).toEqual(['false', 'true']);
expect(screen.getByDisplayValue('joinNonEmpty(names, ", ")')).toBeTruthy();
// A stdlib call is not refused and not flagged.
expect(screen.queryByRole('alert')).toBeNull();
cleanup();
// `@objectstack/spec` 17.5.0 declares `create_record` / `update_record`
// `fields.*` as `value`-role slots in its expression ledger, so the same
// choice reaches those maps (objectui#11073). Through 17.4.0 this row
// asserted NO toggle on `create_record`'s field values. 18.0.0-next.0 adds
// `screen` `defaults.*`, `script` `inputs.*` and `subflow` `input.*`
// (objectui#12030); through 17.x `subflow`'s `input` was this case's control.
const VALUE_ROLE_MAPS: Array<[string, FlowConfigField]> = [
['create_record', FIELD_VALUES],
['update_record', FIELD_VALUES],
['screen', { id: 'defaults', path: ['config', 'defaults'], label: 'Defaults', kind: 'keyValue' }],
['script', { id: 'inputs', path: ['config', 'inputs'], label: 'Inputs', kind: 'keyValue' }],
['subflow', { id: 'input', path: ['config', 'input'], label: 'Input mapping', kind: 'keyValue' }],
];
for (const [nodeType, field] of VALUE_ROLE_MAPS) {
renderField(field, nodeType, { name: 'Acme', total: { dialect: 'cel', source: 'a + b' } });
expect(
screen.getAllByRole('button', { name: TOGGLE }).map((b) => b.getAttribute('aria-pressed')),
`${nodeType}: one toggle per value, the envelope pressed`,
).toEqual(['false', 'true']);
expect(screen.getByDisplayValue('a + b')).toBeTruthy();
cleanup();
}
// Control: a key/value map the ledger gives no `value` role keeps plain
// text cells — an object naming a `dialect` there is just data.
renderField({ id: 'headers', path: ['config', 'headers'], label: 'Headers', kind: 'keyValue' }, 'http', {
name: '{record.name}',
});
expect(screen.queryAllByRole('button', { name: TOGGLE })).toHaveLength(0);
});
it('commits a `{token}` string unchanged on blur, and shows the spec refusal of it', () => {
const onCommit = renderField(ASSIGNMENTS, 'assignment', { label: '{record.first} {record.last}' });
fireEvent.blur(screen.getByDisplayValue('{record.first} {record.last}'));
expect(onCommit).toHaveBeenLastCalledWith({ label: '{record.first} {record.last}' });
// `@objectstack/spec` 18 (ADR-0032 §3) reads a value-slot string as the
// literal it spells and refuses a `{…}` token in it; the cell shows that
// sentence, as it shows any other refusal (objectui#12030). Through 17.x a
// `{token}` string drew no alert here.
expect(screen.getByRole('alert').textContent).toContain(VALUE_SLOT_TEMPLATE_REFUSAL);
});
it('stores an expression as a `{ dialect, source }` envelope and leaves the text values alone', () => {
const onCommit = renderField(ASSIGNMENTS, 'assignment', { total: 'amount * 2', label: '{name}' });
fireEvent.click(screen.getAllByRole('button', { name: TOGGLE })[0]);
expect(onCommit).toHaveBeenLastCalledWith({ total: { dialect: 'cel', source: 'amount * 2' }, label: '{name}' });
expect(screen.getAllByRole('button', { name: TOGGLE })[0].getAttribute('aria-pressed')).toBe('true');
});
it('shows the spec refusal inline for a malformed envelope, and for a blank expression', () => {
renderField(ASSIGNMENTS, 'assignment', { bad: { dialect: 'template', source: 'Hi {name}' } });
expect(screen.getByRole('alert').textContent).toContain(ASSIGNMENT_VALUE_ENVELOPE_REFUSAL);
cleanup();
const onCommit = renderField(ASSIGNMENTS, 'assignment', { empty: '' });
expect(screen.queryByRole('alert')).toBeNull();
fireEvent.click(screen.getByRole('button', { name: TOGGLE }));
expect(onCommit).toHaveBeenLastCalledWith({ empty: { dialect: 'cel', source: '' } });
// objectstack#27799 — the spec's refusal of a blank expression, said in
// business words rather than the envelope's wire shape.
const alert = screen.getByRole('alert').textContent ?? '';
expect(alert).toBe(t('engine.inspector.flowNode.kv.expressionNeeded'));
expect(alert).not.toContain(ASSIGNMENT_VALUE_ENVELOPE_REFUSAL);
});
it('names the legacy array form with the spec prescription, and an expression writes the map', () => {
const onCommit = renderField(ASSIGNMENTS, 'assignment', [
{ variable: 'label', value: '{name}' },
{ variable: 'total', value: 'amount' },
]);
expect(screen.getByRole('note').textContent).toBe(ASSIGNMENT_ARRAY_FORM_PRESCRIPTION);
fireEvent.click(screen.getAllByRole('button', { name: TOGGLE })[1]);
expect(onCommit).toHaveBeenLastCalledWith({ label: '{name}', total: { dialect: 'cel', source: 'amount' } });
});
});