Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit c10e7bb

Browse files
committedApr 27, 2018
add tests for startRTM
1 parent 4481e35 commit c10e7bb

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
 

‎__test__/lib/Slackbot_worker.test.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use strict';
2+
3+
const util = require('util');
4+
const EventEmitter = require('events').EventEmitter;
5+
6+
let mockWs;
7+
function MockWs(url) {
8+
this.url = url;
9+
mockWs = this;
10+
}
11+
util.inherits(MockWs, EventEmitter);
12+
13+
jest.mock('ws', () => MockWs);
14+
15+
let mockResponse;
16+
let mockRequest = {
17+
post: jest.fn().mockImplementation((params, cb) => {
18+
cb(null, mockResponse, mockResponse.body);
19+
})
20+
};
21+
jest.mock('request', () => mockRequest);
22+
23+
// these need to be required after mocks are set up
24+
const CoreBot = require('../../lib/CoreBot');
25+
const Slackbot_worker = require('../../lib/Slackbot_worker');
26+
27+
beforeEach(() => {
28+
jest.clearAllMocks();
29+
});
30+
31+
describe('startRTM', () => {
32+
it('connects to rtm url and calls callback function', () => {
33+
const botkit = CoreBot({});
34+
const bot = Slackbot_worker(botkit, {});
35+
36+
mockResponse = {
37+
statusCode: 200,
38+
body: JSON.stringify({
39+
ok: true,
40+
url: 'http://mockurl',
41+
self: {
42+
name: 'botkit'
43+
}
44+
})
45+
};
46+
47+
const cb = jest.fn();
48+
bot.startRTM(cb);
49+
mockWs.emit('open');
50+
expect(mockRequest.post).toHaveBeenCalledTimes(1);
51+
expect(mockWs.url).toBe('http://mockurl');
52+
expect(cb).toHaveBeenCalledTimes(1);
53+
54+
const errorArg = cb.mock.calls[0][0];
55+
expect(errorArg).toBeNull();
56+
});
57+
it('handles Slack API rtm.connect errors', () => {
58+
const botkit = CoreBot({});
59+
const bot = Slackbot_worker(botkit, {});
60+
61+
mockResponse = {
62+
statusCode: 200,
63+
body: JSON.stringify({
64+
ok: false,
65+
error: 'test_error'
66+
})
67+
};
68+
69+
const cb = jest.fn();
70+
bot.startRTM(cb);
71+
expect(mockRequest.post).toHaveBeenCalledTimes(1);
72+
expect(cb).toHaveBeenCalledTimes(1);
73+
74+
const errorArg = cb.mock.calls[0][0];
75+
expect(errorArg).toBe('test_error');
76+
});
77+
it('handles websocket connection errors', () => {
78+
const botkit = CoreBot({});
79+
const bot = Slackbot_worker(botkit, {});
80+
81+
mockResponse = {
82+
statusCode: 200,
83+
body: JSON.stringify({
84+
ok: true,
85+
url: 'http://mockurl',
86+
self: {
87+
name: 'botkit'
88+
}
89+
})
90+
};
91+
92+
const cb = jest.fn();
93+
bot.startRTM(cb);
94+
mockWs.emit('error', 'test websocket error');
95+
expect(mockRequest.post).toHaveBeenCalledTimes(1);
96+
expect(cb).toHaveBeenCalledTimes(1);
97+
98+
const errorArg = cb.mock.calls[0][0];
99+
expect(errorArg).toBe('test websocket error');
100+
});
101+
});

0 commit comments

Comments
 (0)
This repository has been archived.