From f7145941fda926a7a67b078d8b8de10960cbee57 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Mon, 24 Jul 2017 14:42:20 +0100 Subject: [PATCH] Add tests to assert correct MD mention link stripping Tab-completed @Mentions should only be sent as display names in the `body` of the event. The HTML should be unaffected, and always sent as an anchor tag. --- .../views/rooms/MessageComposerInput-test.js | 66 +++++++++++++++++++ test/test-utils.js | 7 +- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/test/components/views/rooms/MessageComposerInput-test.js b/test/components/views/rooms/MessageComposerInput-test.js index fe379afcff..e5d98e26c6 100644 --- a/test/components/views/rooms/MessageComposerInput-test.js +++ b/test/components/views/rooms/MessageComposerInput-test.js @@ -9,6 +9,7 @@ import sdk from 'matrix-react-sdk'; import UserSettingsStore from '../../../../src/UserSettingsStore'; const MessageComposerInput = sdk.getComponent('views.rooms.MessageComposerInput'); import MatrixClientPeg from '../../../../src/MatrixClientPeg'; +import RoomMember from 'matrix-js-sdk'; function addTextToDraft(text) { const components = document.getElementsByClassName('public-DraftEditor-content'); @@ -31,6 +32,7 @@ describe('MessageComposerInput', () => { testUtils.beforeEach(this); sandbox = testUtils.stubClient(sandbox); client = MatrixClientPeg.get(); + client.credentials = {userId: '@me:domain.com'}; parentDiv = document.createElement('div'); document.body.appendChild(parentDiv); @@ -236,4 +238,68 @@ describe('MessageComposerInput', () => { expect(spy.calledOnce).toEqual(true); expect(spy.args[0][1]).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) + const spy = sinon.spy(client, 'sendHtmlMessage'); + 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()); + + expect(spy.args[0][1]).toEqual( + 'Some Member', + 'the plaintext body should only include the display name', + ); + expect(spy.args[0][2]).toEqual( + 'Some Member', + '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) + const spy = sinon.spy(client, 'sendHtmlMessage'); + 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()); + + expect(spy.args[0][1]).toEqual('Some Member'); + expect(spy.args[0][2]).toEqual('Some Member'); + }); + + 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) + const spy = sinon.spy(client, 'sendHtmlMessage'); + // Markdown mode enabled + mci.enableRichtext(false); + addTextToDraft('[My Not-Tab-Completed Mention](https://matrix.to/#/@some_member:domain.bla)'); + + mci.handleReturn(sinon.stub()); + + expect(spy.args[0][1]).toEqual('[My Not-Tab-Completed Mention](https://matrix.to/#/@some_member:domain.bla)'); + expect(spy.args[0][2]).toEqual('My Not-Tab-Completed Mention'); + }); + + 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) + const spy = sinon.spy(client, 'sendHtmlMessage'); + // Markdown mode enabled + mci.enableRichtext(false); + addTextToDraft('[Click here](https://some.lovely.url)'); + + mci.handleReturn(sinon.stub()); + + expect(spy.args[0][1]).toEqual('[Click here](https://some.lovely.url)'); + expect(spy.args[0][2]).toEqual('Click here'); + }); }); diff --git a/test/test-utils.js b/test/test-utils.js index 23f16a2e4c..06d3c03c49 100644 --- a/test/test-utils.js +++ b/test/test-utils.js @@ -238,7 +238,12 @@ export function mkStubRoom(roomId = null) { return { roomId, getReceiptsForEvent: sinon.stub().returns([]), - getMember: sinon.stub().returns({}), + getMember: sinon.stub().returns({ + userId: '@member:domain.bla', + name: 'Member', + roomId: roomId, + getAvatarUrl: () => 'mxc://avatar.url/image.png', + }), getJoinedMembers: sinon.stub().returns([]), getPendingEvents: () => [], getLiveTimeline: () => stubTimeline,