Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Fix user pill click (#10359)
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 authored Mar 13, 2023
1 parent 1e46efe commit 41d88ad
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 177 deletions.
1 change: 1 addition & 0 deletions src/hooks/usePermalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export const usePermalink: (args: Args) => HookResult = ({ room, type: argType,
text = member.name || resourceId;
onClick = (e: ButtonEvent): void => {
e.preventDefault();
e.stopPropagation();
dis.dispatch({
action: Action.ViewUser,
member: member,
Expand Down
20 changes: 17 additions & 3 deletions test/components/views/elements/Pill-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from "../../../test-utils";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { Action } from "../../../../src/dispatcher/actions";
import { ButtonEvent } from "../../../../src/components/views/elements/AccessibleButton";

describe("<Pill>", () => {
let client: Mocked<MatrixClient>;
Expand All @@ -43,14 +44,20 @@ describe("<Pill>", () => {
const user1Id = "@user1:example.com";
const user2Id = "@user2:example.com";
let renderResult: RenderResult;
let pillParentClickHandler: (e: ButtonEvent) => void;

const renderPill = (props: PillProps): void => {
const withDefault = {
inMessage: true,
shouldShowPillAvatar: true,
...props,
} as PillProps;
renderResult = render(<Pill {...withDefault} />);
// wrap Pill with a div to allow testing of event bubbling
renderResult = render(
<div onClick={pillParentClickHandler}>
<Pill {...withDefault} />
</div>,
);
};

filterConsole(
Expand Down Expand Up @@ -88,6 +95,7 @@ describe("<Pill>", () => {
});

jest.spyOn(dis, "dispatch");
pillParentClickHandler = jest.fn();
});

describe("when rendering a pill for a room", () => {
Expand Down Expand Up @@ -168,11 +176,13 @@ describe("<Pill>", () => {
await userEvent.click(screen.getByText("User 1"));
});

it("should dipsatch a view user action", () => {
it("should dipsatch a view user action and prevent event bubbling", () => {
expect(dis.dispatch).toHaveBeenCalledWith({
action: Action.ViewUser,
member: room1.getMember(user1Id),
});

expect(pillParentClickHandler).not.toHaveBeenCalled();
});
});
});
Expand All @@ -195,7 +205,11 @@ describe("<Pill>", () => {
renderPill({
url: permalinkPrefix,
});
expect(renderResult.asFragment()).toMatchInlineSnapshot(`<DocumentFragment />`);
expect(renderResult.asFragment()).toMatchInlineSnapshot(`
<DocumentFragment>
<div />
</DocumentFragment>
`);
});

it("should not render an avatar or link when called with inMessage = false and shouldShowPillAvatar = false", () => {
Expand Down
Loading

0 comments on commit 41d88ad

Please sign in to comment.