-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSendMessageForm.test.tsx
More file actions
342 lines (277 loc) · 11.2 KB
/
Copy pathSendMessageForm.test.tsx
File metadata and controls
342 lines (277 loc) · 11.2 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
340
341
342
import { stringify } from '@metamask/kernel-utils';
import { setupOcapKernelMock } from '@ocap/repo-tools/test-utils';
import {
render,
screen,
fireEvent,
waitFor,
cleanup,
within,
} from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import type { PanelContextType } from '../context/PanelContext.tsx';
import { usePanelContext } from '../context/PanelContext.tsx';
import { useRegistry } from '../hooks/useRegistry.ts';
import type { ObjectRegistry } from '../types.ts';
import { SendMessageForm } from './SendMessageForm.tsx';
const { resetMocks, setMockBehavior } = setupOcapKernelMock();
vi.mock('../context/PanelContext.tsx', () => ({
usePanelContext: vi.fn(),
}));
vi.mock('../hooks/useRegistry.ts', () => ({
useRegistry: vi.fn(),
}));
vi.mock('@metamask/kernel-utils', async (importOriginal) => ({
...(await importOriginal()),
stringify: vi.fn(),
}));
describe('SendMessageForm Component', () => {
const callKernelMethod = vi.fn();
const logMessage = vi.fn();
const fetchObjectRegistry = vi.fn();
const mockObjectRegistry: ObjectRegistry = {
gcActions: '',
reapQueue: '',
terminatedVats: '',
vats: {
vat1: {
overview: { name: 'TestVat1', bundleSpec: '' },
ownedObjects: [
{
kref: 'ko1',
eref: 'eref1',
refCount: '1',
toVats: [],
revoked: 'false',
},
{
kref: 'ko2',
eref: 'eref2',
refCount: '1',
toVats: [],
revoked: 'false',
},
],
importedObjects: [],
importedPromises: [],
exportedPromises: [],
},
vat2: {
overview: { name: 'TestVat2', bundleSpec: '' },
ownedObjects: [
{
kref: 'ko3',
eref: 'eref3',
refCount: '1',
toVats: [],
revoked: 'false',
},
],
importedObjects: [
{
kref: 'ko4',
eref: 'eref4',
refCount: '1',
fromVat: 'vat1',
},
],
importedPromises: [],
exportedPromises: [],
},
},
};
beforeEach(() => {
vi.mocked(stringify).mockImplementation((value) =>
JSON.stringify(value, null, 2),
);
vi.mocked(useRegistry).mockReturnValue({
fetchObjectRegistry,
} as unknown as ReturnType<typeof useRegistry>);
vi.mocked(usePanelContext).mockReturnValue({
callKernelMethod,
logMessage,
objectRegistry: mockObjectRegistry,
} as unknown as PanelContextType);
callKernelMethod.mockResolvedValue({ body: 'success', slots: [] });
});
afterEach(() => {
cleanup();
resetMocks();
vi.resetModules();
});
it('renders nothing when objectRegistry is null', async () => {
vi.mocked(usePanelContext).mockReturnValue({
callKernelMethod,
logMessage,
objectRegistry: null,
} as unknown as PanelContextType);
const { container } = render(<SendMessageForm />);
expect(container.firstChild).toBeNull();
});
it('renders form with correct initial values when objectRegistry is available', async () => {
const { getByTestId } = render(<SendMessageForm />);
// Check form elements are rendered
expect(screen.getByText('Send Message')).toBeInTheDocument();
expect(screen.getByLabelText('Target:')).toBeInTheDocument();
expect(screen.getByLabelText('Method:')).toBeInTheDocument();
expect(screen.getByLabelText('Params (JSON):')).toBeInTheDocument();
expect(screen.getByText('Send')).toBeInTheDocument();
// Check initial values
expect(screen.getByDisplayValue('__getMethodNames__')).toBeInTheDocument();
expect(screen.getByDisplayValue('[]')).toBeInTheDocument();
// Check target dropdown contains the expected options
const targetSelect = getByTestId('message-target');
expect(targetSelect).toBeInTheDocument();
expect(targetSelect).toHaveValue('');
const options = within(targetSelect).getAllByRole('option');
expect(options).toHaveLength(5); // 1 placeholder + 4 options from mock registry
// Check dropdown options include objects from the registry
expect(screen.getByText('ko1 (TestVat1)')).toBeInTheDocument();
expect(screen.getByText('ko2 (TestVat1)')).toBeInTheDocument();
expect(screen.getByText('ko3 (TestVat2)')).toBeInTheDocument();
expect(screen.getByText('ko4 (TestVat1)')).toBeInTheDocument();
});
it('updates form values when inputs change', async () => {
const { getByTestId } = render(<SendMessageForm />);
// Change target
const targetSelect = getByTestId('message-target');
fireEvent.change(targetSelect, { target: { value: 'ko1' } });
expect(targetSelect).toHaveValue('ko1');
// Change method
const methodInput = getByTestId('message-method');
await userEvent.clear(methodInput);
await userEvent.type(methodInput, 'testMethod');
expect(methodInput).toHaveValue('testMethod');
// Change params - using fireEvent.change instead of userEvent.type
const paramsInput = getByTestId('message-params');
fireEvent.change(paramsInput, { target: { value: '["arg1", "arg2"]' } });
expect(paramsInput).toHaveValue('["arg1", "arg2"]');
});
it('disables Send button when target is empty', async () => {
const { getByTestId } = render(<SendMessageForm />);
// Initially button should be disabled (no target selected)
const sendButton = getByTestId('message-send-button');
expect(sendButton).toBeDisabled();
// Select a target
const targetSelect = getByTestId('message-target');
fireEvent.change(targetSelect, { target: { value: 'ko1' } });
// Button should now be enabled
expect(sendButton).not.toBeDisabled();
// Clear method
const methodInput = getByTestId('message-method');
await userEvent.clear(methodInput);
// Button should be disabled again
expect(sendButton).toBeDisabled();
});
it('does not set target when value fails KRef validation', async () => {
setMockBehavior({ isKRef: false });
const { getByTestId } = render(<SendMessageForm />);
const targetSelect = getByTestId('message-target');
fireEvent.change(targetSelect, { target: { value: 'not-a-kref' } });
// Target should remain unset, button stays disabled
expect(targetSelect).toHaveValue('');
expect(getByTestId('message-send-button')).toBeDisabled();
});
it('calls callKernelMethod with correct parameters when Send button is clicked', async () => {
const { getByTestId } = render(<SendMessageForm />);
// Set up form values
const targetSelect = getByTestId('message-target');
fireEvent.change(targetSelect, { target: { value: 'ko1' } });
const methodInput = getByTestId('message-method');
fireEvent.change(methodInput, { target: { value: 'testMethod' } });
const paramsInput = getByTestId('message-params');
fireEvent.change(paramsInput, { target: { value: '["arg1", "arg2"]' } });
// Parse expected args to match what the component will do
const expectedArgs = ['arg1', 'arg2'];
// Click send button
const sendButton = getByTestId('message-send-button');
await userEvent.click(sendButton);
// Check if callKernelMethod was called with correct parameters
expect(callKernelMethod).toHaveBeenCalledWith({
method: 'queueMessage',
params: ['ko1', 'testMethod', expectedArgs],
});
// Check if fetchObjectRegistry was called
await waitFor(() => {
expect(fetchObjectRegistry).toHaveBeenCalled();
});
});
it('logs error when callKernelMethod fails', async () => {
const testError = new Error('Test error');
callKernelMethod.mockRejectedValueOnce(testError);
const { getByTestId } = render(<SendMessageForm />);
// Set up form values and submit
const targetSelect = getByTestId('message-target');
fireEvent.change(targetSelect, { target: { value: 'ko1' } });
const sendButton = getByTestId('message-send-button');
await userEvent.click(sendButton);
// Check if error was logged
await waitFor(() => {
expect(logMessage).toHaveBeenCalledWith(String(testError), 'error');
});
});
it('displays response after successful submission', async () => {
const mockResponse = { body: 'Success response', slots: [] };
callKernelMethod.mockResolvedValueOnce(mockResponse);
// Mock stringify to return a consistent string
const mockResponseString = JSON.stringify(mockResponse, null, 2);
vi.mocked(stringify).mockReturnValueOnce(mockResponseString);
const { getByTestId } = render(<SendMessageForm />);
// Set up form values and submit
const targetSelect = getByTestId('message-target');
fireEvent.change(targetSelect, { target: { value: 'ko1' } });
const sendButton = getByTestId('message-send-button');
await userEvent.click(sendButton);
// Check if response section is displayed
await waitFor(() => {
const responseHeading = screen.getByText('Response:');
expect(responseHeading).toBeInTheDocument();
// Find the pre element that contains the response
const preElement = responseHeading.parentElement?.querySelector('pre');
expect(preElement).toBeInTheDocument();
expect(preElement?.textContent).toContain('Success response');
});
});
it('handles invalid JSON in params input', async () => {
const { getByTestId } = render(<SendMessageForm />);
// Set up form values with invalid JSON
const targetSelect = getByTestId('message-target');
fireEvent.change(targetSelect, { target: { value: 'ko1' } });
const paramsInput = getByTestId('message-params');
fireEvent.change(paramsInput, { target: { value: 'invalid json' } });
// Click send button
const sendButton = getByTestId('message-send-button');
await userEvent.click(sendButton);
// Now the JSON parse error is caught by the component's catch handler
await waitFor(() => {
expect(logMessage).toHaveBeenCalledWith(
expect.stringContaining('SyntaxError'),
'error',
);
});
});
it('triggers submission when Enter key is pressed in input fields', async () => {
const { getByTestId } = render(<SendMessageForm />);
// Set up form values - we need a valid target and valid JSON
const targetSelect = getByTestId('message-target');
fireEvent.change(targetSelect, { target: { value: 'ko1' } });
// Ensure we have valid JSON in the params field
const paramsInput = getByTestId('message-params');
fireEvent.change(paramsInput, { target: { value: '[]' } });
// Press Enter in method input - using fireEvent for better control
const methodInput = getByTestId('message-method');
fireEvent.keyDown(methodInput, { key: 'Enter', code: 'Enter' });
// Wait for the async handleSend to be called
await waitFor(() => {
expect(callKernelMethod).toHaveBeenCalled();
});
callKernelMethod.mockClear();
// Press Enter in params input
fireEvent.keyDown(paramsInput, { key: 'Enter', code: 'Enter' });
// Wait for the async handleSend to be called
await waitFor(() => {
expect(callKernelMethod).toHaveBeenCalled();
});
});
});