From fefda571d6adfe7cd5659c3442b9fe7c5a29281b Mon Sep 17 00:00:00 2001 From: Sharon Kennedy Date: Fri, 27 Mar 2020 00:48:03 -0400 Subject: [PATCH] fix tests --- __mocks__/matrix-js-sdk.js | 19 +++++++++++++++++++ src/bot.js | 5 ++--- src/bot.test.js | 11 +++++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/__mocks__/matrix-js-sdk.js b/__mocks__/matrix-js-sdk.js index 1911e4a..7412779 100644 --- a/__mocks__/matrix-js-sdk.js +++ b/__mocks__/matrix-js-sdk.js @@ -79,6 +79,17 @@ export const mockSendTextMessage = jest.fn() return Promise.resolve() }) +export const mockSendMessage = jest.fn() + .mockImplementationOnce(() => { + return Promise.reject({ + name: "UnknownDeviceError", + devices: ['device1', 'device2'] + }) + }) + .mockImplementation(() => { + return Promise.resolve() + }) + export const mockSetDeviceKnown = jest.fn() export const mockInvite = jest.fn() @@ -138,6 +149,12 @@ export const mockGetJoinedRoomMembers = jest.fn(() => { }) }); +export const mockGetGroupUsers = jest.fn(() => { + return Promise.resolve({ + chunk: { user_id: 'user_id_1', user_id: 'user_id_2' } + }) +}); + export const mockGetUser = jest.fn().mockReturnValue({ presence: 'online'}); export const mockClient = { @@ -156,6 +173,7 @@ export const mockClient = { isCryptoEnabled: mockIsCryptoEnabled, createRoom: mockCreateRoom, setPowerLevel: mockSetPowerLevel, + sendMessage: mockSendMessage, sendTextMessage: mockSendTextMessage, deactivateAccount: mockDeactivateAccount, login: mockLogin, @@ -167,6 +185,7 @@ export const mockClient = { kick: mockKick, getJoinedRoomMembers: mockGetJoinedRoomMembers, getUser: mockGetUser, + getGroupUsers: mockGetGroupUsers, } export const WebStorageSessionStore = jest.fn() diff --git a/src/bot.js b/src/bot.js index f1e4168..75dabd1 100644 --- a/src/bot.js +++ b/src/bot.js @@ -19,7 +19,7 @@ const MAX_RETRIES = 3; class OcrccBot { constructor() { this.awaitingFacilitator = {}; - this.client = null; + this.client = matrix.createClient(process.env.MATRIX_SERVER_URL); this.joinedRooms = []; this.activeChatrooms = {}; } @@ -450,9 +450,8 @@ class OcrccBot { start() { const localStorage = this.createLocalStorage(); - const tmpClient = matrix.createClient(process.env.MATRIX_SERVER_URL); - tmpClient + this.client .login("m.login.password", { user: process.env.BOT_USERNAME, password: process.env.BOT_PASSWORD, diff --git a/src/bot.test.js b/src/bot.test.js index 3ed056e..d8b5bc9 100644 --- a/src/bot.test.js +++ b/src/bot.test.js @@ -20,6 +20,7 @@ import { mockClearStores, mockOn, mockOnce, + mockSendMessage, mockSendTextMessage, mockLogin, mockGetDevices, @@ -30,7 +31,8 @@ import { mockInvite, mockKick, mockGetJoinedRoomMembers, - mockGetUser + mockGetUser, + mockGetGroupUsers } from "matrix-js-sdk"; import OcrccBot from './bot' @@ -63,13 +65,14 @@ describe('OcrccBot', () => { mockKick.mockClear() mockGetJoinedRoomMembers.mockClear() mockGetUser.mockClear() + mockSendMessage.mockClear() mockSendTextMessage.mockClear() mockAppendFileSync.mockClear() + mockGetGroupUsers.mockClear() }) - test('constructor should inititialize matrix client', () => { + test('constructor should inititialize class variables', () => { const bot = new OcrccBot() - expect(createClient).toHaveBeenCalledWith(process.env.MATRIX_SERVER_URL) expect(bot.joinedRooms).toEqual([]) expect(bot.awaitingFacilitator).toEqual({}) expect(bot.activeChatrooms).toEqual({}) @@ -99,7 +102,7 @@ describe('OcrccBot', () => { expect(mockSetDeviceVerified).toHaveBeenCalledTimes(2) }) waitForExpect(() => { - expect(mockSendTextMessage).toHaveBeenCalledWith(testRoom, testMsg) + expect(mockSendMessage).toHaveBeenCalledWith(testRoom, testMsg) }) })