From 91bec23c48909536405347aae596b4c9d51e8d03 Mon Sep 17 00:00:00 2001 From: Sharon Kennedy Date: Sun, 6 Sep 2020 14:13:04 -0400 Subject: [PATCH 1/2] handle bot signal --- public/index.html | 2 +- src/components/chatbox.jsx | 30 +++++++++++++++++++----------- src/setupTests.js | 3 +++ 3 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 src/setupTests.js diff --git a/public/index.html b/public/index.html index 2614593..b4cc3d8 100644 --- a/public/index.html +++ b/public/index.html @@ -25,7 +25,7 @@ anonymousDisplayName: 'Anonymous', position: 'bottom right', size: 'large', - maxWaitTime: 6000*3, + maxWaitTime: 1000*60*3, // 3 minutes } EmbeddableChatbox.mount(config); diff --git a/src/components/chatbox.jsx b/src/components/chatbox.jsx index f803b83..7ed5c93 100644 --- a/src/components/chatbox.jsx +++ b/src/components/chatbox.jsx @@ -167,6 +167,7 @@ class ChatBox extends React.Component { await this.state.client.stopClient() await this.state.client.clearStores() this.setState({ client: null }) + window.clearInterval(this.state.waitIntervalId) // no more waiting messages } this.state.localStorage.clear() @@ -487,8 +488,6 @@ class ChatBox extends React.Component { handleChatOffline = () => { this.exitChat(false) // close the chat connection but keep chatbox state - window.clearInterval(this.state.waitIntervalId) // no more waiting messages - window.clearInterval(this.state.waitTimeoutId) // no more waiting messages this.setState({ ready: true }) // no more loading animation } @@ -546,7 +545,6 @@ class ChatBox extends React.Component { this.verifyAllRoomDevices(client, room) this.setState({ facilitatorId: sender, ready: true }) window.clearInterval(this.state.waitIntervalId) - window.clearInterval(this.state.waitTimeoutId) } }); @@ -568,6 +566,23 @@ class ChatBox extends React.Component { this.setState({ typingStatus: null }) } }); + + client.on("event", (event) => { + const eventType = event.getType() + const content = event.getContent() + + if (eventType === 'm.bot.signal') { + this.handleBotSignal(content.signal) + } + }) + } + + handleBotSignal = (signal) => { + switch (signal) { + case 'END_CHAT': + this.displayBotMessage({ body: this.props.exitMessage }) + return this.exitChat(false); // keep chat state + } } componentDidUpdate(prevProps, prevState) { @@ -624,14 +639,7 @@ class ChatBox extends React.Component { } }, this.props.waitInterval) - const waitTimeoutId = window.setTimeout(() => { - if (!this.state.facilitatorId && !this.state.ready) { - this.displayBotMessage({ body: this.props.chatUnavailableMessage }) - this.handleChatOffline() - } - }, this.props.maxWaitTime) - - this.setState({ waitIntervalId, waitTimeoutId }) + this.setState({ waitIntervalId }) } handleRejectTerms = () => { diff --git a/src/setupTests.js b/src/setupTests.js new file mode 100644 index 0000000..de70497 --- /dev/null +++ b/src/setupTests.js @@ -0,0 +1,3 @@ +import { configure } from 'enzyme'; +import Adapter from 'enzyme-adapter-react-16'; +configure({ adapter: new Adapter() }); \ No newline at end of file From dbbe188adc200d2e01525534a7ae6b48084087b0 Mon Sep 17 00:00:00 2001 From: Sharon Kennedy Date: Sun, 6 Sep 2020 15:24:41 -0400 Subject: [PATCH 2/2] update tests --- __mocks__/matrix-js-sdk.js | 8 ++++++- src/components/chatbox.jsx | 4 +++- src/components/chatbox.test.js | 38 ++++++++++++++++++---------------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/__mocks__/matrix-js-sdk.js b/__mocks__/matrix-js-sdk.js index 8f1bc35..c8515f2 100644 --- a/__mocks__/matrix-js-sdk.js +++ b/__mocks__/matrix-js-sdk.js @@ -24,7 +24,13 @@ export const mockInitCrypto = jest.fn() export const mockStartClient = jest.fn(() => { return Promise.resolve('value'); }); -export const mockOnce = jest.fn() +export const mockOnce = jest + .fn() + .mockImplementation((event, callback) => { + if (event === 'sync') { + callback('PREPARED') + } + }) export const mockStopClient = jest.fn(() => { return Promise.resolve('value'); }); diff --git a/src/components/chatbox.jsx b/src/components/chatbox.jsx index 7ed5c93..43c6ad1 100644 --- a/src/components/chatbox.jsx +++ b/src/components/chatbox.jsx @@ -170,7 +170,9 @@ class ChatBox extends React.Component { window.clearInterval(this.state.waitIntervalId) // no more waiting messages } - this.state.localStorage.clear() + if (this.state.localStorage) { + this.state.localStorage.clear() + } if (resetState) { this.setState(this.initialState) diff --git a/src/components/chatbox.test.js b/src/components/chatbox.test.js index 5fd9d8b..a7cafb6 100644 --- a/src/components/chatbox.test.js +++ b/src/components/chatbox.test.js @@ -115,9 +115,7 @@ describe('Chatbox', () => { expect(createClient).toHaveBeenCalled() expect(mockInitCrypto).toHaveBeenCalled() expect(mockStartClient).toHaveBeenCalled() - expect(mockCreateRoom).toHaveBeenCalled() - expect(mockSetPowerLevel).toHaveBeenCalled() - expect(mockOn).toHaveBeenCalled() + expect(mockOnce).toHaveBeenCalled() }) test('rejecting terms should not start chat', async () => { @@ -146,6 +144,10 @@ describe('Chatbox', () => { acceptButton.simulate('click') + await waitForExpect(() => { + expect(mockOnce).toHaveBeenCalled() + }); + await waitForExpect(() => { expect(mockCreateRoom).toHaveBeenCalled() }); @@ -169,7 +171,7 @@ describe('Chatbox', () => { }) - test('decryption failure should lead to a new unencrypted chat', async () => { + test('decryption failure should handle the message event and save the event ID in state', async () => { const chatbox = mount() const dock = chatbox.find('button.dock') const instance = chatbox.instance() @@ -183,25 +185,25 @@ describe('Chatbox', () => { acceptButton.simulate('click') await waitForExpect(() => { - expect(mockCreateRoom).toHaveBeenCalled() + expect(mockOnce).toHaveBeenCalled() }); - jest.spyOn(instance, 'initializeUnencryptedChat') - instance.handleDecryptionError() + jest.spyOn(instance, 'handleMessageEvent') + + instance.handleDecryptionError({ + getId: () => 'test_event_id', + getType: () => 'm.message', + getSender: () => 'sender', + getRoomId: () => 'room id', + getContent: () => ({ body: 'test msg' }), + getTs: () => '123', + }) await waitForExpect(() => { - expect(mockLeave).toHaveBeenCalled() - }); + expect(instance.handleMessageEvent).toHaveBeenCalled() + }) - await waitForExpect(() => { - expect(mockStopClient).toHaveBeenCalled() - }); - - await waitForExpect(() => { - expect(mockClearStores).toHaveBeenCalled() - }); - - expect(instance.initializeUnencryptedChat).toHaveBeenCalled() + expect(chatbox.state().decryptionErrors).toEqual({ 'test_event_id': true }) }) test('creating an unencrypted chat', async () => {