-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathIncomingWebhook.spec.ts
More file actions
115 lines (100 loc) · 3.59 KB
/
IncomingWebhook.spec.ts
File metadata and controls
115 lines (100 loc) · 3.59 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
import { assert } from 'chai';
import nock from 'nock';
import { ErrorCode } from './errors';
import { IncomingWebhook } from './IncomingWebhook';
const url = 'https://hooks.slack.com/services/FAKEWEBHOOK';
describe('IncomingWebhook', () => {
afterEach(() => {
nock.cleanAll();
});
describe('constructor()', () => {
it('should build a default webhook given a URL', () => {
const webhook = new IncomingWebhook(url);
assert.instanceOf(webhook, IncomingWebhook);
});
it('should create a default webhook with a default timeout', () => {
const webhook = new IncomingWebhook(url);
assert.nestedPropertyVal(webhook, 'defaults.timeout', 0);
});
it('should create an axios instance that has the timeout passed by the user', () => {
const givenTimeout = 100;
const webhook = new IncomingWebhook(url, { timeout: givenTimeout });
assert.nestedPropertyVal(webhook, 'axios.defaults.timeout', givenTimeout);
});
});
describe('send()', () => {
let webhook: IncomingWebhook;
beforeEach(() => {
webhook = new IncomingWebhook(url);
});
describe('when making a successful call', () => {
let scope: nock.Scope;
beforeEach(() => {
scope = nock('https://hooks.slack.com')
.post(/services/)
.reply(200, 'ok');
});
it('should return results in a Promise', async () => {
const result = await webhook.send('Hello');
assert.strictEqual(result.text, 'ok');
scope.done();
});
it('should send metadata', async () => {
const result = await webhook.send({
text: 'Hello',
metadata: {
event_type: 'foo',
event_payload: { foo: 'bar' },
},
});
assert.strictEqual(result.text, 'ok');
scope.done();
});
});
describe('when the call fails', () => {
let statusCode: number;
let scope: nock.Scope;
beforeEach(() => {
statusCode = 500;
scope = nock('https://hooks.slack.com')
.post(/services/)
.reply(statusCode);
});
it('should return a Promise which rejects on error', async () => {
try {
await webhook.send('Hello');
assert.fail('expected rejection');
} catch (error) {
assert.ok(error);
assert.instanceOf(error, Error);
assert.match((error as Error).message, new RegExp(String(statusCode)));
scope.done();
}
});
it('should fail with IncomingWebhookRequestError when the API request fails', async () => {
// One known request error is when the node encounters an ECONNREFUSED. In order to simulate this, rather than
// using nock, we send the request to a host:port that is not listening.
const webhook = new IncomingWebhook('https://localhost:8999/api/');
try {
await webhook.send('Hello');
assert.fail('expected rejection');
} catch (error) {
assert.instanceOf(error, Error);
assert.propertyVal(error, 'code', ErrorCode.RequestError);
}
});
});
describe('lifecycle', () => {
it('should not overwrite the default parameters after a call', async () => {
const defaultParams = { channel: 'default' };
const webhook = new IncomingWebhook(url, defaultParams);
try {
await webhook.send({ channel: 'different' });
assert.fail('expected rejection');
} catch (_err) {
assert.nestedPropertyVal(webhook, 'defaults.channel', defaultParams.channel);
}
});
});
});
});