-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuseActionEngine.sharedRunner.test.tsx
More file actions
139 lines (127 loc) · 5.04 KB
/
Copy pathuseActionEngine.sharedRunner.test.tsx
File metadata and controls
139 lines (127 loc) · 5.04 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
/**
* 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.
*/
/**
* Regression suite for the `useActionEngine` × `<ActionProvider>` contract.
*
* Two behaviours are locked down here:
*
* 1. When mounted under `<ActionProvider>`, `useActionEngine` MUST share
* the provider's `ActionRunner` instance. The provider is the only
* place that has the platform-level handlers (`onConfirm`, `onToast`,
* `onParamCollection`, …) wired up — a nested standalone runner would
* silently no-op on `params:` / `confirmText` actions.
*
* 2. When the hook layers per-render context onto a shared runner, it
* must MERGE into the existing `ctx` namespace rather than replace it.
* The provider seeds `ctx: { record, user, objectName }`; if the hook
* wrote `ctx: { record, recordId, objectName }` wholesale it would
* wipe `ctx.user`, and every `record.id == ctx.user.id`-style
* predicate would throw → fail closed → every action hidden.
* This is the bug that hid Change Password / Delete My Account on the
* sys_user Security tab.
*/
import React from 'react';
import { describe, it, expect } from 'vitest';
import { renderHook } from '@testing-library/react';
import { useActionEngine } from '../useActionEngine';
import { ActionProvider, useAction } from '../../context/ActionContext';
const SELF_ID = 'user-self';
function withProvider(providerContext: any) {
return ({ children }: { children: React.ReactNode }) =>
React.createElement(ActionProvider, { context: providerContext }, children);
}
describe('useActionEngine — shared ActionProvider runner', () => {
it('reuses the provider runner instead of building a standalone one', () => {
// Render the hook and `useAction()` under the same provider tree and
// verify both observe the same in-memory `ActionRunner` instance.
// (Two separate `renderHook` calls would each instantiate their own
// provider, so identity comparison there is meaningless.)
const wrapper = withProvider({
record: { id: SELF_ID },
user: { id: SELF_ID },
objectName: 'sys_user',
});
const { result } = renderHook(
() => ({
hook: useActionEngine({ actions: [] }),
provider: useAction(),
}),
{ wrapper }
);
expect(result.current.hook.engine.getRunner()).toBe(result.current.provider.runner);
});
it('preserves provider `ctx.user` when hook layers per-render context (regression)', () => {
// The bug: the hook used to call `updateContext({ ctx: {record, recordId, objectName} })`
// which overwrote the provider's `ctx: {record, user, objectName}` wholesale,
// dropping `ctx.user`. Predicates like `record.id == ctx.user.id` then
// threw ReferenceError → fail-closed → every action hidden.
const wrapper = withProvider({
record: { id: SELF_ID },
user: { id: SELF_ID, name: 'Self' },
objectName: 'sys_user',
});
const { result } = renderHook(
() =>
useActionEngine({
actions: [
{
name: 'change_password',
type: 'script',
target: 'true',
visible: 'record.id == ctx.user.id',
locations: ['record_section'],
} as any,
],
context: {
// Per-render context that previously stomped the provider's ctx.
record: { id: SELF_ID },
recordId: SELF_ID,
objectName: 'sys_user',
},
}),
{ wrapper }
);
const visible = result.current.getActionsForLocation('record_section').map(a => a.name);
expect(visible).toContain('change_password');
// And verify the runner's resolved ctx still has `user` after the merge.
const ctxSnapshot = result.current.engine.getRunner().getEvaluator().getContext().toObject() as any;
expect(ctxSnapshot.ctx).toBeDefined();
expect(ctxSnapshot.ctx.user).toBeDefined();
expect(ctxSnapshot.ctx.user.id).toBe(SELF_ID);
// Per-render fields are also present.
expect(ctxSnapshot.ctx.record.id).toBe(SELF_ID);
});
it('still works standalone (no provider) with both flat and ctx accessors', () => {
const { result } = renderHook(() =>
useActionEngine({
actions: [
{
name: 'self_only_flat',
type: 'script',
target: 'true',
visible: 'record.id == user.id',
locations: ['record_section'],
} as any,
{
name: 'self_only_ctx',
type: 'script',
target: 'true',
visible: 'record.id == ctx.user.id',
locations: ['record_section'],
} as any,
],
context: {
record: { id: SELF_ID },
user: { id: SELF_ID },
},
})
);
const visible = result.current.getActionsForLocation('record_section').map(a => a.name);
expect(visible.sort()).toEqual(['self_only_ctx', 'self_only_flat']);
});
});