forked from slackapi/bolt-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkflowStep.spec.ts
380 lines (305 loc) · 13.5 KB
/
WorkflowStep.spec.ts
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
import 'mocha';
import { assert } from 'chai';
import sinon from 'sinon';
import rewiremock from 'rewiremock';
import { WebClient } from '@slack/web-api';
import {
WorkflowStep,
SlackWorkflowStepMiddlewareArgs,
AllWorkflowStepMiddlewareArgs,
WorkflowStepMiddleware,
WorkflowStepConfig,
WorkflowStepEditMiddlewareArgs,
WorkflowStepSaveMiddlewareArgs,
WorkflowStepExecuteMiddlewareArgs,
} from './WorkflowStep';
import { Override } from './test-helpers';
import { AllMiddlewareArgs, AnyMiddlewareArgs, WorkflowStepEdit, Middleware } from './types';
import { WorkflowStepInitializationError } from './errors';
async function importWorkflowStep(overrides: Override = {}): Promise<typeof import('./WorkflowStep')> {
return rewiremock.module(() => import('./WorkflowStep'), overrides);
}
const MOCK_FN = async () => {};
const MOCK_CONFIG_SINGLE = {
edit: MOCK_FN,
save: MOCK_FN,
execute: MOCK_FN,
};
const MOCK_CONFIG_MULTIPLE = {
edit: [MOCK_FN, MOCK_FN],
save: [MOCK_FN],
execute: [MOCK_FN, MOCK_FN, MOCK_FN],
};
describe('WorkflowStep class', () => {
describe('constructor', () => {
it('should accept config as single functions', async () => {
const ws = new WorkflowStep('test_callback_id', MOCK_CONFIG_SINGLE);
assert.isNotNull(ws);
});
it('should accept config as multiple functions', async () => {
const ws = new WorkflowStep('test_callback_id', MOCK_CONFIG_MULTIPLE);
assert.isNotNull(ws);
});
});
describe('getMiddleware', () => {
it('should not call next if a workflow step event', async () => {
const ws = new WorkflowStep('test_edit_callback_id', MOCK_CONFIG_SINGLE);
const middleware = ws.getMiddleware();
const fakeEditArgs = createFakeStepEditAction() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs;
const fakeNext = sinon.spy();
fakeEditArgs.next = fakeNext;
await middleware(fakeEditArgs);
assert(fakeNext.notCalled);
});
it('should call next if valid workflow step with mismatched callback_id', async () => {
const ws = new WorkflowStep('bad_callback_id', MOCK_CONFIG_SINGLE);
const middleware = ws.getMiddleware();
const fakeEditArgs = createFakeStepEditAction() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs;
const fakeNext = sinon.spy();
fakeEditArgs.next = fakeNext;
await middleware(fakeEditArgs);
assert(fakeNext.called);
});
it('should call next if not a workflow step event', async () => {
const ws = new WorkflowStep('test_view_callback_id', MOCK_CONFIG_SINGLE);
const middleware = ws.getMiddleware();
const fakeViewArgs = createFakeViewEvent() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs;
const fakeNext = sinon.spy();
fakeViewArgs.next = fakeNext;
await middleware(fakeViewArgs);
assert(fakeNext.called);
});
});
describe('validate', () => {
it('should throw an error if callback_id is not valid', async () => {
const { validate } = await importWorkflowStep();
// intentionally casting to string to trigger failure
const badId = {} as string;
const validationFn = () => validate(badId, MOCK_CONFIG_SINGLE);
const expectedMsg = 'WorkflowStep expects a callback_id as the first argument';
assert.throws(validationFn, WorkflowStepInitializationError, expectedMsg);
});
it('should throw an error if config is not an object', async () => {
const { validate } = await importWorkflowStep();
// intentionally casting to WorkflowStepConfig to trigger failure
const badConfig = '' as unknown as WorkflowStepConfig;
const validationFn = () => validate('callback_id', badConfig);
const expectedMsg = 'WorkflowStep expects a configuration object as the second argument';
assert.throws(validationFn, WorkflowStepInitializationError, expectedMsg);
});
it('should throw an error if required keys are missing', async () => {
const { validate } = await importWorkflowStep();
// intentionally casting to WorkflowStepConfig to trigger failure
const badConfig = {
edit: async () => {},
} as unknown as WorkflowStepConfig;
const validationFn = () => validate('callback_id', badConfig);
const expectedMsg = 'WorkflowStep is missing required keys: save, execute';
assert.throws(validationFn, WorkflowStepInitializationError, expectedMsg);
});
it('should throw an error if lifecycle props are not a single callback or an array of callbacks', async () => {
const { validate } = await importWorkflowStep();
// intentionally casting to WorkflowStepConfig to trigger failure
const badConfig = {
edit: async () => {},
save: {},
execute: async () => {},
} as unknown as WorkflowStepConfig;
const validationFn = () => validate('callback_id', badConfig);
const expectedMsg = 'WorkflowStep save property must be a function or an array of functions';
assert.throws(validationFn, WorkflowStepInitializationError, expectedMsg);
});
});
describe('isStepEvent', () => {
it('should return true if recognized workflow step payload type', async () => {
const fakeEditArgs = createFakeStepEditAction() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs;
const fakeSaveArgs = createFakeStepSaveEvent() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs;
const fakeExecuteArgs = createFakeStepExecuteEvent() as unknown as SlackWorkflowStepMiddlewareArgs
& AllMiddlewareArgs;
const { isStepEvent } = await importWorkflowStep();
const editIsStepEvent = isStepEvent(fakeEditArgs);
const viewIsStepEvent = isStepEvent(fakeSaveArgs);
const executeIsStepEvent = isStepEvent(fakeExecuteArgs);
assert.isTrue(editIsStepEvent);
assert.isTrue(viewIsStepEvent);
assert.isTrue(executeIsStepEvent);
});
it('should return false if not a recognized workflow step payload type', async () => {
const fakeEditArgs = createFakeStepEditAction() as unknown as AnyMiddlewareArgs;
fakeEditArgs.payload.type = 'invalid_type';
const { isStepEvent } = await importWorkflowStep();
const actionIsStepEvent = isStepEvent(fakeEditArgs);
assert.isFalse(actionIsStepEvent);
});
});
describe('prepareStepArgs', () => {
it('should remove next() from all original event args', async () => {
const fakeEditArgs = createFakeStepEditAction() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs;
const fakeSaveArgs = createFakeStepSaveEvent() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs;
const fakeExecuteArgs = createFakeStepExecuteEvent() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs; // eslint-disable-line max-len
const { prepareStepArgs } = await importWorkflowStep();
const editStepArgs = prepareStepArgs(fakeEditArgs);
const viewStepArgs = prepareStepArgs(fakeSaveArgs);
const executeStepArgs = prepareStepArgs(fakeExecuteArgs);
assert.notExists(editStepArgs.next);
assert.notExists(viewStepArgs.next);
assert.notExists(executeStepArgs.next);
});
it('should augment workflow_step_edit args with step and configure()', async () => {
const fakeArgs = createFakeStepEditAction();
const { prepareStepArgs } = await importWorkflowStep();
// casting to returned type because prepareStepArgs isn't built to do so
const stepArgs = prepareStepArgs(fakeArgs) as AllWorkflowStepMiddlewareArgs<WorkflowStepEditMiddlewareArgs>;
assert.exists(stepArgs.step);
assert.exists(stepArgs.configure);
});
it('should augment view_submission with step and update()', async () => {
const fakeArgs = createFakeStepSaveEvent();
const { prepareStepArgs } = await importWorkflowStep();
// casting to returned type because prepareStepArgs isn't built to do so
const stepArgs = prepareStepArgs(fakeArgs) as AllWorkflowStepMiddlewareArgs<WorkflowStepSaveMiddlewareArgs>;
assert.exists(stepArgs.step);
assert.exists(stepArgs.update);
});
it('should augment workflow_step_execute with step, complete() and fail()', async () => {
const fakeArgs = createFakeStepExecuteEvent();
const { prepareStepArgs } = await importWorkflowStep();
// casting to returned type because prepareStepArgs isn't built to do so
const stepArgs = prepareStepArgs(fakeArgs) as AllWorkflowStepMiddlewareArgs<WorkflowStepExecuteMiddlewareArgs>;
assert.exists(stepArgs.step);
assert.exists(stepArgs.complete);
assert.exists(stepArgs.fail);
});
});
describe('step utility functions', () => {
it('configure should call views.open', async () => {
const fakeEditArgs = createFakeStepEditAction() as unknown as AllWorkflowStepMiddlewareArgs;
const fakeClient = { views: { open: sinon.spy() } };
fakeEditArgs.client = fakeClient as unknown as WebClient;
const { prepareStepArgs } = await importWorkflowStep();
// casting to returned type because prepareStepArgs isn't built to do so
const editStepArgs = prepareStepArgs(
fakeEditArgs,
) as AllWorkflowStepMiddlewareArgs<WorkflowStepEditMiddlewareArgs>;
await editStepArgs.configure({ blocks: [] });
assert(fakeClient.views.open.called);
});
it('update should call workflows.updateStep', async () => {
const fakeSaveArgs = createFakeStepSaveEvent() as unknown as AllWorkflowStepMiddlewareArgs;
const fakeClient = { workflows: { updateStep: sinon.spy() } };
fakeSaveArgs.client = fakeClient as unknown as WebClient;
const { prepareStepArgs } = await importWorkflowStep();
// casting to returned type because prepareStepArgs isn't built to do so
const saveStepArgs = prepareStepArgs(
fakeSaveArgs,
) as AllWorkflowStepMiddlewareArgs<WorkflowStepSaveMiddlewareArgs>;
await saveStepArgs.update();
assert(fakeClient.workflows.updateStep.called);
});
it('complete should call workflows.stepCompleted', async () => {
const fakeExecuteArgs = createFakeStepExecuteEvent() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs; // eslint-disable-line max-len
const fakeClient = { workflows: { stepCompleted: sinon.spy() } };
fakeExecuteArgs.client = fakeClient as unknown as WebClient;
const { prepareStepArgs } = await importWorkflowStep();
// casting to returned type because prepareStepArgs isn't built to do so
const executeStepArgs = prepareStepArgs(
fakeExecuteArgs,
) as AllWorkflowStepMiddlewareArgs<WorkflowStepExecuteMiddlewareArgs>;
await executeStepArgs.complete();
assert(fakeClient.workflows.stepCompleted.called);
});
it('fail should call workflows.stepFailed', async () => {
const fakeExecuteArgs = createFakeStepExecuteEvent() as unknown as SlackWorkflowStepMiddlewareArgs & AllMiddlewareArgs; // eslint-disable-line max-len
const fakeClient = { workflows: { stepFailed: sinon.spy() } };
fakeExecuteArgs.client = fakeClient as unknown as WebClient;
const { prepareStepArgs } = await importWorkflowStep();
// casting to returned type because prepareStepArgs isn't built to do so
const executeStepArgs = prepareStepArgs(
fakeExecuteArgs,
) as AllWorkflowStepMiddlewareArgs<WorkflowStepExecuteMiddlewareArgs>;
await executeStepArgs.fail({ error: { message: 'Failed' } });
assert(fakeClient.workflows.stepFailed.called);
});
});
describe('processStepMiddleware', () => {
it('should call each callback in user-provided middleware', async () => {
const { ...fakeArgs } = createFakeStepEditAction() as unknown as AllWorkflowStepMiddlewareArgs;
const { processStepMiddleware } = await importWorkflowStep();
const fn1 = sinon.spy((async ({ next: continuation }) => {
await continuation();
}) as Middleware<WorkflowStepEdit>);
const fn2 = sinon.spy(async () => {});
const fakeMiddleware = [fn1, fn2] as WorkflowStepMiddleware;
await processStepMiddleware(fakeArgs, fakeMiddleware);
assert(fn1.called);
assert(fn2.called);
});
});
});
function createFakeStepEditAction() {
return {
body: {
callback_id: 'test_edit_callback_id',
trigger_id: 'test_edit_trigger_id',
},
payload: {
type: 'workflow_step_edit',
callback_id: 'test_edit_callback_id',
},
action: {
workflow_step: {},
},
context: {},
};
}
function createFakeStepSaveEvent() {
return {
body: {
callback_id: 'test_save_callback_id',
trigger_id: 'test_save_trigger_id',
workflow_step: {
workflow_step_edit_id: '',
},
},
payload: {
type: 'workflow_step',
callback_id: 'test_save_callback_id',
},
context: {},
};
}
function createFakeStepExecuteEvent() {
return {
body: {
callback_id: 'test_execute_callback_id',
trigger_id: 'test_execute_trigger_id',
},
event: {
workflow_step: {},
},
payload: {
type: 'workflow_step_execute',
callback_id: 'test_execute_callback_id',
workflow_step: {
workflow_step_execute_id: '',
},
},
context: {},
};
}
function createFakeViewEvent() {
return {
body: {
callback_id: 'test_view_callback_id',
trigger_id: 'test_view_trigger_id',
workflow_step: {
workflow_step_edit_id: '',
},
},
payload: {
type: 'view_submission',
callback_id: 'test_view_callback_id',
},
context: {},
};
}