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