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

Commit 41d88ad

Browse files
authored
Fix user pill click (#10359)
1 parent 1e46efe commit 41d88ad

File tree

3 files changed

+210
-177
lines changed

3 files changed

+210
-177
lines changed

src/hooks/usePermalink.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export const usePermalink: (args: Args) => HookResult = ({ room, type: argType,
167167
text = member.name || resourceId;
168168
onClick = (e: ButtonEvent): void => {
169169
e.preventDefault();
170+
e.stopPropagation();
170171
dis.dispatch({
171172
action: Action.ViewUser,
172173
member: member,

test/components/views/elements/Pill-test.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
} from "../../../test-utils";
3232
import DMRoomMap from "../../../../src/utils/DMRoomMap";
3333
import { Action } from "../../../../src/dispatcher/actions";
34+
import { ButtonEvent } from "../../../../src/components/views/elements/AccessibleButton";
3435

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

4749
const renderPill = (props: PillProps): void => {
4850
const withDefault = {
4951
inMessage: true,
5052
shouldShowPillAvatar: true,
5153
...props,
5254
} as PillProps;
53-
renderResult = render(<Pill {...withDefault} />);
55+
// wrap Pill with a div to allow testing of event bubbling
56+
renderResult = render(
57+
<div onClick={pillParentClickHandler}>
58+
<Pill {...withDefault} />
59+
</div>,
60+
);
5461
};
5562

5663
filterConsole(
@@ -88,6 +95,7 @@ describe("<Pill>", () => {
8895
});
8996

9097
jest.spyOn(dis, "dispatch");
98+
pillParentClickHandler = jest.fn();
9199
});
92100

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

171-
it("should dipsatch a view user action", () => {
179+
it("should dipsatch a view user action and prevent event bubbling", () => {
172180
expect(dis.dispatch).toHaveBeenCalledWith({
173181
action: Action.ViewUser,
174182
member: room1.getMember(user1Id),
175183
});
184+
185+
expect(pillParentClickHandler).not.toHaveBeenCalled();
176186
});
177187
});
178188
});
@@ -195,7 +205,11 @@ describe("<Pill>", () => {
195205
renderPill({
196206
url: permalinkPrefix,
197207
});
198-
expect(renderResult.asFragment()).toMatchInlineSnapshot(`<DocumentFragment />`);
208+
expect(renderResult.asFragment()).toMatchInlineSnapshot(`
209+
<DocumentFragment>
210+
<div />
211+
</DocumentFragment>
212+
`);
199213
});
200214

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

0 commit comments

Comments
 (0)