element-web/test/components/views/rooms/MessageComposerInput-test.js

299 lines
11 KiB
JavaScript
Raw Normal View History

import React from 'react';
import ReactTestUtils from 'react-dom/test-utils';
import ReactDOM from 'react-dom';
import * as testUtils from '../../../test-utils';
2019-12-16 14:12:48 +03:00
import sdk from '../../../skinned-sdk';
const MessageComposerInput = sdk.getComponent('views.rooms.MessageComposerInput');
import MatrixClientPeg from '../../../../src/MatrixClientPeg';
import {sleep} from "../../../../src/utils/promise";
function addTextToDraft(text) {
const components = document.getElementsByClassName('public-DraftEditor-content');
if (components && components.length) {
const textarea = components[0];
const textEvent = document.createEvent('TextEvent');
textEvent.initTextEvent('textInput', true, true, null, text);
textarea.dispatchEvent(textEvent);
}
}
// FIXME: These tests need to be updated from Draft to Slate.
xdescribe('MessageComposerInput', () => {
let parentDiv = null,
client = null,
mci = null,
room = testUtils.mkStubRoom('!DdJkzRliezrwpNebLk:matrix.org');
beforeEach(function() {
2019-12-16 14:12:48 +03:00
testUtils.stubClient();
client = MatrixClientPeg.get();
client.credentials = {userId: '@me:domain.com'};
parentDiv = document.createElement('div');
document.body.appendChild(parentDiv);
mci = ReactDOM.render(
<MessageComposerInput
room={room}
client={client}
/>,
parentDiv);
});
afterEach((done) => {
// hack: let the component finish mounting before unmounting, to avoid
// warnings
// (please can we make the components not setState() after
// they are unmounted?)
sleep(10).done(() => {
if (parentDiv) {
ReactDOM.unmountComponentAtNode(parentDiv);
parentDiv.remove();
parentDiv = null;
}
done();
});
});
// XXX this fails
xit('should change mode if indicator is clicked', (done) => {
mci.enableRichtext(true);
setTimeout(() => {
const indicator = ReactTestUtils.findRenderedDOMComponentWithClass(
mci,
'mx_MessageComposer_input_markdownIndicator');
ReactTestUtils.Simulate.click(indicator);
expect(mci.state.isRichTextEnabled).toEqual(false, 'should have changed mode');
done();
});
});
it('should not send messages when composer is empty', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(true);
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
2019-12-16 14:12:48 +03:00
expect(spy).not.toBeCalled();
});
it('should not change content unnecessarily on RTE -> Markdown conversion', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(true);
addTextToDraft('a');
mci.handleKeyCommand('toggle-mode');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('a');
});
it('should not change content unnecessarily on Markdown -> RTE conversion', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('a');
mci.handleKeyCommand('toggle-mode');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('a');
});
2017-06-30 14:56:19 +03:00
it('should send emoji messages when rich text is enabled', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(true);
addTextToDraft('☹');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true, 'should send message');
});
2017-06-30 14:56:19 +03:00
it('should send emoji messages when Markdown is enabled', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('☹');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true, 'should send message');
});
2016-09-13 14:59:17 +03:00
// FIXME
// it('should convert basic Markdown to rich text correctly', () => {
2019-12-16 14:12:48 +03:00
// const spy = jest.spyOn(client, 'sendHtmlMessage');
2016-09-13 14:59:17 +03:00
// mci.enableRichtext(false);
// addTextToDraft('*abc*');
// mci.handleKeyCommand('toggle-mode');
2019-12-16 14:12:48 +03:00
// mci.handleReturn(jest.fn());
2016-09-13 14:59:17 +03:00
// console.error(spy.args[0][2]);
// expect(spy.args[0][2]).toContain('<em>abc');
// });
//
// it('should convert basic rich text to Markdown correctly', () => {
2019-12-16 14:12:48 +03:00
// const spy = jest.spyOn(client, 'sendHtmlMessage');
2016-09-13 14:59:17 +03:00
// mci.enableRichtext(true);
// process.nextTick(() => {
//
// });
// mci.handleKeyCommand('italic');
// addTextToDraft('abc');
// mci.handleKeyCommand('toggle-mode');
2019-12-16 14:12:48 +03:00
// mci.handleReturn(jest.fn());
2016-09-13 14:59:17 +03:00
// expect(['_abc_', '*abc*']).toContain(spy.args[0][1]);
// });
it('should insert formatting characters in Markdown mode', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
mci.handleKeyCommand('italic');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(['__', '**']).toContain(spy.args[0][1].body);
});
it('should not entity-encode " in Markdown mode', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('"');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('"');
});
it('should escape characters without other markup in Markdown mode', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('\\*escaped\\*');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('*escaped*');
});
it('should escape characters with other markup in Markdown mode', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('\\*escaped\\* *italic*');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('\\*escaped\\* *italic*');
expect(spy.args[0][1].formatted_body).toEqual('*escaped* <em>italic</em>');
});
it('should not convert -_- into a horizontal rule in Markdown mode', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('-_-');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('-_-');
});
it('should not strip <del> tags in Markdown mode', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('<del>striked-out</del>');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('<del>striked-out</del>');
expect(spy.args[0][1].formatted_body).toEqual('<del>striked-out</del>');
});
it('should not strike-through ~~~ in Markdown mode', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('~~~striked-out~~~');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('~~~striked-out~~~');
});
it('should not mark single unmarkedup paragraphs as HTML in Markdown mode', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
});
it('should not mark two unmarkedup paragraphs as HTML in Markdown mode', () => {
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
addTextToDraft('Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\nFusce congue sapien sed neque molestie volutpat.');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1].body).toEqual('Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\nFusce congue sapien sed neque molestie volutpat.');
});
it('should strip tab-completed mentions so that only the display name is sent in the plain body in Markdown mode', () => {
// Sending a HTML message because we have entities in the composer (because of completions)
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(false);
mci.setDisplayedCompletion({
completion: 'Some Member',
selection: mci.state.editorState.getSelection(),
href: `https://matrix.to/#/@some_member:domain.bla`,
});
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.args[0][1].body).toEqual(
'Some Member',
'the plaintext body should only include the display name',
);
expect(spy.args[0][1].formatted_body).toEqual(
'<a href="https://matrix.to/#/@some_member:domain.bla">Some Member</a>',
'the html body should contain an anchor tag with a matrix.to href and display name text',
);
});
it('should strip tab-completed mentions so that only the display name is sent in the plain body in RTE mode', () => {
// Sending a HTML message because we have entities in the composer (because of completions)
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
mci.enableRichtext(true);
mci.setDisplayedCompletion({
completion: 'Some Member',
selection: mci.state.editorState.getSelection(),
href: `https://matrix.to/#/@some_member:domain.bla`,
});
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.args[0][1].body).toEqual('Some Member');
expect(spy.args[0][1].formatted_body).toEqual('<a href="https://matrix.to/#/@some_member:domain.bla">Some Member</a>');
});
it('should not strip non-tab-completed mentions when manually typing MD', () => {
// Sending a HTML message because we have entities in the composer (because of completions)
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
// Markdown mode enabled
mci.enableRichtext(false);
addTextToDraft('[My Not-Tab-Completed Mention](https://matrix.to/#/@some_member:domain.bla)');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.args[0][1].body).toEqual('[My Not-Tab-Completed Mention](https://matrix.to/#/@some_member:domain.bla)');
expect(spy.args[0][1].formatted_body).toEqual('<a href="https://matrix.to/#/@some_member:domain.bla">My Not-Tab-Completed Mention</a>');
});
it('should not strip arbitrary typed (i.e. not tab-completed) MD links', () => {
// Sending a HTML message because we have entities in the composer (because of completions)
2019-12-16 14:12:48 +03:00
const spy = jest.spyOn(client, 'sendMessage');
// Markdown mode enabled
mci.enableRichtext(false);
addTextToDraft('[Click here](https://some.lovely.url)');
2019-12-16 14:12:48 +03:00
mci.handleReturn(jest.fn());
expect(spy.args[0][1].body).toEqual('[Click here](https://some.lovely.url)');
expect(spy.args[0][1].formatted_body).toEqual('<a href="https://some.lovely.url">Click here</a>');
});
});