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 () => {