fix tests

This commit is contained in:
Sharon Kennedy 2020-03-27 18:21:13 -04:00
parent 3fd9c7365d
commit 3f2aa3a9db
8 changed files with 65 additions and 9 deletions

View File

@ -0,0 +1,6 @@
import React from "react";
import { ReactElement } from "react";
const MockPicker = () => <div>Emoji Picker</div>
export default MockPicker;

View File

@ -45,6 +45,7 @@ export const mockDeactivateAccount = jest.fn(() => {
return Promise.resolve('value'); return Promise.resolve('value');
}); });
export const mockOn = jest.fn() export const mockOn = jest.fn()
export const mockSetDisplayName = jest.fn()
export const mockClient = { export const mockClient = {
registerRequest: mockRegisterRequest, registerRequest: mockRegisterRequest,
@ -64,6 +65,9 @@ export const mockClient = {
setPowerLevel: mockSetPowerLevel, setPowerLevel: mockSetPowerLevel,
sendTextMessage: mockSendTextMessage, sendTextMessage: mockSendTextMessage,
deactivateAccount: mockDeactivateAccount, deactivateAccount: mockDeactivateAccount,
setDisplayName: mockSetDisplayName,
} }
export const WebStorageSessionStore = jest.fn()
export const createClient = jest.fn().mockReturnValue(mockClient) export const createClient = jest.fn().mockReturnValue(mockClient)

View File

@ -471,8 +471,6 @@ class ChatBox extends React.Component {
handleSubmit = e => { handleSubmit = e => {
e.preventDefault() e.preventDefault()
const message = this.state.inputValue const message = this.state.inputValue
console.log('event', e)
console.log('message', message)
if (!Boolean(message)) return null; if (!Boolean(message)) return null;
if (this.state.client && this.state.roomId) { if (this.state.client && this.state.roomId) {

View File

@ -21,24 +21,24 @@ import { createWaitForElement } from 'enzyme-wait';
import { config } from 'react-transition-group'; import { config } from 'react-transition-group';
import waitForExpect from 'wait-for-expect' import waitForExpect from 'wait-for-expect'
config.disabled = true config.disabled = true
const testConfig = { var testConfig = {
matrixServerUrl: 'https://matrix.rhok.space', matrixServerUrl: 'https://matrix.rhok.space',
botUsername: '@help-bot:rhok.space', botId: '@help-bot:rhok.space',
roomName: 'Support Chat', roomName: 'Support Chat',
termsUrl: 'https://tosdr.org/', 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.', introMessage: 'This chat application does not collect any of your personal data or any data from your use of this service.',
agreementMessage: '👉 Do you want to continue? Type yes or no.', agreementMessage: 'Do you want to continue?',
confirmationMessage: 'Waiting for a facilitator to join the chat...', confirmationMessage: 'Waiting for a facilitator to join the chat...',
exitMessage: 'The chat was not started.', exitMessage: 'The chat is closed. You may close this window.',
chatUnavailableMessage: 'The chat service is not available right now. Please try again later.', chatUnavailableMessage: 'The chat service is not available right now. Please try again later.',
anonymousDisplayName: 'Anonymous', anonymousDisplayName: 'Anonymous',
} }
describe('Chatbox', () => { describe('Chatbox', () => {
beforeEach(() => { beforeEach(() => {
createClient.mockClear() createClient.mockClear()
mockInitCrypto.mockClear() mockInitCrypto.mockClear()
@ -52,6 +52,7 @@ describe('Chatbox', () => {
mockClearStores.mockClear() mockClearStores.mockClear()
mockOnce.mockClear() mockOnce.mockClear()
mockOn.mockClear() mockOn.mockClear()
mockSendTextMessage.mockClear()
}) })
test('chat window should open and close', async () => { test('chat window should open and close', async () => {

View File

@ -28,7 +28,10 @@ class EmojiSelector extends React.Component {
status => { status => {
return( return(
<div className={`emoji-picker emoji-picker-${status}`} aria-hidden={!emojiSelectorOpen}> <div className={`emoji-picker emoji-picker-${status}`} aria-hidden={!emojiSelectorOpen}>
<EmojiPicker onEmojiClick={onEmojiClick} /> <EmojiPicker
onEmojiClick={onEmojiClick}
emojiUrl="https://cdn.jsdelivr.net/gh/iamcal/emoji-data@master/img-apple-64"
/>
</div> </div>
) )
} }

View File

@ -2,6 +2,13 @@ import ReactDOM from 'react-dom';
import bookmarklet from './bookmarklet'; import bookmarklet from './bookmarklet';
describe('bookmarklet', () => { describe('bookmarklet', () => {
beforeAll(() => {
const el = document.querySelectorAll('body > div');
ReactDOM.unmountComponentAtNode(el[0]);
el[0].parentNode.removeChild(el[0]);
window.EmbeddableChatbox = null;
})
afterEach(() => { afterEach(() => {
const el = document.querySelectorAll('body > div'); const el = document.querySelectorAll('body > div');
ReactDOM.unmountComponentAtNode(el[0]); ReactDOM.unmountComponentAtNode(el[0]);

View File

@ -1,8 +1,15 @@
import EmbeddableChatbox from './embeddable-chatbox'; import EmbeddableChatbox from './embeddable-chatbox';
import { waitForSelection } from '../test-helpers'; import { waitForSelection } from '../utils/test-helpers';
describe('EmbeddableChatbox', () => { describe('EmbeddableChatbox', () => {
beforeAll(() => {
document.readyState = 'complete';
if (EmbeddableChatbox.el) {
EmbeddableChatbox.unmount();
}
});
afterEach(() => { afterEach(() => {
document.readyState = 'complete'; document.readyState = 'complete';
if (EmbeddableChatbox.el) { if (EmbeddableChatbox.el) {

30
src/utils/test-helpers.js Normal file
View File

@ -0,0 +1,30 @@
function checkFunc(dom, selector) {
if (typeof dom.update === 'function') {
const el = dom.update().find(selector);
if (el.exists()) {
return el;
}
return null;
}
const els = dom.querySelectorAll(selector);
if (els.length !== 0) {
return els;
}
return null;
}
export async function waitForSelection(dom, selector) {
let numSleep = 0;
for (;;) {
const el = checkFunc(dom, selector);
if (el) {
return el;
}
if (numSleep > 2) {
throw new Error(`could not find ${selector}`);
}
await new Promise(resolve => setTimeout(resolve, 250));
numSleep += 1;
}
}