Repository navigation
Expand file tree
/
Copy patheditFormLockedByState.test.ts
More file actions
171 lines (145 loc) · 7.41 KB
/
Copy patheditFormLockedByState.test.ts
File metadata and controls
171 lines (145 loc) · 7.41 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* `editFormLockedByState` (objectstack#32756): `true` only when the record's
* state locks every field the edit form draws, and fail-open everywhere else.
*/
import { describe, it, expect, vi } from 'vitest';
import { editFormLockedByState } from './editFormLockedByState';
const LOCKED_WHEN = "record.status == 'approved'";
const APPROVED = { id: 'r1', status: 'approved', amount: 1 };
const locked = {
name: 'claim',
fields: {
id: { type: 'text' },
owner: { type: 'lookup', reference: 'sys_user' },
status: { type: 'text', readonlyWhen: LOCKED_WHEN },
amount: { type: 'number', readonlyWhen: LOCKED_WHEN },
},
};
describe('editFormLockedByState (objectstack#32756)', () => {
it('is true when readonlyWhen locks every drawn field', () => {
expect(editFormLockedByState(locked, APPROVED)).toBe(true);
});
it('reads the array spelling of the field list too', () => {
const asArray = { name: 'claim', fields: Object.entries(locked.fields).map(([name, f]) => ({ name, ...f })) };
expect(editFormLockedByState(asArray, APPROVED)).toBe(true);
});
it('is false when the predicate does not hold for the record', () => {
expect(editFormLockedByState(locked, { ...APPROVED, status: 'draft' })).toBe(false);
});
it('is false when any drawn field stays writable', () => {
const withNote = { ...locked, fields: { ...locked.fields, note: { type: 'text' } } };
expect(editFormLockedByState(withNote, APPROVED)).toBe(false);
});
it('ignores a writable field the form does not draw (hidden, or one its visibleWhen hides)', () => {
const hiddenNote = {
...locked,
fields: {
...locked.fields,
note: { type: 'text', hidden: true },
memo: { type: 'text', visibleWhen: "record.status == 'draft'" },
},
};
expect(editFormLockedByState(hiddenNote, APPROVED)).toBe(true);
});
it('counts a server-computed field as read-only, but never as the state lock', () => {
const withFormula = { ...locked, fields: { ...locked.fields, total: { type: 'formula' } } };
expect(editFormLockedByState(withFormula, APPROVED)).toBe(true);
const formulaOnly = { name: 'claim', fields: { total: { type: 'formula' }, calc: { type: 'text', expression: 'record.a' } } };
expect(editFormLockedByState(formulaOnly, APPROVED)).toBe(false);
});
it('is false for an object read-only by static flags alone: no state locks it', () => {
const staticOnly = { name: 'claim', fields: { amount: { type: 'number', readonly: true } } };
expect(editFormLockedByState(staticOnly, APPROVED)).toBe(false);
});
it('fails open on a predicate that faults', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const broken = { ...locked, fields: { ...locked.fields, amount: { type: 'number', readonlyWhen: 'record.status ==' } } };
expect(editFormLockedByState(broken, APPROVED)).toBe(false);
warn.mockRestore();
});
it('is false when the field groups place an inline child collection (objectstack-ai/objectstack#36425)', () => {
const withSubforms = { ...locked, groupSubforms: [{ childObject: 'claim_line' }] };
expect(editFormLockedByState(withSubforms, APPROVED)).toBe(false);
});
it('CONTROL: an internal form view\'s subforms are not read', () => {
const withFormSubforms = { ...locked, form: { subforms: [{ childObject: 'claim_line' }] } } as any;
expect(editFormLockedByState(withFormSubforms, APPROVED)).toBe(true);
});
it('is false with no object definition or no record', () => {
expect(editFormLockedByState(undefined, APPROVED)).toBe(false);
expect(editFormLockedByState(locked, null)).toBe(false);
});
});
/**
* #37402 — the object-level hand lock is the record's state too: an object
* whose `uiReadonlyWhen` holds greys Edit when it locks every drawn field, and
* a field that overrides it (`uiReadonlyWhen: 'false'`) keeps Edit live.
*/
describe('editFormLockedByState reads the object-level uiReadonlyWhen (#37402)', () => {
const CONFIRMED = { id: 'o1', status: 'confirmed', amount: 1, notes: 'n' };
const order = (notes: Record<string, unknown>) => ({
name: 'order',
uiReadonlyWhen: "record.status in ['confirmed', 'done']",
fields: { status: { type: 'text' }, amount: { type: 'number' }, notes: { type: 'text', ...notes } },
});
it('is true when the object lock holds and no field overrides it', () => {
expect(editFormLockedByState(order({}), CONFIRMED)).toBe(true);
});
it('is false when a field keeps itself editable with its own uiReadonlyWhen', () => {
expect(editFormLockedByState(order({ uiReadonlyWhen: 'false' }), CONFIRMED)).toBe(false);
});
it('is false while the object predicate does not hold', () => {
expect(editFormLockedByState(order({}), { ...CONFIRMED, status: 'draft' })).toBe(false);
});
});
/**
* #38249 — an object's whole-record hand lock locks its inline lines too, so
* an object that places a child collection greys Edit when the lock holds and
* no field of the child can open a line cell. Without the child's definition,
* or when a child field owns a lock that could open, Edit stays live.
*/
describe('editFormLockedByState with inline lines under the object-level uiReadonlyWhen (#38249)', () => {
const CONFIRMED = { id: 'o1', status: 'confirmed', amount: 1 };
const order = {
name: 'order',
uiReadonlyWhen: "record.status == 'confirmed'",
fields: { status: { type: 'text' }, amount: { type: 'number' } },
groupSubforms: [{ childObject: 'order_line', relationshipField: 'order' }],
};
const line = (qty: Record<string, unknown> = {}) => ({
name: 'order_line',
fields: { order: { type: 'master_detail', reference: 'order' }, qty: { type: 'number', ...qty } },
});
const defs = (child: unknown) => (name: string) => (name === 'order_line' ? (child as any) : undefined);
it('is true when the object lock holds and no child field can open a line cell', () => {
expect(editFormLockedByState(order, CONFIRMED, undefined, defs(line()))).toBe(true);
});
it("is false when a child field keeps its cell editable with its own uiReadonlyWhen 'false'", () => {
expect(editFormLockedByState(order, CONFIRMED, undefined, defs(line({ uiReadonlyWhen: 'false' })))).toBe(false);
});
it('is true when the child field owns only a static lock', () => {
expect(editFormLockedByState(order, CONFIRMED, undefined, defs(line({ uiReadonly: true })))).toBe(true);
});
it('is false while the object predicate does not hold', () => {
expect(editFormLockedByState(order, { ...CONFIRMED, status: 'draft' }, undefined, defs(line()))).toBe(false);
});
it('is false when the header is locked only field by field: that leaves the lines open', () => {
const { uiReadonlyWhen: _drop, ...plain } = order;
const byField = {
...plain,
fields: { status: { type: 'text', readonlyWhen: "record.status == 'confirmed'" }, amount: { type: 'number', readonlyWhen: "record.status == 'confirmed'" } },
};
expect(editFormLockedByState(byField, CONFIRMED, undefined, defs(line()))).toBe(false);
});
it("fails open without the child object's definition", () => {
expect(editFormLockedByState(order, CONFIRMED)).toBe(false);
expect(editFormLockedByState(order, CONFIRMED, undefined, () => undefined)).toBe(false);
});
});