-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBaseNode.test.tsx
More file actions
339 lines (303 loc) · 12.8 KB
/
Copy pathBaseNode.test.tsx
File metadata and controls
339 lines (303 loc) · 12.8 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { render, screen } from '@testing-library/react';
import type { Node, NodeProps } from '@uipath/apollo-react/canvas/xyflow/react';
import { Position } from '@uipath/apollo-react/canvas/xyflow/react';
import { describe, expect, it, vi } from 'vitest';
import type { BaseNodeData } from './BaseNode.types';
const DEFAULT_MANIFEST = {
display: { label: 'Test Node', shape: 'square', icon: 'test-icon' },
handleConfiguration: [],
} as const;
// Hoisted mocks — available inside vi.mock factories
const {
mockUpdateNode,
mockHandleConfigs,
mockManifest,
mockUseButtonHandles,
mockOverrideConfig,
} = vi.hoisted(() => ({
mockUpdateNode: vi.fn(),
mockHandleConfigs: { current: undefined as HandleGroupManifest[] | undefined },
mockManifest: {
current: {
display: { label: 'Test Node', shape: 'square', icon: 'test-icon' },
handleConfiguration: [],
} as Record<string, unknown>,
},
// biome-ignore lint/suspicious/noExplicitAny: hook receives a broad typed options object
mockUseButtonHandles: vi.fn() as any,
mockOverrideConfig: { current: {} as Record<string, unknown> },
}));
// xyflow is globally mocked in canvas-mocks.ts; extend with test-specific overrides.
vi.mock('@uipath/apollo-react/canvas/xyflow/react', async (importOriginal) => ({
...(await importOriginal<typeof import('@uipath/apollo-react/canvas/xyflow/react')>()),
useStore: () => false,
useUpdateNodeInternals: () => vi.fn(),
useReactFlow: () => ({ updateNodeData: vi.fn(), updateNode: mockUpdateNode }),
}));
vi.mock('../../hooks', () => ({
useNodeExecutionState: () => undefined,
useElementValidationStatus: () => undefined,
}));
vi.mock('../../core', () => ({
useNodeTypeRegistry: () => ({
getManifest: () => mockManifest.current,
}),
}));
vi.mock('../BaseCanvas/BaseCanvasModeProvider', () => ({
useBaseCanvasMode: () => ({ mode: 'design' }),
}));
vi.mock('../BaseCanvas/ConnectedHandlesContext', () => ({ useConnectedHandles: () => new Set() }));
vi.mock('../BaseCanvas/SelectionStateContext', () => ({
useSelectionState: () => ({ multipleNodesSelected: false }),
}));
vi.mock('../BaseCanvas/CanvasThemeContext', () => ({
useCanvasTheme: () => ({ isDarkMode: false }),
}));
vi.mock('../ButtonHandle/useButtonHandles', () => ({
useButtonHandles: (opts: unknown) => {
mockUseButtonHandles(opts);
return null;
},
}));
vi.mock('../ButtonHandle/SmartHandle', () => ({
SmartHandle: () => null,
SmartHandleProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
}));
vi.mock('./BaseNodeConfigContext', () => ({
useBaseNodeOverrideConfig: () => ({
handleConfigurations: mockHandleConfigs.current,
...mockOverrideConfig.current,
}),
}));
vi.mock('../../utils/adornment-resolver', () => ({ resolveAdornments: () => ({}) }));
vi.mock('../../utils/toolbar-resolver', () => ({ resolveToolbar: () => undefined }));
vi.mock('@uipath/apollo-wind', () => ({
Skeleton: (props: Record<string, unknown>) => <div {...props} />,
cn: (...args: unknown[]) =>
args
.flat(Infinity)
.filter((v): v is string => typeof v === 'string' && v.length > 0)
.join(' '),
}));
vi.mock('@uipath/apollo-react/canvas/utils', () => ({
cx: (...args: unknown[]) => args.filter(Boolean).join(' '),
}));
vi.mock('../../utils/icon-registry', () => ({
getIcon: () => () => <div data-testid="node-icon">Icon</div>,
}));
vi.mock('../../utils/manifest-resolver', () => ({
resolveDisplay: (display: Record<string, unknown> | undefined) => ({
label: 'Test Node',
subLabel: 'Test SubLabel',
shape: 'square',
icon: 'test-icon',
...display,
}),
resolveHandles: () => [],
}));
vi.mock('./NodeLabel', () => ({
NodeLabel: ({ label }: { label?: string }) => <div data-testid="node-label">{label}</div>,
}));
import type { HandleGroupManifest } from '../../schema';
import { BaseNode } from './BaseNode';
const defaultProps: NodeProps<Node<BaseNodeData>> = {
id: 'test-node',
type: 'test',
data: {},
selected: false,
dragging: false,
draggable: true,
zIndex: 0,
isConnectable: true,
positionAbsoluteX: 0,
positionAbsoluteY: 0,
selectable: true,
deletable: true,
};
// Handle spacing: 2.5 grid units × 16px = 40px per handle
const makeHandles = (position: Position, count: number): HandleGroupManifest[] => [
{
position,
handles: Array.from({ length: count }, (_, i) => ({
id: `h${i}`,
type: 'target',
handleType: 'input',
})),
},
];
describe('BaseNode', () => {
afterEach(() => {
mockUpdateNode.mockClear();
mockUseButtonHandles.mockClear();
mockHandleConfigs.current = undefined;
mockManifest.current = { ...DEFAULT_MANIFEST };
mockOverrideConfig.current = {};
});
describe('Height computation', () => {
// Override the rAF mock from canvas-mocks to run synchronously
// so updateNode calls complete during the effect flush within act/render.
const savedRAF = window.requestAnimationFrame;
beforeEach(() => {
window.requestAnimationFrame = (cb) => {
cb(performance.now());
return 0;
};
});
afterEach(() => {
window.requestAnimationFrame = savedRAF;
});
it('expands height when handles exceed base height', () => {
// 4 handles × 40px = 160px > 96px default
mockHandleConfigs.current = makeHandles(Position.Left, 4);
render(<BaseNode {...defaultProps} height={96} />);
expect(mockUpdateNode).toHaveBeenCalledWith('test-node', { height: 160 });
});
it('does not expand when handles fit within base height', () => {
// 2 handles × 40px = 80px < 96px default
mockHandleConfigs.current = makeHandles(Position.Left, 2);
render(<BaseNode {...defaultProps} height={96} />);
expect(mockUpdateNode).not.toHaveBeenCalled();
});
it('respects user-provided height as the base', () => {
// height=200, 2 handles need 80px < 200 → no expansion
mockHandleConfigs.current = makeHandles(Position.Left, 2);
render(<BaseNode {...defaultProps} height={200} />);
expect(mockUpdateNode).not.toHaveBeenCalled();
});
it('shrinks back to default when handles are removed', () => {
// Start with 4 handles (160px needed)
mockHandleConfigs.current = makeHandles(Position.Left, 4);
const { rerender } = render(<BaseNode {...defaultProps} height={96} />);
expect(mockUpdateNode).toHaveBeenCalledWith('test-node', { height: 160 });
// Simulate React Flow updating height to match our sync
// Note: new data={{}} ref on each rerender to break through React.memo
mockUpdateNode.mockClear();
rerender(<BaseNode {...defaultProps} height={160} data={{}} />);
expect(mockUpdateNode).not.toHaveBeenCalled();
// Remove handles — should shrink back to DEFAULT_NODE_SIZE (96)
mockHandleConfigs.current = [];
mockUpdateNode.mockClear();
rerender(<BaseNode {...defaultProps} height={160} data={{}} />);
expect(mockUpdateNode).toHaveBeenCalledWith('test-node', { height: 96 });
});
it('shrinks back to user-provided height after handle removal', () => {
// User height=200, 8 handles need 288px
mockHandleConfigs.current = makeHandles(Position.Left, 8);
const { rerender } = render(<BaseNode {...defaultProps} height={200} />);
expect(mockUpdateNode).toHaveBeenCalledWith('test-node', { height: 288 });
// Simulate React Flow updating height
mockUpdateNode.mockClear();
rerender(<BaseNode {...defaultProps} height={288} data={{}} />);
// Remove handles — should return to 200, not DEFAULT_NODE_SIZE (96)
mockHandleConfigs.current = [];
mockUpdateNode.mockClear();
rerender(<BaseNode {...defaultProps} height={288} data={{}} />);
expect(mockUpdateNode).toHaveBeenCalledWith('test-node', { height: 200 });
});
it('uses max of left and right handle counts', () => {
// 2 left, 4 right → uses 4 → 160px
mockHandleConfigs.current = [
...makeHandles(Position.Left, 2),
...makeHandles(Position.Right, 4),
];
render(<BaseNode {...defaultProps} height={96} />);
expect(mockUpdateNode).toHaveBeenCalledWith('test-node', { height: 160 });
});
});
describe('Loading state', () => {
it.each([
{ loading: true, expectSkeleton: true },
{ loading: false, expectSkeleton: false },
{ loading: undefined, expectSkeleton: false },
])('renders skeleton=$expectSkeleton when loading=$loading', ({ loading, expectSkeleton }) => {
const data = loading !== undefined ? { loading } : {};
render(<BaseNode {...defaultProps} data={data} />);
if (expectSkeleton) {
expect(screen.getByTestId('skeleton-icon')).toBeInTheDocument();
expect(screen.queryByTestId('node-icon')).not.toBeInTheDocument();
} else {
expect(screen.queryByTestId('skeleton-icon')).not.toBeInTheDocument();
expect(screen.getByTestId('node-icon')).toBeInTheDocument();
}
});
it('still renders label when loading', () => {
render(<BaseNode {...defaultProps} data={{ loading: true }} />);
expect(screen.getByTestId('skeleton-icon')).toBeInTheDocument();
expect(screen.getByText('Test Node')).toBeInTheDocument();
});
});
describe('Stacked treatment', () => {
it('sets data-stacked when manifest.drillable is true', () => {
mockManifest.current = { ...DEFAULT_MANIFEST, drillable: true };
render(<BaseNode {...defaultProps} />);
expect(screen.getByTestId('base-container')).toHaveAttribute('data-stacked', 'true');
});
it('sets data-stacked when data.isCollapsed is true', () => {
render(<BaseNode {...defaultProps} data={{ isCollapsed: true }} />);
expect(screen.getByTestId('base-container')).toHaveAttribute('data-stacked', 'true');
});
it('omits data-stacked when neither drillable nor collapsed', () => {
render(<BaseNode {...defaultProps} />);
expect(screen.getByTestId('base-container')).not.toHaveAttribute('data-stacked');
});
});
// Initials badge fallback (Priority 3 in BaseNode's icon resolution): when
// neither `iconComponent` nor `display.icon` is supplied, render the
// shared InitialsBadge derived from `display.label` so missing icons look
// consistent with the canvas ListView's same fallback.
//
// Note: the resolveDisplay mock above pre-fills `icon: 'test-icon'`, so
// tests must explicitly pass an empty-string sentinel to model the
// "no icon" path that resolveDisplay produces in reality (option-b
// sentinel — see manifest-resolver.ts).
describe('Initials badge fallback', () => {
it('renders the initials badge when display.icon is the empty-string sentinel', () => {
mockManifest.current = {
...DEFAULT_MANIFEST,
display: { label: 'Microsoft Foundry', shape: 'square', icon: '' },
};
render(<BaseNode {...defaultProps} />);
// The badge renders aria-hidden with the first character of the label.
const badge = screen.getByText('M', { selector: '[aria-hidden="true"]' });
expect(badge).toBeInTheDocument();
});
it('does not render the initials badge when display.icon is provided', () => {
mockManifest.current = {
...DEFAULT_MANIFEST,
display: { label: 'Microsoft Foundry', shape: 'square', icon: 'star' },
};
render(<BaseNode {...defaultProps} />);
// No aria-hidden 'M' span — the registered icon takes the slot.
expect(screen.queryByText('M', { selector: '[aria-hidden="true"]' })).not.toBeInTheDocument();
});
});
// BaseNode forwards the consumer-facing `onHandleMouseEnter`/`onHandleMouseLeave`
// from `BaseNodeOverrideConfigProvider` into `useButtonHandles` as
// `handleMouseEnter`/`handleMouseLeave`. Trigger those by reaching into the
// captured hook arguments and invoking them with a synthetic payload.
describe('Handle hover handlers', () => {
it('forwards onHandleMouseEnter/onHandleMouseLeave overrides and fires with the payload', () => {
const onHandleMouseEnter = vi.fn();
const onHandleMouseLeave = vi.fn();
mockOverrideConfig.current = { onHandleMouseEnter, onHandleMouseLeave };
render(<BaseNode {...defaultProps} />);
expect(mockUseButtonHandles).toHaveBeenCalled();
const opts = mockUseButtonHandles.mock.calls.at(-1)?.[0] as {
handleMouseEnter?: (e: unknown) => void;
handleMouseLeave?: (e: unknown) => void;
};
expect(opts.handleMouseEnter).toBe(onHandleMouseEnter);
expect(opts.handleMouseLeave).toBe(onHandleMouseLeave);
const payload = {
handleId: 'output',
nodeId: 'test-node',
handleType: 'output',
position: Position.Right,
};
opts.handleMouseEnter?.(payload);
opts.handleMouseLeave?.(payload);
expect(onHandleMouseEnter).toHaveBeenCalledWith(payload);
expect(onHandleMouseLeave).toHaveBeenCalledWith(payload);
});
});
});