2020-03-12 17:08:57 +00:00
|
|
|
import React from 'react';
|
|
|
|
import Chatbox from './chatbox';
|
2020-03-13 04:05:12 +00:00
|
|
|
import {
|
|
|
|
createClient,
|
|
|
|
mockClient,
|
|
|
|
mockRegisterRequest,
|
|
|
|
mockInitCrypto,
|
|
|
|
mockStartClient,
|
|
|
|
mockSetPowerLevel,
|
|
|
|
mockCreateRoom,
|
|
|
|
mockLeave,
|
|
|
|
mockDeactivateAccount,
|
|
|
|
mockStopClient,
|
|
|
|
mockClearStores,
|
|
|
|
mockOn,
|
|
|
|
mockOnce,
|
2020-04-29 15:31:23 +00:00
|
|
|
mockSendTextMessage,
|
|
|
|
mockIsCryptoEnabled,
|
|
|
|
mockIsRoomEncrypted,
|
2020-03-13 04:05:12 +00:00
|
|
|
} from "matrix-js-sdk";
|
2020-03-12 17:08:57 +00:00
|
|
|
import { mount, shallow } from 'enzyme';
|
|
|
|
import { createWaitForElement } from 'enzyme-wait';
|
|
|
|
import { config } from 'react-transition-group';
|
2020-03-13 04:05:12 +00:00
|
|
|
import waitForExpect from 'wait-for-expect'
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-27 22:21:13 +00:00
|
|
|
|
2020-03-12 17:08:57 +00:00
|
|
|
config.disabled = true
|
|
|
|
|
2020-03-27 22:21:13 +00:00
|
|
|
var testConfig = {
|
2020-03-12 17:08:57 +00:00
|
|
|
matrixServerUrl: 'https://matrix.rhok.space',
|
2020-03-27 22:21:13 +00:00
|
|
|
botId: '@help-bot:rhok.space',
|
2020-03-12 17:08:57 +00:00
|
|
|
roomName: 'Support Chat',
|
|
|
|
termsUrl: 'https://tosdr.org/',
|
|
|
|
introMessage: 'This chat application does not collect any of your personal data or any data from your use of this service.',
|
2020-03-27 22:21:13 +00:00
|
|
|
agreementMessage: 'Do you want to continue?',
|
2020-03-12 17:08:57 +00:00
|
|
|
confirmationMessage: 'Waiting for a facilitator to join the chat...',
|
2020-03-27 22:21:13 +00:00
|
|
|
exitMessage: 'The chat is closed. You may close this window.',
|
2020-03-12 17:08:57 +00:00
|
|
|
chatUnavailableMessage: 'The chat service is not available right now. Please try again later.',
|
|
|
|
anonymousDisplayName: 'Anonymous',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
describe('Chatbox', () => {
|
2020-03-13 04:05:12 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
createClient.mockClear()
|
|
|
|
mockInitCrypto.mockClear()
|
|
|
|
mockStartClient.mockClear()
|
|
|
|
mockRegisterRequest.mockClear()
|
|
|
|
mockSetPowerLevel.mockClear()
|
|
|
|
mockCreateRoom.mockClear()
|
|
|
|
mockLeave.mockClear()
|
|
|
|
mockDeactivateAccount.mockClear()
|
|
|
|
mockStopClient.mockClear()
|
|
|
|
mockClearStores.mockClear()
|
|
|
|
mockOnce.mockClear()
|
|
|
|
mockOn.mockClear()
|
2020-03-27 22:21:13 +00:00
|
|
|
mockSendTextMessage.mockClear()
|
2020-04-29 15:31:23 +00:00
|
|
|
mockIsCryptoEnabled.mockClear()
|
|
|
|
mockIsRoomEncrypted.mockClear()
|
2020-03-13 04:05:12 +00:00
|
|
|
})
|
|
|
|
|
2020-03-12 17:08:57 +00:00
|
|
|
test('chat window should open and close', async () => {
|
|
|
|
const chatbox = mount(<Chatbox {...testConfig} />)
|
|
|
|
|
|
|
|
let dock = chatbox.find('button.dock')
|
|
|
|
let chatWindow = chatbox.find('.widget')
|
|
|
|
|
|
|
|
expect(dock.length).toEqual(1)
|
|
|
|
expect(chatWindow.hasClass('widget-exited')).toEqual(true)
|
|
|
|
|
|
|
|
// open chat window
|
|
|
|
dock.simulate('click')
|
|
|
|
|
|
|
|
const openChatWindow = await createWaitForElement('.widget-entered')(chatbox)
|
|
|
|
dock = chatbox.find('button.dock')
|
|
|
|
expect(openChatWindow.length).toEqual(1)
|
|
|
|
expect(dock.length).toEqual(0)
|
|
|
|
|
|
|
|
// close chat window
|
|
|
|
const closeButton = chatbox.find('button.widget-header-close')
|
|
|
|
closeButton.simulate('click')
|
|
|
|
|
|
|
|
chatWindow = chatbox.find('.widget')
|
|
|
|
dock = chatbox.find('button.dock')
|
|
|
|
|
|
|
|
expect(dock.length).toEqual(1)
|
|
|
|
expect(chatWindow.hasClass('widget-exited')).toEqual(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('chat window should contain the right messages', () => {
|
|
|
|
const chatbox = mount(<Chatbox {...testConfig} />)
|
|
|
|
const props = chatbox.props()
|
|
|
|
const messages = chatbox.find('.messages')
|
|
|
|
|
|
|
|
expect(messages.text()).toContain(props.introMessage)
|
|
|
|
expect(messages.html()).toContain(props.termsUrl)
|
|
|
|
expect(messages.text()).toContain(props.agreementMessage)
|
|
|
|
});
|
|
|
|
|
|
|
|
test('agreeing to terms should start encrypted chat', async () => {
|
|
|
|
const chatbox = mount(<Chatbox {...testConfig} />)
|
|
|
|
const dock = chatbox.find('button.dock')
|
|
|
|
|
|
|
|
dock.simulate('click')
|
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
const openChatWindow = await createWaitForElement('.widget-entered')(chatbox)
|
|
|
|
let acceptButton = await createWaitForElement('button#accept')(chatbox)
|
|
|
|
acceptButton = chatbox.find('button#accept')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
acceptButton.simulate('click')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
const ready = await createWaitForElement('.loader')(chatbox)
|
|
|
|
expect(ready.length).toEqual(1)
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
expect(createClient).toHaveBeenCalled()
|
|
|
|
expect(mockInitCrypto).toHaveBeenCalled()
|
|
|
|
expect(mockStartClient).toHaveBeenCalled()
|
|
|
|
expect(mockCreateRoom).toHaveBeenCalled()
|
|
|
|
expect(mockSetPowerLevel).toHaveBeenCalled()
|
|
|
|
expect(mockOn).toHaveBeenCalled()
|
|
|
|
})
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
test('rejecting terms should not start chat', async () => {
|
|
|
|
const chatbox = mount(<Chatbox {...testConfig} />)
|
|
|
|
const dock = chatbox.find('button.dock')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
dock.simulate('click')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
const openChatWindow = await createWaitForElement('.widget-entered')(chatbox)
|
|
|
|
let rejectButton = await createWaitForElement('button#reject')(chatbox)
|
|
|
|
rejectButton = chatbox.find('button#reject')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
rejectButton.simulate('click')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
expect(createClient.mock.calls.length).toEqual(0)
|
2020-03-12 17:08:57 +00:00
|
|
|
})
|
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
test('submitted messages should be sent to matrix', async () => {
|
|
|
|
const chatbox = mount(<Chatbox {...testConfig} />)
|
|
|
|
const dock = chatbox.find('button.dock')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
dock.simulate('click')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
let acceptButton = await createWaitForElement('button#accept')(chatbox)
|
|
|
|
acceptButton = chatbox.find('button#accept')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
acceptButton.simulate('click')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockCreateRoom).toHaveBeenCalled()
|
|
|
|
});
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
const input = chatbox.find('#message-input')
|
|
|
|
const form = chatbox.find('form')
|
|
|
|
const message = 'Hello'
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
input.simulate('change', { target: { value: message }})
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
await waitForExpect(() => {
|
|
|
|
chatbox.update()
|
|
|
|
expect(chatbox.state().inputValue).toEqual(message)
|
|
|
|
})
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
form.simulate('submit')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockSendTextMessage).toHaveBeenCalledWith(chatbox.state().roomId, message)
|
|
|
|
});
|
2020-03-12 17:08:57 +00:00
|
|
|
})
|
|
|
|
|
2020-03-13 17:35:07 +00:00
|
|
|
|
|
|
|
test('decryption failure should lead to a new unencrypted chat', async () => {
|
|
|
|
const chatbox = mount(<Chatbox {...testConfig} />)
|
|
|
|
const dock = chatbox.find('button.dock')
|
|
|
|
const instance = chatbox.instance()
|
|
|
|
|
|
|
|
dock.simulate('click')
|
|
|
|
|
|
|
|
const openChatWindow = await createWaitForElement('.widget-entered')(chatbox)
|
|
|
|
let acceptButton = await createWaitForElement('button#accept')(chatbox)
|
|
|
|
acceptButton = chatbox.find('button#accept')
|
|
|
|
|
|
|
|
acceptButton.simulate('click')
|
|
|
|
|
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockCreateRoom).toHaveBeenCalled()
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.spyOn(instance, 'initializeUnencryptedChat')
|
|
|
|
instance.handleDecryptionError()
|
|
|
|
|
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockLeave).toHaveBeenCalled()
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockStopClient).toHaveBeenCalled()
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockClearStores).toHaveBeenCalled()
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(instance.initializeUnencryptedChat).toHaveBeenCalled()
|
2020-03-12 17:08:57 +00:00
|
|
|
})
|
|
|
|
|
2020-03-13 17:35:07 +00:00
|
|
|
test('creating an unencrypted chat', async () => {
|
|
|
|
const chatbox = mount(<Chatbox {...testConfig} />)
|
|
|
|
const instance = chatbox.instance()
|
|
|
|
|
|
|
|
instance.initializeUnencryptedChat()
|
|
|
|
|
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(createClient).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockStartClient).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockInitCrypto).not.toHaveBeenCalled()
|
|
|
|
})
|
2020-03-12 17:08:57 +00:00
|
|
|
})
|
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
test('exiting the chat should leave the room and destroy client', async () => {
|
|
|
|
const chatbox = mount(<Chatbox {...testConfig} />)
|
|
|
|
const dock = chatbox.find('button.dock')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
dock.simulate('click')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
const openChatWindow = await createWaitForElement('.widget-entered')(chatbox)
|
|
|
|
let acceptButton = await createWaitForElement('button#accept')(chatbox)
|
|
|
|
acceptButton = chatbox.find('button#accept')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
acceptButton.simulate('click')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockCreateRoom).toHaveBeenCalled()
|
|
|
|
});
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
const exitButton = chatbox.find('button.widget-header-close')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
exitButton.simulate('click')
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
let closed = await createWaitForElement('button.dock')
|
|
|
|
expect(closed.length).toEqual(1)
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockLeave).toHaveBeenCalled()
|
|
|
|
});
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockDeactivateAccount).toHaveBeenCalled()
|
|
|
|
});
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockStopClient).toHaveBeenCalled()
|
|
|
|
});
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
await waitForExpect(() => {
|
|
|
|
expect(mockClearStores).toHaveBeenCalled()
|
|
|
|
});
|
2020-03-12 17:08:57 +00:00
|
|
|
})
|
2020-03-13 17:35:07 +00:00
|
|
|
|
|
|
|
test('notification should appear when facilitator joins chat', () => {
|
|
|
|
//
|
|
|
|
})
|
|
|
|
|
|
|
|
test('received messages should appear in chat window', () => {
|
|
|
|
//
|
|
|
|
})
|
|
|
|
|
2020-03-12 17:08:57 +00:00
|
|
|
});
|