2016-09-09 14:37:42 +02:00
|
|
|
import React from 'react';
|
2019-08-24 12:47:07 +02:00
|
|
|
import ReactTestUtils from 'react-dom/test-utils';
|
2016-09-09 14:37:42 +02:00
|
|
|
import ReactDOM from 'react-dom';
|
2018-12-25 03:55:10 +01:00
|
|
|
import expect from 'expect';
|
2016-09-09 14:37:42 +02:00
|
|
|
import sinon from 'sinon';
|
2017-07-12 14:58:14 +02:00
|
|
|
import Promise from 'bluebird';
|
2016-09-09 14:37:42 +02:00
|
|
|
import * as testUtils from '../../../test-utils';
|
|
|
|
import sdk from 'matrix-react-sdk';
|
|
|
|
const MessageComposerInput = sdk.getComponent('views.rooms.MessageComposerInput');
|
2017-05-26 00:13:49 +02:00
|
|
|
import MatrixClientPeg from '../../../../src/MatrixClientPeg';
|
2016-09-09 14:37:42 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 01:52:27 +02:00
|
|
|
// FIXME: These tests need to be updated from Draft to Slate.
|
|
|
|
|
|
|
|
xdescribe('MessageComposerInput', () => {
|
2016-09-09 14:37:42 +02:00
|
|
|
let parentDiv = null,
|
|
|
|
sandbox = null,
|
|
|
|
client = null,
|
|
|
|
mci = null,
|
|
|
|
room = testUtils.mkStubRoom('!DdJkzRliezrwpNebLk:matrix.org');
|
|
|
|
|
2016-10-10 18:51:26 +02:00
|
|
|
beforeEach(function() {
|
|
|
|
testUtils.beforeEach(this);
|
2016-09-09 14:37:42 +02:00
|
|
|
sandbox = testUtils.stubClient(sandbox);
|
|
|
|
client = MatrixClientPeg.get();
|
2017-07-24 15:42:20 +02:00
|
|
|
client.credentials = {userId: '@me:domain.com'};
|
2016-09-09 14:37:42 +02:00
|
|
|
|
|
|
|
parentDiv = document.createElement('div');
|
|
|
|
document.body.appendChild(parentDiv);
|
|
|
|
mci = ReactDOM.render(
|
|
|
|
<MessageComposerInput
|
|
|
|
room={room}
|
|
|
|
client={client}
|
|
|
|
/>,
|
|
|
|
parentDiv);
|
|
|
|
});
|
|
|
|
|
2016-10-10 18:51:26 +02:00
|
|
|
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?)
|
2017-07-12 15:04:20 +02:00
|
|
|
Promise.delay(10).done(() => {
|
2016-10-10 18:51:26 +02:00
|
|
|
if (parentDiv) {
|
|
|
|
ReactDOM.unmountComponentAtNode(parentDiv);
|
|
|
|
parentDiv.remove();
|
|
|
|
parentDiv = null;
|
|
|
|
}
|
|
|
|
sandbox.restore();
|
|
|
|
done();
|
2017-10-11 18:56:17 +02:00
|
|
|
});
|
2016-09-09 14:37:42 +02:00
|
|
|
});
|
|
|
|
|
2016-10-10 18:51:26 +02:00
|
|
|
// XXX this fails
|
|
|
|
xit('should change mode if indicator is clicked', (done) => {
|
2016-09-09 14:37:42 +02:00
|
|
|
mci.enableRichtext(true);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
const indicator = ReactTestUtils.findRenderedDOMComponentWithClass(
|
|
|
|
mci,
|
|
|
|
'mx_MessageComposer_input_markdownIndicator');
|
|
|
|
ReactTestUtils.Simulate.click(indicator);
|
|
|
|
|
2018-05-20 17:30:39 +02:00
|
|
|
expect(mci.state.isRichTextEnabled).toEqual(false, 'should have changed mode');
|
2016-10-10 18:51:26 +02:00
|
|
|
done();
|
2016-09-09 14:37:42 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not send messages when composer is empty', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2016-09-09 14:37:42 +02:00
|
|
|
mci.enableRichtext(true);
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.calledOnce).toEqual(false, 'should not send message');
|
2016-09-09 14:37:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not change content unnecessarily on RTE -> Markdown conversion', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2016-09-09 14:37:42 +02:00
|
|
|
mci.enableRichtext(true);
|
|
|
|
addTextToDraft('a');
|
|
|
|
mci.handleKeyCommand('toggle-mode');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('a');
|
2016-09-09 14:37:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not change content unnecessarily on Markdown -> RTE conversion', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2016-09-09 14:37:42 +02:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('a');
|
|
|
|
mci.handleKeyCommand('toggle-mode');
|
|
|
|
mci.handleReturn(sinon.stub());
|
2017-06-23 18:08:37 +02:00
|
|
|
|
2016-09-09 14:37:42 +02:00
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('a');
|
2016-09-09 14:37:42 +02:00
|
|
|
});
|
|
|
|
|
2017-06-30 13:56:19 +02:00
|
|
|
it('should send emoji messages when rich text is enabled', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2016-09-09 14:37:42 +02:00
|
|
|
mci.enableRichtext(true);
|
|
|
|
addTextToDraft('☹');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true, 'should send message');
|
|
|
|
});
|
|
|
|
|
2017-06-30 13:56:19 +02:00
|
|
|
it('should send emoji messages when Markdown is enabled', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2016-09-09 14:37:42 +02:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('☹');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true, 'should send message');
|
|
|
|
});
|
|
|
|
|
2016-09-13 13:59:17 +02:00
|
|
|
// FIXME
|
|
|
|
// it('should convert basic Markdown to rich text correctly', () => {
|
|
|
|
// const spy = sinon.spy(client, 'sendHtmlMessage');
|
|
|
|
// mci.enableRichtext(false);
|
|
|
|
// addTextToDraft('*abc*');
|
|
|
|
// mci.handleKeyCommand('toggle-mode');
|
|
|
|
// mci.handleReturn(sinon.stub());
|
|
|
|
// console.error(spy.args[0][2]);
|
|
|
|
// expect(spy.args[0][2]).toContain('<em>abc');
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// it('should convert basic rich text to Markdown correctly', () => {
|
|
|
|
// const spy = sinon.spy(client, 'sendHtmlMessage');
|
|
|
|
// mci.enableRichtext(true);
|
|
|
|
// process.nextTick(() => {
|
|
|
|
//
|
|
|
|
// });
|
|
|
|
// mci.handleKeyCommand('italic');
|
|
|
|
// addTextToDraft('abc');
|
|
|
|
// mci.handleKeyCommand('toggle-mode');
|
|
|
|
// mci.handleReturn(sinon.stub());
|
|
|
|
// expect(['_abc_', '*abc*']).toContain(spy.args[0][1]);
|
|
|
|
// });
|
2016-09-09 14:37:42 +02:00
|
|
|
|
|
|
|
it('should insert formatting characters in Markdown mode', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2016-09-09 14:37:42 +02:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
mci.handleKeyCommand('italic');
|
|
|
|
mci.handleReturn(sinon.stub());
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(['__', '**']).toContain(spy.args[0][1].body);
|
2016-09-09 14:37:42 +02:00
|
|
|
});
|
|
|
|
|
2017-01-18 20:54:34 +01:00
|
|
|
it('should not entity-encode " in Markdown mode', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-01-18 20:54:34 +01:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('"');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('"');
|
2017-01-18 20:54:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should escape characters without other markup in Markdown mode', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-01-18 20:54:34 +01:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('\\*escaped\\*');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('*escaped*');
|
2017-01-18 20:54:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should escape characters with other markup in Markdown mode', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-01-18 20:54:34 +01:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('\\*escaped\\* *italic*');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('\\*escaped\\* *italic*');
|
|
|
|
expect(spy.args[0][1].formatted_body).toEqual('*escaped* <em>italic</em>');
|
2017-01-18 20:54:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not convert -_- into a horizontal rule in Markdown mode', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-01-18 20:54:34 +01:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('-_-');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('-_-');
|
2017-01-18 20:54:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not strip <del> tags in Markdown mode', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-01-18 20:54:34 +01:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('<del>striked-out</del>');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('<del>striked-out</del>');
|
|
|
|
expect(spy.args[0][1].formatted_body).toEqual('<del>striked-out</del>');
|
2017-01-18 20:54:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not strike-through ~~~ in Markdown mode', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-01-18 20:54:34 +01:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('~~~striked-out~~~');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('~~~striked-out~~~');
|
2017-01-18 20:54:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not mark single unmarkedup paragraphs as HTML in Markdown mode', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-01-18 20:54:34 +01:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
|
2017-01-18 20:54:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not mark two unmarkedup paragraphs as HTML in Markdown mode', () => {
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-01-18 20:54:34 +01:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\nFusce congue sapien sed neque molestie volutpat.');
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
|
|
|
expect(spy.calledOnce).toEqual(true);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual('Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\nFusce congue sapien sed neque molestie volutpat.');
|
2017-01-18 20:54:34 +01:00
|
|
|
});
|
2017-07-24 15:42:20 +02:00
|
|
|
|
|
|
|
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)
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-07-24 15:42:20 +02:00
|
|
|
mci.enableRichtext(false);
|
|
|
|
mci.setDisplayedCompletion({
|
|
|
|
completion: 'Some Member',
|
|
|
|
selection: mci.state.editorState.getSelection(),
|
|
|
|
href: `https://matrix.to/#/@some_member:domain.bla`,
|
|
|
|
});
|
|
|
|
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].body).toEqual(
|
2017-07-24 15:42:20 +02:00
|
|
|
'Some Member',
|
|
|
|
'the plaintext body should only include the display name',
|
|
|
|
);
|
2018-05-01 14:26:24 +02:00
|
|
|
expect(spy.args[0][1].formatted_body).toEqual(
|
2017-07-24 15:42:20 +02:00
|
|
|
'<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)
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-07-24 15:42:20 +02:00
|
|
|
mci.enableRichtext(true);
|
|
|
|
mci.setDisplayedCompletion({
|
|
|
|
completion: 'Some Member',
|
|
|
|
selection: mci.state.editorState.getSelection(),
|
|
|
|
href: `https://matrix.to/#/@some_member:domain.bla`,
|
|
|
|
});
|
|
|
|
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
2018-05-01 14:26:24 +02:00
|
|
|
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>');
|
2017-07-24 15:42:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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)
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-07-24 15:42:20 +02:00
|
|
|
// Markdown mode enabled
|
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('[My Not-Tab-Completed Mention](https://matrix.to/#/@some_member:domain.bla)');
|
|
|
|
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
2018-05-01 14:26:24 +02:00
|
|
|
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>');
|
2017-07-24 15:42:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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)
|
2018-05-01 14:26:24 +02:00
|
|
|
const spy = sinon.spy(client, 'sendMessage');
|
2017-07-24 15:42:20 +02:00
|
|
|
// Markdown mode enabled
|
|
|
|
mci.enableRichtext(false);
|
|
|
|
addTextToDraft('[Click here](https://some.lovely.url)');
|
|
|
|
|
|
|
|
mci.handleReturn(sinon.stub());
|
|
|
|
|
2018-05-01 14:26:24 +02:00
|
|
|
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>');
|
2017-07-24 15:42:20 +02:00
|
|
|
});
|
2019-08-24 12:47:07 +02:00
|
|
|
});
|