-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuseActionEngine.test.ts
More file actions
182 lines (154 loc) · 5.81 KB
/
Copy pathuseActionEngine.test.ts
File metadata and controls
182 lines (154 loc) · 5.81 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
172
173
174
175
176
177
178
179
180
181
182
/**
* Tests for useActionEngine hook
*/
import { describe, it, expect, vi } from 'vitest';
import { renderHook, act } from '@testing-library/react';
import { useActionEngine } from '../useActionEngine';
describe('useActionEngine', () => {
const sampleActions = [
{
name: 'mark_complete',
type: 'script',
target: 'true',
locations: ['list_toolbar', 'record_header'],
bulkEnabled: true,
},
{
name: 'delete_record',
type: 'api',
endpoint: '/api/delete',
locations: ['record_more'],
},
{
name: 'view_details',
type: 'url',
target: '/details',
locations: ['list_item'],
},
{
name: 'global_search',
type: 'script',
target: 'true',
locations: ['global_nav'],
shortcut: 'ctrl+k',
},
];
describe('getActionsForLocation', () => {
it('returns actions for list_toolbar', () => {
const { result } = renderHook(() =>
useActionEngine({ actions: sampleActions }),
);
const actions = result.current.getActionsForLocation('list_toolbar');
expect(actions.length).toBe(1);
expect(actions[0].name).toBe('mark_complete');
});
it('returns actions for record_header', () => {
const { result } = renderHook(() =>
useActionEngine({ actions: sampleActions }),
);
const actions = result.current.getActionsForLocation('record_header');
expect(actions.length).toBe(1);
expect(actions[0].name).toBe('mark_complete');
});
it('returns actions for record_more', () => {
const { result } = renderHook(() =>
useActionEngine({ actions: sampleActions }),
);
const actions = result.current.getActionsForLocation('record_more');
expect(actions.length).toBe(1);
expect(actions[0].name).toBe('delete_record');
});
it('returns empty array for location with no actions', () => {
const { result } = renderHook(() =>
useActionEngine({ actions: sampleActions }),
);
const actions = result.current.getActionsForLocation('record_related');
expect(actions.length).toBe(0);
});
});
describe('getBulkActions', () => {
// Deliberately INVERTED. `sampleActions` still carries the stale
// `bulkEnabled: true` — spec 17 retired the key as a `retiredKey()`
// tombstone, so metadata like this no longer parses at all, and harvesting
// it here made a dead registration option look load-bearing. Same posture
// as plugin-grid's "ignores a stale bulkEnabled flag on an object action".
// The engine's bulk mechanics keep their coverage in ActionEngine.test.ts,
// where a HOST passes `{ bulkEnabled: true }` to `registerAction`
// explicitly — which is still supported.
it('does not harvest the retired bulkEnabled key from metadata', () => {
const { result } = renderHook(() =>
useActionEngine({ actions: sampleActions }),
);
expect(result.current.getBulkActions()).toEqual([]);
});
});
describe('executeAction', () => {
it('executes a registered action by name', async () => {
const { result } = renderHook(() =>
useActionEngine({ actions: sampleActions }),
);
let actionResult: any;
await act(async () => {
actionResult = await result.current.executeAction('mark_complete');
});
expect(actionResult.success).toBe(true);
});
it('returns error for unregistered action', async () => {
const { result } = renderHook(() =>
useActionEngine({ actions: sampleActions }),
);
let actionResult: any;
await act(async () => {
actionResult = await result.current.executeAction('nonexistent');
});
expect(actionResult.success).toBe(false);
expect(actionResult.error).toContain('not found');
});
});
describe('handleShortcut', () => {
// Deliberately INVERTED, for the same reason as getBulkActions above:
// `sampleActions` still declares the stale `shortcut: 'ctrl+k'`, which
// spec 17 retired. Its tombstone is explicit that nothing ever consumed it
// ("no keydown listener feeds ActionEngine.getShortcuts(), and objectui's
// keyboard stack is hand-registered and never consults action metadata"),
// so harvesting it only kept a dead path looking alive. A host that wants
// a shortcut still passes one to `registerAction` explicitly.
it('does not harvest the retired shortcut key from metadata', async () => {
const { result } = renderHook(() =>
useActionEngine({ actions: sampleActions }),
);
let shortcutResult: any;
await act(async () => {
shortcutResult = await result.current.handleShortcut('ctrl+k');
});
expect(shortcutResult).toBeNull();
});
it('returns null for unregistered shortcut', async () => {
const { result } = renderHook(() =>
useActionEngine({ actions: sampleActions }),
);
let shortcutResult: any;
await act(async () => {
shortcutResult = await result.current.handleShortcut('ctrl+z');
});
expect(shortcutResult).toBeNull();
});
});
describe('defaults', () => {
it('works with no options', () => {
const { result } = renderHook(() => useActionEngine());
expect(result.current.engine).toBeDefined();
expect(typeof result.current.getActionsForLocation).toBe('function');
expect(typeof result.current.getBulkActions).toBe('function');
expect(typeof result.current.executeAction).toBe('function');
expect(typeof result.current.handleShortcut).toBe('function');
});
it('works with empty actions array', () => {
const { result } = renderHook(() =>
useActionEngine({ actions: [] }),
);
const actions = result.current.getActionsForLocation('list_toolbar');
expect(actions).toEqual([]);
});
});
});