-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathlib.slack.test.js
107 lines (79 loc) · 2.69 KB
/
lib.slack.test.js
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
import _ from 'lodash';
import { expect } from 'chai';
import sinon from 'sinon';
import Slack from 'node-slack';
import activitiesLib from '../server/lib/activities';
import slackLib from '../server/lib/slack';
describe('lib/slack', () => {
describe('calling postMessage', () => {
const message = "lorem ipsum";
const webhookUrl = 'hookurl';
const basePayload = {
text: message,
username: 'OpenCollective Activity Bot',
icon_url: 'https://opencollective.com/favicon.ico',
attachments: [],
};
it('with message succeeds', done => {
expectPayload(basePayload);
callSlackLib(done, message, webhookUrl);
});
it('with attachment succeeds', done => {
const attachments = ["att1", "att2"];
expectPayload(_.extend({}, basePayload, { attachments }));
callSlackLib(done, message, webhookUrl, { attachments });
});
it('with channel succeeds', done => {
const channel = "kewl channel";
expectPayload(_.extend({}, basePayload, { channel }));
callSlackLib(done, message, webhookUrl, { channel });
});
});
describe('calling postActivity', () => {
let formatMessageStub, postMessageStub;
const activity = "my activity";
const formattedMessage = "my formatted activity";
const webhookUrl = 'hookurl';
beforeEach(() => {
formatMessageStub = sinon.stub(activitiesLib, "formatMessageForPublicChannel");
postMessageStub = sinon.stub(slackLib, "postMessage");
});
afterEach(() => {
formatMessageStub.restore();
postMessageStub.restore();
});
it('with activity succeeds', done => {
formatMessageStub
.withArgs(activity, 'slack')
.returns(formattedMessage);
const expected = postMessageStub
.withArgs(formattedMessage, webhookUrl, {});
slackLib.postActivityOnPublicChannel(activity, webhookUrl, {});
expect(expected.called).to.be.ok;
done();
});
it('with options keeps the options', done => {
const options = { option1: "option1", attachments: [] };
formatMessageStub
.withArgs(activity, 'slack')
.returns(formattedMessage);
const expected = postMessageStub
.withArgs(formattedMessage, webhookUrl, options);
slackLib.postActivityOnPublicChannel(activity, webhookUrl, options);
expect(expected.called).to.be.ok;
done();
});
});
});
function expectPayload(expectedPayload) {
Slack.prototype.send = (actualPayload, cb) => {
expect(actualPayload).to.deep.equal(expectedPayload);
cb();
};
}
function callSlackLib(done, msg, webhookUrl, options) {
slackLib
.postMessage(msg, webhookUrl, options)
.then(done)
.catch(done);
}