Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/components/views/polls/pollHistory/PollListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,34 @@ interface Props {

export const PollListItem: React.FC<Props> = ({ event, onClick }) => {
const pollEvent = event.unstableExtensibleEvent as unknown as PollStartEvent;
const [showTooltip, setShowTooltip] = React.useState(false);

if (!pollEvent) {
return null;
}
const formattedDate = formatLocalDateShort(event.getTs());

return (
<li data-testid={`pollListItem-${event.getId()!}`} className="mx_PollListItem" onClick={onClick}>
<Tooltip label={_t("right_panel|poll|view_poll")} placement="top" isTriggerInteractive={false}>
<div className="mx_PollListItem_content">
<span>{formattedDate}</span>
<PollIcon className="mx_PollListItem_icon" />
<li
data-testid={`pollListItem-${event.getId()!}`}
className="mx_PollListItem"
onClick={onClick}
onMouseEnter={() => setShowTooltip(true)}
onMouseLeave={() => setShowTooltip(false)}
aria-label={`${formattedDate} ${pollEvent.question.text}`}
>
<div className="mx_PollListItem_content">
<span>{formattedDate}</span>
<PollIcon className="mx_PollListItem_icon" />
<Tooltip
label={_t("right_panel|poll|view_poll")}
placement="top"
isTriggerInteractive={false}
open={showTooltip}
>
<span className="mx_PollListItem_question">{pollEvent.question.text}</span>
</div>
</Tooltip>
</Tooltip>
</div>
</li>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ Please see LICENSE files in the repository root for full details.
*/

import React from "react";
import { fireEvent, render } from "jest-matrix-react";
import { fireEvent, render, waitFor } from "jest-matrix-react";
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
import userEvent from "@testing-library/user-event";

import { PollListItem } from "../../../../../../src/components/views/polls/pollHistory/PollListItem";
import { makePollStartEvent, mockIntlDateTimeFormat, unmockIntlDateTimeFormat } from "../../../../../test-utils";
Expand Down Expand Up @@ -50,4 +51,20 @@ describe("<PollListItem />", () => {

expect(onClick).toHaveBeenCalled();
});

it("shows and hides tooltip on hover", async () => {
const { getByRole, getByText } = getComponent();
const listItem = getByRole("listitem", { name: "01/01/70 Question?" });

await userEvent.hover(listItem);

// Wait for the tooltip to open
const questionSpan = getByText("Question?");
const tooltip = await waitFor(() => {
const tooltip = document.getElementById(questionSpan.getAttribute("aria-labelledby")!);
expect(tooltip).toBeVisible();
return tooltip;
});
expect(tooltip).toHaveTextContent("View poll");
});
Comment on lines +59 to +69
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested and, annoyingly, it looks like the test you copied from doesn't actually work, in that the assertion that the tooltip is visible passes even without hovering. I think this is because floating-ui hides the tooltip by positioning it offscreen or something. This means this is actually quite difficult to test. Writing a playwright test might be an option, or possibly mocking the tooltip component to remove that from the equation and just assert that it's been passed open=true. Possibly you could test manually for the tooltip position or classes, although this would be quite fragile. Sorry, this seems to be the classic case of pulling one tiny thread and a bunch of stuff unravelling.

});
Original file line number Diff line number Diff line change
Expand Up @@ -87,51 +87,51 @@ exports[`<PollHistory /> renders a list of active polls when there are polls in
class="mx_PollListItem"
data-testid="pollListItem-$2"
>
<span
tabindex="0"
<div
class="mx_PollListItem_content"
>
<span>
02/02/23
</span>
<div
aria-labelledby="_r_a_"
class="mx_PollListItem_content"
class="mx_PollListItem_icon"
/>
<span
tabindex="0"
>
<span>
02/02/23
</span>
<div
class="mx_PollListItem_icon"
/>
<span
aria-labelledby="_r_a_"
class="mx_PollListItem_question"
>
Where?
</span>
</div>
</span>
</span>
</div>
</li>
<li
class="mx_PollListItem"
data-testid="pollListItem-$1"
>
<span
tabindex="0"
<div
class="mx_PollListItem_content"
>
<span>
02/02/23
</span>
<div
aria-labelledby="_r_f_"
class="mx_PollListItem_content"
class="mx_PollListItem_icon"
/>
<span
tabindex="0"
>
<span>
02/02/23
</span>
<div
class="mx_PollListItem_icon"
/>
<span
aria-labelledby="_r_f_"
class="mx_PollListItem_question"
>
Question?
</span>
</div>
</span>
</span>
</div>
</li>
</ol>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@
exports[`<PollListItem /> renders a poll 1`] = `
<div>
<li
aria-label="01/01/70 Question?"
class="mx_PollListItem"
data-testid="pollListItem-$mypoll"
>
<span
tabindex="0"
<div
class="mx_PollListItem_content"
>
<span>
01/01/70
</span>
<div
aria-labelledby="_r_0_"
class="mx_PollListItem_content"
class="mx_PollListItem_icon"
/>
<span
tabindex="0"
>
<span>
01/01/70
</span>
<div
class="mx_PollListItem_icon"
/>
<span
aria-labelledby="_r_0_"
class="mx_PollListItem_question"
>
Question?
</span>
</div>
</span>
</span>
</div>
</li>
</div>
`;
Loading