forked from slackapi/bolt-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp-workflow-steps.spec.ts
89 lines (71 loc) · 2.39 KB
/
App-workflow-steps.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
import 'mocha';
import sinon from 'sinon';
import { assert } from 'chai';
import rewiremock from 'rewiremock';
import { Override, mergeOverrides } from './test-helpers';
import {
Receiver,
ReceiverEvent,
} from './types';
import App from './App';
import { WorkflowStep } from './WorkflowStep';
// Fakes
class FakeReceiver implements Receiver {
private bolt: App | undefined;
public init = (bolt: App) => {
this.bolt = bolt;
};
public start = sinon.fake((...params: any[]): Promise<unknown> => Promise.resolve([...params]));
public stop = sinon.fake((...params: any[]): Promise<unknown> => Promise.resolve([...params]));
public async sendEvent(event: ReceiverEvent): Promise<void> {
return this.bolt?.processEvent(event);
}
}
describe('App WorkflowStep middleware', () => {
let fakeReceiver: FakeReceiver;
let dummyAuthorizationResult: { botToken: string; botId: string };
beforeEach(() => {
fakeReceiver = new FakeReceiver();
dummyAuthorizationResult = { botToken: '', botId: '' };
});
let app: App;
beforeEach(async () => {
const MockAppNoOverrides = await importApp();
app = new MockAppNoOverrides({
receiver: fakeReceiver,
authorize: sinon.fake.resolves(dummyAuthorizationResult),
});
});
it('should add a listener to middleware for each WorkflowStep passed to app.step', async () => {
const ws = new WorkflowStep('test_id', { edit: [], save: [], execute: [] });
/* middleware is a private property on App. Since app.step relies on app.use,
and app.use is fully tested above, we're opting just to ensure that the step listener
is added to the global middleware array, rather than repeating the same tests. */
const { middleware } = (app as any);
assert.equal(middleware.length, 2);
app.step(ws);
assert.equal(middleware.length, 3);
});
});
/* Testing Harness */
// Loading the system under test using overrides
async function importApp(
overrides: Override = mergeOverrides(withNoopAppMetadata(), withNoopWebClient()),
): Promise<typeof import('./App').default> {
return (await rewiremock.module(() => import('./App'), overrides)).default;
}
// Composable overrides
function withNoopWebClient(): Override {
return {
'@slack/web-api': {
WebClient: class {},
},
};
}
function withNoopAppMetadata(): Override {
return {
'@slack/web-api': {
addAppMetadata: sinon.fake(),
},
};
}