-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathSubmitMethod.test.js
More file actions
494 lines (403 loc) · 14.4 KB
/
Copy pathSubmitMethod.test.js
File metadata and controls
494 lines (403 loc) · 14.4 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
* Tests for submit() method in edit.js
*
* These tests ensure that the submit method works correctly with the new
* dataToSubmit parameter and maintains backward compatibility.
*/
// Mock ProcessMaker global
global.ProcessMaker = {
apiClient: {
put: jest.fn(),
},
alert: jest.fn(),
};
// Mock global alert
global.window = {
ProcessMaker: {
alert: jest.fn(),
},
};
// Mock lodash
global._ = {
intersection: jest.fn((a, b) => a.filter(value => b.includes(value))),
pick: jest.fn((obj, keys) => {
const result = {};
keys.forEach(key => {
if (obj.hasOwnProperty(key)) {
result[key] = obj[key];
}
});
return result;
}),
};
// Create test component with submit method
const createTestComponent = () => ({
isSelfService: false,
submitting: false,
task: {
id: 123,
screen: null,
},
$t: (key) => key, // Mock translation
$refs: {
task: {
loadNextAssignedTask: jest.fn(),
},
},
processCollectionData: jest.fn(() => null),
submit(task, dataToSubmit) {
if (this.isSelfService) {
ProcessMaker.alert(this.$t("Claim the Task to continue."), "warning");
return;
}
if (this.submitting) {
return;
}
// Process collection data
const resultCollectionComponent = this.processCollectionData(this.task);
const messageCollection = this.$t("Collection data was updated");
if (resultCollectionComponent && resultCollectionComponent.length > 0) {
resultCollectionComponent.forEach((result) => {
if (result.submitCollectionChecked) {
const collectionKeys = Object.keys(result.collectionFields);
const matchingKeys = _.intersection(Object.keys(dataToSubmit), collectionKeys);
const collectionsData = _.pick(dataToSubmit, matchingKeys);
ProcessMaker.apiClient
.put(`collections/${result.collectionId}/records/${result.recordId}`, {
data: collectionsData,
uploads: [],
})
.then(() => {
window.ProcessMaker.alert(messageCollection, "success", 5, true);
});
}
});
}
const message = this.$t("Task Completed Successfully");
const taskId = task.id;
this.submitting = true;
return ProcessMaker.apiClient
.put(`tasks/${taskId}`, { status: "COMPLETED", data: dataToSubmit })
.then(() => {
window.ProcessMaker.alert(message, "success", 5, true);
})
.catch((error) => {
if (error.response?.status && error.response?.status === 422) {
if (error.response.data.errors) {
Object.entries(error.response.data.errors).forEach(([key, value]) => {
window.ProcessMaker.alert(`${key}: ${value[0]}`, "danger", 0);
});
} else if (error.response.data.message) {
window.ProcessMaker.alert(error.response.data.message, "danger", 0);
}
this.$refs.task.loadNextAssignedTask();
}
})
.finally(() => {
this.submitting = false;
});
},
});
describe('submit() Method Tests', () => {
let component;
beforeEach(() => {
// Reset all mocks before each test
jest.clearAllMocks();
// Reset ProcessMaker.apiClient.put to return a resolved promise
ProcessMaker.apiClient.put.mockResolvedValue({ data: {} });
component = createTestComponent();
});
// ============================================================================
// BASIC FUNCTIONALITY TESTS
// ============================================================================
describe('Basic Functionality', () => {
test('should submit task with data successfully', async () => {
const task = { id: 123 };
const dataToSubmit = {
name: 'John Doe',
email: 'john@example.com',
_user: { id: 1 },
_request: { id: 100 },
};
await component.submit(task, dataToSubmit);
// Verify API was called with correct parameters
expect(ProcessMaker.apiClient.put).toHaveBeenCalledWith(
'tasks/123',
{ status: 'COMPLETED', data: dataToSubmit }
);
// Verify success alert was shown
expect(window.ProcessMaker.alert).toHaveBeenCalledWith(
'Task Completed Successfully',
'success',
5,
true
);
});
test('should set submitting flag during submission', async () => {
const task = { id: 123 };
const dataToSubmit = { name: 'John' };
expect(component.submitting).toBe(false);
const promise = component.submit(task, dataToSubmit);
expect(component.submitting).toBe(true);
await promise;
expect(component.submitting).toBe(false);
});
test('should prevent multiple simultaneous submissions', async () => {
const task = { id: 123 };
const dataToSubmit = { name: 'John' };
component.submitting = true;
const result = await component.submit(task, dataToSubmit);
// Should return early without calling API
expect(ProcessMaker.apiClient.put).not.toHaveBeenCalled();
expect(result).toBeUndefined();
});
});
// ============================================================================
// DATA SUBMISSION TESTS
// ============================================================================
describe('Data Submission', () => {
test('should submit only requested variables (filtered data)', async () => {
const task = { id: 123 };
const dataToSubmit = {
name: 'John Doe',
email: 'john@example.com',
_user: { id: 1 },
_request: { id: 100 },
// phone and address were filtered out
};
await component.submit(task, dataToSubmit);
const callArgs = ProcessMaker.apiClient.put.mock.calls[0];
const submittedData = callArgs[1].data;
expect(submittedData.name).toBe('John Doe');
expect(submittedData.email).toBe('john@example.com');
expect(submittedData._user).toEqual({ id: 1 });
expect(submittedData._request).toEqual({ id: 100 });
expect(submittedData.phone).toBeUndefined();
expect(submittedData.address).toBeUndefined();
});
test('should submit all data when no filtering applied', async () => {
const task = { id: 123 };
const dataToSubmit = {
name: 'John Doe',
email: 'john@example.com',
phone: '555-1234',
address: '123 Main St',
_user: { id: 1 },
_request: { id: 100 },
};
await component.submit(task, dataToSubmit);
const callArgs = ProcessMaker.apiClient.put.mock.calls[0];
const submittedData = callArgs[1].data;
expect(submittedData).toEqual(dataToSubmit);
});
test('should always include system variables', async () => {
const task = { id: 123 };
const dataToSubmit = {
name: 'John',
_user: { id: 1 },
_request: { id: 100 },
};
await component.submit(task, dataToSubmit);
const callArgs = ProcessMaker.apiClient.put.mock.calls[0];
const submittedData = callArgs[1].data;
expect(submittedData._user).toBeDefined();
expect(submittedData._request).toBeDefined();
});
});
// ============================================================================
// SELF SERVICE TESTS
// ============================================================================
describe('Self Service', () => {
test('should show alert and not submit when task is self service', async () => {
component.isSelfService = true;
const task = { id: 123 };
const dataToSubmit = { name: 'John' };
await component.submit(task, dataToSubmit);
// Should show alert
expect(ProcessMaker.alert).toHaveBeenCalledWith(
'Claim the Task to continue.',
'warning'
);
// Should NOT call API
expect(ProcessMaker.apiClient.put).not.toHaveBeenCalled();
});
});
// ============================================================================
// ERROR HANDLING TESTS
// ============================================================================
describe('Error Handling', () => {
test('should handle 422 validation errors', async () => {
const task = { id: 123 };
const dataToSubmit = { name: 'John' };
const error = {
response: {
status: 422,
data: {
errors: {
email: ['Email is required'],
phone: ['Phone format is invalid'],
},
},
},
};
ProcessMaker.apiClient.put.mockRejectedValue(error);
await component.submit(task, dataToSubmit);
// Should show error alerts
expect(window.ProcessMaker.alert).toHaveBeenCalledWith(
'email: Email is required',
'danger',
0
);
expect(window.ProcessMaker.alert).toHaveBeenCalledWith(
'phone: Phone format is invalid',
'danger',
0
);
// Should load next assigned task
expect(component.$refs.task.loadNextAssignedTask).toHaveBeenCalled();
});
test('should handle 422 error with message', async () => {
const task = { id: 123 };
const dataToSubmit = { name: 'John' };
const error = {
response: {
status: 422,
data: {
message: 'Validation failed',
},
},
};
ProcessMaker.apiClient.put.mockRejectedValue(error);
await component.submit(task, dataToSubmit);
expect(window.ProcessMaker.alert).toHaveBeenCalledWith(
'Validation failed',
'danger',
0
);
});
test('should reset submitting flag after error', async () => {
const task = { id: 123 };
const dataToSubmit = { name: 'John' };
ProcessMaker.apiClient.put.mockRejectedValue(new Error('Network error'));
await component.submit(task, dataToSubmit);
expect(component.submitting).toBe(false);
});
});
// ============================================================================
// COLLECTION DATA TESTS
// ============================================================================
describe('Collection Data', () => {
test('should process collection data when present', async () => {
const task = { id: 123 };
const dataToSubmit = {
name: 'John',
collection_field_1: 'value1',
collection_field_2: 'value2',
};
// Mock collection data result
component.processCollectionData = jest.fn(() => [
{
submitCollectionChecked: true,
collectionId: 456,
recordId: 789,
collectionFields: {
collection_field_1: {},
collection_field_2: {},
},
},
]);
await component.submit(task, dataToSubmit);
// Should call collection API
expect(ProcessMaker.apiClient.put).toHaveBeenCalledWith(
'collections/456/records/789',
{
data: {
collection_field_1: 'value1',
collection_field_2: 'value2',
},
uploads: [],
}
);
// Should also call task completion API
expect(ProcessMaker.apiClient.put).toHaveBeenCalledWith(
'tasks/123',
{ status: 'COMPLETED', data: dataToSubmit }
);
});
test('should skip collection data when submitCollectionChecked is false', async () => {
const task = { id: 123 };
const dataToSubmit = { name: 'John' };
component.processCollectionData = jest.fn(() => [
{
submitCollectionChecked: false,
collectionId: 456,
recordId: 789,
collectionFields: {},
},
]);
await component.submit(task, dataToSubmit);
// Should NOT call collection API
const calls = ProcessMaker.apiClient.put.mock.calls;
const collectionCall = calls.find(call => call[0].includes('collections'));
expect(collectionCall).toBeUndefined();
// But should still call task completion API
expect(ProcessMaker.apiClient.put).toHaveBeenCalledWith(
'tasks/123',
{ status: 'COMPLETED', data: dataToSubmit }
);
});
});
// ============================================================================
// INTEGRATION TESTS
// ============================================================================
describe('Integration Tests', () => {
test('should handle complete workflow with filtered data', async () => {
const task = { id: 123 };
// Simulating data after filtering by prepareSubmissionData
const dataToSubmit = {
name: 'John Doe',
email: 'john@example.com',
// phone was filtered out
_user: { id: 1, username: 'john' },
_request: { id: 100, status: 'ACTIVE' },
};
await component.submit(task, dataToSubmit);
// Verify submission
expect(ProcessMaker.apiClient.put).toHaveBeenCalledWith(
'tasks/123',
{
status: 'COMPLETED',
data: expect.objectContaining({
name: 'John Doe',
email: 'john@example.com',
_user: { id: 1, username: 'john' },
_request: { id: 100, status: 'ACTIVE' },
})
}
);
// Verify phone was NOT submitted
const submittedData = ProcessMaker.apiClient.put.mock.calls[0][1].data;
expect(submittedData.phone).toBeUndefined();
});
test('should handle empty data submission', async () => {
const task = { id: 123 };
const dataToSubmit = {};
await component.submit(task, dataToSubmit);
expect(ProcessMaker.apiClient.put).toHaveBeenCalledWith(
'tasks/123',
{ status: 'COMPLETED', data: {} }
);
});
test('should handle data with only system variables', async () => {
const task = { id: 123 };
const dataToSubmit = {
_user: { id: 1 },
_request: { id: 100 },
};
await component.submit(task, dataToSubmit);
const submittedData = ProcessMaker.apiClient.put.mock.calls[0][1].data;
expect(submittedData._user).toEqual({ id: 1 });
expect(submittedData._request).toEqual({ id: 100 });
});
});
});