From cb592dc709cb0386cbcd218660454ad87cf8467b Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 14 Jul 2023 11:55:55 +0100 Subject: [PATCH] Fix clicking MXID in timeline going to matrix.to (#11263) * Fix clicking MXID in timeline going to matrix.to * Add tests * Increase coverage --- src/linkify-matrix.ts | 8 ++---- test/linkify-matrix-test.ts | 49 ++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/linkify-matrix.ts b/src/linkify-matrix.ts index f6dcbfff6f..f5a419f0a7 100644 --- a/src/linkify-matrix.ts +++ b/src/linkify-matrix.ts @@ -139,7 +139,6 @@ export const options: Opts = { const permalink = parsePermalink(href); if (permalink?.userId) { return { - // @ts-ignore see https://linkify.js.org/docs/options.html click: function (e: MouseEvent) { onUserClick(e, permalink.userId!); }, @@ -150,7 +149,6 @@ export const options: Opts = { if (localHref !== href) { // it could be converted to a localHref -> therefore handle locally return { - // @ts-ignore see https://linkify.js.org/docs/options.html click: function (e: MouseEvent) { e.preventDefault(); window.location.hash = localHref; @@ -165,17 +163,15 @@ export const options: Opts = { } case Type.UserId: return { - // @ts-ignore see https://linkify.js.org/docs/options.html click: function (e: MouseEvent) { - const userId = parsePermalink(href)?.userId; + const userId = parsePermalink(href)?.userId ?? href; if (userId) onUserClick(e, userId); }, }; case Type.RoomAlias: return { - // @ts-ignore see https://linkify.js.org/docs/options.html click: function (e: MouseEvent) { - const alias = parsePermalink(href)?.roomIdOrAlias; + const alias = parsePermalink(href)?.roomIdOrAlias ?? href; if (alias) onAliasClick(e, alias); }, }; diff --git a/test/linkify-matrix-test.ts b/test/linkify-matrix-test.ts index 4fd2166c2b..bb27accf77 100644 --- a/test/linkify-matrix-test.ts +++ b/test/linkify-matrix-test.ts @@ -13,7 +13,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import { linkify, Type } from "../src/linkify-matrix"; + +import { EventListeners } from "linkifyjs"; + +import { linkify, Type, options } from "../src/linkify-matrix"; +import dispatcher from "../src/dispatcher/dispatcher"; +import { Action } from "../src/dispatcher/actions"; describe("linkify-matrix", () => { const linkTypesByInitialCharacter: Record = { @@ -324,6 +329,26 @@ describe("linkify-matrix", () => { describe("roomalias plugin", () => { genTests("#"); + + it("should intercept clicks with a ViewRoom dispatch", () => { + const dispatchSpy = jest.spyOn(dispatcher, "dispatch"); + + const handlers = (options.events as (href: string, type: string) => EventListeners)( + "#room:server.com", + "roomalias", + ); + + const event = new MouseEvent("mousedown"); + event.preventDefault = jest.fn(); + handlers.click(event); + expect(event.preventDefault).toHaveBeenCalled(); + expect(dispatchSpy).toHaveBeenCalledWith( + expect.objectContaining({ + action: Action.ViewRoom, + room_alias: "#room:server.com", + }), + ); + }); }); describe("userid plugin", () => { @@ -344,6 +369,28 @@ describe("linkify-matrix", () => { }, ]); }); + + it("should intercept clicks with a ViewUser dispatch", () => { + const dispatchSpy = jest.spyOn(dispatcher, "dispatch"); + + const handlers = (options.events as (href: string, type: string) => EventListeners)( + "@localpart:server.com", + "userid", + ); + + const event = new MouseEvent("mousedown"); + event.preventDefault = jest.fn(); + handlers.click(event); + expect(event.preventDefault).toHaveBeenCalled(); + expect(dispatchSpy).toHaveBeenCalledWith( + expect.objectContaining({ + action: Action.ViewUser, + member: expect.objectContaining({ + userId: "@localpart:server.com", + }), + }), + ); + }); }); describe("matrix uri", () => {