2021-11-23 10:27:44 +00:00
|
|
|
/*
|
2022-01-17 17:06:30 +00:00
|
|
|
Copyright 2021 - 2022 The Matrix.org Foundation C.I.C.
|
2021-11-23 10:27:44 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
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 React from "react";
|
2023-01-16 01:25:33 +00:00
|
|
|
import { fireEvent, render, RenderResult } from "@testing-library/react";
|
2023-08-09 15:10:54 +00:00
|
|
|
import { MatrixEvent, Relations } from "matrix-js-sdk/src/matrix";
|
2021-12-08 14:56:48 +00:00
|
|
|
import {
|
2022-01-17 17:06:30 +00:00
|
|
|
M_POLL_KIND_DISCLOSED,
|
2022-02-21 10:21:35 +00:00
|
|
|
M_POLL_KIND_UNDISCLOSED,
|
2022-01-17 17:06:30 +00:00
|
|
|
M_POLL_RESPONSE,
|
|
|
|
M_POLL_START,
|
2023-01-13 17:02:33 +00:00
|
|
|
PollStartEventContent,
|
|
|
|
PollAnswer,
|
|
|
|
} from "matrix-js-sdk/src/@types/polls";
|
|
|
|
import { M_TEXT } from "matrix-js-sdk/src/@types/extensible_events";
|
2021-12-15 14:26:26 +00:00
|
|
|
|
2023-08-03 12:56:30 +00:00
|
|
|
import MPollBody, { allVotes, findTopAnswer, isPollEnded } from "../../../../src/components/views/messages/MPollBody";
|
2021-12-02 17:12:18 +00:00
|
|
|
import { IBodyProps } from "../../../../src/components/views/messages/IBodyProps";
|
2023-02-07 21:12:39 +00:00
|
|
|
import {
|
|
|
|
flushPromises,
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
makePollEndEvent,
|
|
|
|
mockClientMethodsUser,
|
|
|
|
setupRoomWithPollEvents,
|
|
|
|
} from "../../../test-utils";
|
2022-03-22 10:32:35 +00:00
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
2023-01-16 01:25:33 +00:00
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
|
|
|
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper";
|
2021-11-23 10:27:44 +00:00
|
|
|
|
2023-02-13 02:55:39 +00:00
|
|
|
const CHECKED = "mx_PollOption_checked";
|
2023-02-02 20:22:26 +00:00
|
|
|
const userId = "@me:example.com";
|
2021-11-26 09:24:34 +00:00
|
|
|
|
2022-03-22 10:32:35 +00:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
2023-02-02 20:22:26 +00:00
|
|
|
...mockClientMethodsUser(userId),
|
2022-12-12 11:24:14 +00:00
|
|
|
sendEvent: jest.fn().mockReturnValue(Promise.resolve({ event_id: "fake_send_id" })),
|
2022-03-22 10:32:35 +00:00
|
|
|
getRoom: jest.fn(),
|
2023-02-02 20:22:26 +00:00
|
|
|
decryptEventIfNeeded: jest.fn().mockResolvedValue(true),
|
|
|
|
relations: jest.fn(),
|
2022-03-22 10:32:35 +00:00
|
|
|
});
|
|
|
|
|
2021-11-23 10:27:44 +00:00
|
|
|
describe("MPollBody", () => {
|
2022-03-22 10:32:35 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
mockClient.sendEvent.mockClear();
|
2021-11-23 10:27:44 +00:00
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
mockClient.getRoom.mockReturnValue(null);
|
|
|
|
mockClient.relations.mockResolvedValue({ events: [] });
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("finds no votes if there are none", () => {
|
|
|
|
expect(allVotes(newVoteRelations([]))).toEqual([]);
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-16 22:27:03 +00:00
|
|
|
it("renders a loader while responses are still loading", async () => {
|
2023-02-02 20:22:26 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza"),
|
|
|
|
responseEvent("@bellc:example.com", "pizza"),
|
|
|
|
responseEvent("@catrd:example.com", "poutine"),
|
|
|
|
responseEvent("@dune2:example.com", "wings"),
|
|
|
|
];
|
|
|
|
// render without waiting for responses
|
|
|
|
const renderResult = await newMPollBody(votes, [], undefined, undefined, false);
|
2021-12-08 14:56:48 +00:00
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
// spinner rendered
|
2023-02-16 22:27:03 +00:00
|
|
|
expect(renderResult.getByTestId("spinner")).toBeInTheDocument();
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders no votes if none were made", async () => {
|
2023-01-16 01:25:33 +00:00
|
|
|
const votes: MatrixEvent[] = [];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("No votes cast");
|
|
|
|
expect(renderResult.getByText("What should we order for the party?")).toBeTruthy();
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("finds votes from multiple people", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
const votes = [
|
2021-12-03 10:26:28 +00:00
|
|
|
responseEvent("@me:example.com", "pizza"),
|
2021-11-23 10:27:44 +00:00
|
|
|
responseEvent("@bellc:example.com", "pizza"),
|
|
|
|
responseEvent("@catrd:example.com", "poutine"),
|
|
|
|
responseEvent("@dune2:example.com", "wings"),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("2 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("1 vote");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("1 vote");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 4 votes");
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("ignores end poll events from unauthorised users", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza"),
|
|
|
|
responseEvent("@bellc:example.com", "pizza"),
|
|
|
|
responseEvent("@catrd:example.com", "poutine"),
|
|
|
|
responseEvent("@dune2:example.com", "wings"),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@notallowed:example.com", 12)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2021-12-08 14:56:48 +00:00
|
|
|
|
|
|
|
// Even though an end event was sent, we render the poll as unfinished
|
|
|
|
// because this person is not allowed to send these events
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("2 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("1 vote");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("1 vote");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 4 votes");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("hides scores if I have not voted", async () => {
|
2021-12-03 10:26:28 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@alice:example.com", "pizza"),
|
|
|
|
responseEvent("@bellc:example.com", "pizza"),
|
|
|
|
responseEvent("@catrd:example.com", "poutine"),
|
|
|
|
responseEvent("@dune2:example.com", "wings"),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("4 votes cast. Vote to see the results");
|
2021-12-03 10:26:28 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("hides a single vote if I have not voted", async () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const votes = [responseEvent("@alice:example.com", "pizza")];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("1 vote cast. Vote to see the results");
|
2021-12-03 10:26:28 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("takes someone's most recent vote if they voted several times", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
const votes = [
|
2021-12-03 10:26:28 +00:00
|
|
|
responseEvent("@me:example.com", "pizza", 12),
|
2022-12-12 11:24:14 +00:00
|
|
|
responseEvent("@me:example.com", "wings", 20), // latest me
|
2021-11-23 10:27:44 +00:00
|
|
|
responseEvent("@qbert:example.com", "pizza", 14),
|
|
|
|
responseEvent("@qbert:example.com", "poutine", 16), // latest qbert
|
|
|
|
responseEvent("@qbert:example.com", "wings", 15),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("1 vote");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("1 vote");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 2 votes");
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("uses my local vote", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
// Given I haven't voted
|
|
|
|
const votes = [
|
|
|
|
responseEvent("@nf:example.com", "pizza", 15),
|
|
|
|
responseEvent("@fg:example.com", "pizza", 15),
|
|
|
|
responseEvent("@hi:example.com", "pizza", 15),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2021-11-23 10:27:44 +00:00
|
|
|
|
|
|
|
// When I vote for Italian
|
2023-01-16 01:25:33 +00:00
|
|
|
clickOption(renderResult, "italian");
|
2021-11-23 10:27:44 +00:00
|
|
|
|
|
|
|
// My vote is counted
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("3 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("1 vote");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("0 votes");
|
2021-11-23 10:27:44 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 4 votes");
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("overrides my other votes with my local vote", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
// Given two of us have voted for Italian
|
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza", 12),
|
|
|
|
responseEvent("@me:example.com", "poutine", 13),
|
|
|
|
responseEvent("@me:example.com", "italian", 14),
|
|
|
|
responseEvent("@nf:example.com", "italian", 15),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2021-11-23 10:27:44 +00:00
|
|
|
|
|
|
|
// When I click Wings
|
2023-01-16 01:25:33 +00:00
|
|
|
clickOption(renderResult, "wings");
|
2021-11-23 10:27:44 +00:00
|
|
|
|
|
|
|
// Then my vote is counted for Wings, and not for Italian
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("1 vote");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("1 vote");
|
2021-11-23 10:27:44 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 2 votes");
|
2021-11-26 09:24:34 +00:00
|
|
|
|
|
|
|
// And my vote is highlighted
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(voteButton(renderResult, "wings").className.includes(CHECKED)).toBe(true);
|
|
|
|
expect(voteButton(renderResult, "italian").className.includes(CHECKED)).toBe(false);
|
2021-11-26 09:24:34 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("cancels my local vote if another comes in", async () => {
|
2021-12-02 17:12:18 +00:00
|
|
|
// Given I voted locally
|
|
|
|
const votes = [responseEvent("@me:example.com", "pizza", 100)];
|
2023-01-16 01:25:33 +00:00
|
|
|
const mxEvent = new MatrixEvent({
|
|
|
|
type: M_POLL_START.name,
|
|
|
|
event_id: "$mypoll",
|
|
|
|
room_id: "#myroom:example.com",
|
|
|
|
content: newPollStart(undefined, undefined, true),
|
|
|
|
});
|
2023-02-02 20:22:26 +00:00
|
|
|
const props = getMPollBodyPropsFromEvent(mxEvent);
|
2023-02-12 20:19:45 +00:00
|
|
|
const room = await setupRoomWithPollEvents([mxEvent], votes, [], mockClient);
|
2023-01-16 01:25:33 +00:00
|
|
|
const renderResult = renderMPollBodyWithWrapper(props);
|
2023-02-02 20:22:26 +00:00
|
|
|
// wait for /relations promise to resolve
|
|
|
|
await flushPromises();
|
2023-01-16 01:25:33 +00:00
|
|
|
clickOption(renderResult, "pizza");
|
2021-12-02 17:12:18 +00:00
|
|
|
|
|
|
|
// When a new vote from me comes in
|
2023-02-02 20:22:26 +00:00
|
|
|
await room.processPollEvents([responseEvent("@me:example.com", "wings", 101)]);
|
2021-12-02 17:12:18 +00:00
|
|
|
|
|
|
|
// Then the new vote is counted, not the old one
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("1 vote");
|
2021-12-02 17:12:18 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 1 vote");
|
2021-12-02 17:12:18 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("doesn't cancel my local vote if someone else votes", async () => {
|
2021-12-02 17:12:18 +00:00
|
|
|
// Given I voted locally
|
|
|
|
const votes = [responseEvent("@me:example.com", "pizza")];
|
2023-01-16 01:25:33 +00:00
|
|
|
const mxEvent = new MatrixEvent({
|
|
|
|
type: M_POLL_START.name,
|
|
|
|
event_id: "$mypoll",
|
|
|
|
room_id: "#myroom:example.com",
|
|
|
|
content: newPollStart(undefined, undefined, true),
|
|
|
|
});
|
2023-02-02 20:22:26 +00:00
|
|
|
const props = getMPollBodyPropsFromEvent(mxEvent);
|
2023-02-12 20:19:45 +00:00
|
|
|
const room = await setupRoomWithPollEvents([mxEvent], votes, [], mockClient);
|
2023-01-16 01:25:33 +00:00
|
|
|
const renderResult = renderMPollBodyWithWrapper(props);
|
2023-02-02 20:22:26 +00:00
|
|
|
// wait for /relations promise to resolve
|
|
|
|
await flushPromises();
|
2023-01-16 01:25:33 +00:00
|
|
|
|
|
|
|
clickOption(renderResult, "pizza");
|
2021-12-02 17:12:18 +00:00
|
|
|
|
|
|
|
// When a new vote from someone else comes in
|
2023-02-02 20:22:26 +00:00
|
|
|
await room.processPollEvents([responseEvent("@xx:example.com", "wings", 101)]);
|
2021-12-02 17:12:18 +00:00
|
|
|
|
|
|
|
// Then my vote is still for pizza
|
|
|
|
// NOTE: the new event does not affect the counts for other people -
|
|
|
|
// that is handled through the Relations, not by listening to
|
|
|
|
// these timeline events.
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("1 vote");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("1 vote");
|
2021-12-02 17:12:18 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 2 votes");
|
2021-12-02 17:12:18 +00:00
|
|
|
|
|
|
|
// And my vote is highlighted
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(voteButton(renderResult, "pizza").className.includes(CHECKED)).toBe(true);
|
|
|
|
expect(voteButton(renderResult, "wings").className.includes(CHECKED)).toBe(false);
|
2021-12-02 17:12:18 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("highlights my vote even if I did it on another device", async () => {
|
2021-11-26 09:24:34 +00:00
|
|
|
// Given I voted italian
|
2022-12-12 11:24:14 +00:00
|
|
|
const votes = [responseEvent("@me:example.com", "italian"), responseEvent("@nf:example.com", "wings")];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2021-11-26 09:24:34 +00:00
|
|
|
|
|
|
|
// But I didn't click anything locally
|
|
|
|
|
|
|
|
// Then my vote is highlighted, and others are not
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(voteButton(renderResult, "italian").className.includes(CHECKED)).toBe(true);
|
|
|
|
expect(voteButton(renderResult, "wings").className.includes(CHECKED)).toBe(false);
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("ignores extra answers", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
// When cb votes for 2 things, we consider the first only
|
2022-12-12 11:24:14 +00:00
|
|
|
const votes = [responseEvent("@cb:example.com", ["pizza", "wings"]), responseEvent("@me:example.com", "wings")];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("1 vote");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("1 vote");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 2 votes");
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("allows un-voting by passing an empty vote", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@nc:example.com", "pizza", 12),
|
|
|
|
responseEvent("@nc:example.com", [], 13),
|
2021-12-03 10:26:28 +00:00
|
|
|
responseEvent("@me:example.com", "italian"),
|
2021-11-23 10:27:44 +00:00
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("1 vote");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("0 votes");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 1 vote");
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("allows re-voting after un-voting", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@op:example.com", "pizza", 12),
|
|
|
|
responseEvent("@op:example.com", [], 13),
|
|
|
|
responseEvent("@op:example.com", "italian", 14),
|
2021-12-03 10:26:28 +00:00
|
|
|
responseEvent("@me:example.com", "italian"),
|
2021-11-23 10:27:44 +00:00
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("2 votes");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("0 votes");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 2 votes");
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("treats any invalid answer as a spoiled ballot", async () => {
|
2022-01-17 17:06:30 +00:00
|
|
|
// Note that uy's second vote has a valid first answer, but
|
2021-11-23 10:27:44 +00:00
|
|
|
// the ballot is still spoiled because the second answer is
|
|
|
|
// invalid, even though we would ignore it if we continued.
|
|
|
|
const votes = [
|
2021-12-03 10:26:28 +00:00
|
|
|
responseEvent("@me:example.com", "pizza", 12),
|
|
|
|
responseEvent("@me:example.com", ["pizza", "doesntexist"], 13),
|
2021-11-23 10:27:44 +00:00
|
|
|
responseEvent("@uy:example.com", "italian", 14),
|
|
|
|
responseEvent("@uy:example.com", "doesntexist", 15),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("0 votes");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 0 votes");
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("allows re-voting after a spoiled ballot", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
const votes = [
|
2021-12-03 10:26:28 +00:00
|
|
|
responseEvent("@me:example.com", "pizza", 12),
|
|
|
|
responseEvent("@me:example.com", ["pizza", "doesntexist"], 13),
|
2021-11-23 10:27:44 +00:00
|
|
|
responseEvent("@uy:example.com", "italian", 14),
|
|
|
|
responseEvent("@uy:example.com", "doesntexist", 15),
|
|
|
|
responseEvent("@uy:example.com", "poutine", 16),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.container.querySelectorAll('input[type="radio"]')).toHaveLength(4);
|
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("1 vote");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("0 votes");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Based on 1 vote");
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders nothing if poll has no answers", async () => {
|
2023-01-16 01:25:33 +00:00
|
|
|
const answers: PollAnswer[] = [];
|
|
|
|
const votes: MatrixEvent[] = [];
|
|
|
|
const ends: MatrixEvent[] = [];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes, ends, answers);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container.childElementCount).toEqual(0);
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders the first 20 answers if 21 were given", async () => {
|
2022-01-17 17:06:30 +00:00
|
|
|
const answers = Array.from(Array(21).keys()).map((i) => {
|
2022-12-12 11:24:14 +00:00
|
|
|
return { id: `id${i}`, [M_TEXT.name]: `Name ${i}` };
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
2023-01-16 01:25:33 +00:00
|
|
|
const votes: MatrixEvent[] = [];
|
|
|
|
const ends: MatrixEvent[] = [];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes, ends, answers);
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(container.querySelectorAll(".mx_PollOption").length).toBe(20);
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("hides scores if I voted but the poll is undisclosed", async () => {
|
2022-02-21 10:21:35 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza"),
|
|
|
|
responseEvent("@alice:example.com", "pizza"),
|
|
|
|
responseEvent("@bellc:example.com", "pizza"),
|
|
|
|
responseEvent("@catrd:example.com", "poutine"),
|
|
|
|
responseEvent("@dune2:example.com", "wings"),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, [], undefined, false);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(votesCount(renderResult, "pizza")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "poutine")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "italian")).toBe("");
|
|
|
|
expect(votesCount(renderResult, "wings")).toBe("");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Results will be visible when the poll is ended");
|
2022-02-21 10:21:35 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("highlights my vote if the poll is undisclosed", async () => {
|
2022-02-21 10:21:35 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza"),
|
|
|
|
responseEvent("@alice:example.com", "poutine"),
|
|
|
|
responseEvent("@bellc:example.com", "poutine"),
|
|
|
|
responseEvent("@catrd:example.com", "poutine"),
|
|
|
|
responseEvent("@dune2:example.com", "wings"),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes, [], undefined, false);
|
2022-02-21 10:21:35 +00:00
|
|
|
|
|
|
|
// My vote is marked
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container.querySelector('input[value="pizza"]')!).toBeChecked();
|
2022-02-21 10:21:35 +00:00
|
|
|
|
|
|
|
// Sanity: other items are not checked
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container.querySelector('input[value="poutine"]')!).not.toBeChecked();
|
2022-02-21 10:21:35 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("shows scores if the poll is undisclosed but ended", async () => {
|
2022-02-21 10:21:35 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza"),
|
|
|
|
responseEvent("@alice:example.com", "pizza"),
|
|
|
|
responseEvent("@bellc:example.com", "pizza"),
|
|
|
|
responseEvent("@catrd:example.com", "poutine"),
|
|
|
|
responseEvent("@dune2:example.com", "wings"),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 12)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends, undefined, false);
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(endedVotesCount(renderResult, "pizza")).toBe('<div class="mx_PollOption_winnerIcon"></div>3 votes');
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVotesCount(renderResult, "poutine")).toBe("1 vote");
|
|
|
|
expect(endedVotesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "wings")).toBe("1 vote");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Final result based on 5 votes");
|
2022-02-21 10:21:35 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("sends a vote event when I choose an option", async () => {
|
2023-01-16 01:25:33 +00:00
|
|
|
const votes: MatrixEvent[] = [];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
clickOption(renderResult, "wings");
|
2022-03-22 10:32:35 +00:00
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(...expectedResponseEventCall("wings"));
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("sends only one vote event when I click several times", async () => {
|
2023-01-16 01:25:33 +00:00
|
|
|
const votes: MatrixEvent[] = [];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
clickOption(renderResult, "wings");
|
|
|
|
clickOption(renderResult, "wings");
|
|
|
|
clickOption(renderResult, "wings");
|
|
|
|
clickOption(renderResult, "wings");
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(...expectedResponseEventCall("wings"));
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("sends no vote event when I click what I already chose", async () => {
|
2021-12-10 12:36:53 +00:00
|
|
|
const votes = [responseEvent("@me:example.com", "wings")];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
clickOption(renderResult, "wings");
|
|
|
|
clickOption(renderResult, "wings");
|
|
|
|
clickOption(renderResult, "wings");
|
|
|
|
clickOption(renderResult, "wings");
|
2022-03-22 10:32:35 +00:00
|
|
|
expect(mockClient.sendEvent).not.toHaveBeenCalled();
|
2021-12-10 12:36:53 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("sends several events when I click different options", async () => {
|
2023-01-16 01:25:33 +00:00
|
|
|
const votes: MatrixEvent[] = [];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
clickOption(renderResult, "wings");
|
|
|
|
clickOption(renderResult, "italian");
|
|
|
|
clickOption(renderResult, "poutine");
|
2022-03-22 10:32:35 +00:00
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(3);
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(...expectedResponseEventCall("wings"));
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(...expectedResponseEventCall("italian"));
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(...expectedResponseEventCall("poutine"));
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("sends no events when I click in an ended poll", async () => {
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2022-12-12 11:24:14 +00:00
|
|
|
const votes = [responseEvent("@uy:example.com", "wings", 15), responseEvent("@uy:example.com", "poutine", 15)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2023-01-16 01:25:33 +00:00
|
|
|
clickOption(renderResult, "wings");
|
|
|
|
clickOption(renderResult, "italian");
|
|
|
|
clickOption(renderResult, "poutine");
|
2022-03-22 10:32:35 +00:00
|
|
|
expect(mockClient.sendEvent).not.toHaveBeenCalled();
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("finds the top answer among several votes", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
// 2 votes for poutine, 1 for pizza. "me" made an invalid vote.
|
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza", 12),
|
|
|
|
responseEvent("@me:example.com", ["pizza", "doesntexist"], 13),
|
|
|
|
responseEvent("@uy:example.com", "italian", 14),
|
|
|
|
responseEvent("@uy:example.com", "doesntexist", 15),
|
|
|
|
responseEvent("@uy:example.com", "poutine", 16),
|
|
|
|
responseEvent("@ab:example.com", "pizza", 17),
|
|
|
|
responseEvent("@fa:example.com", "poutine", 18),
|
|
|
|
];
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
expect(runFindTopAnswer(votes)).toEqual("Poutine");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("finds all top answers when there is a draw", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@uy:example.com", "italian", 14),
|
|
|
|
responseEvent("@ab:example.com", "pizza", 17),
|
|
|
|
responseEvent("@fa:example.com", "poutine", 18),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
expect(runFindTopAnswer(votes)).toEqual("Italian, Pizza and Poutine");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("is silent about the top answer if there are no votes", async () => {
|
|
|
|
expect(runFindTopAnswer([])).toEqual("");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("shows non-radio buttons if the poll is ended", async () => {
|
2023-02-07 21:12:39 +00:00
|
|
|
const events = [newPollEndEvent()];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody([], events);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container.querySelector(".mx_StyledRadioButton")).not.toBeInTheDocument();
|
|
|
|
expect(container.querySelector('input[type="radio"]')).not.toBeInTheDocument();
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("counts votes as normal if the poll is ended", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza", 12),
|
2022-12-12 11:24:14 +00:00
|
|
|
responseEvent("@me:example.com", "wings", 20), // latest me
|
2021-12-08 14:56:48 +00:00
|
|
|
responseEvent("@qbert:example.com", "pizza", 14),
|
|
|
|
responseEvent("@qbert:example.com", "poutine", 16), // latest qbert
|
|
|
|
responseEvent("@qbert:example.com", "wings", 15),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVotesCount(renderResult, "pizza")).toBe("0 votes");
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(endedVotesCount(renderResult, "poutine")).toBe('<div class="mx_PollOption_winnerIcon"></div>1 vote');
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVotesCount(renderResult, "italian")).toBe("0 votes");
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(endedVotesCount(renderResult, "wings")).toBe('<div class="mx_PollOption_winnerIcon"></div>1 vote');
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Final result based on 2 votes");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("counts a single vote as normal if the poll is ended", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [responseEvent("@qbert:example.com", "poutine", 16)];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVotesCount(renderResult, "pizza")).toBe("0 votes");
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(endedVotesCount(renderResult, "poutine")).toBe('<div class="mx_PollOption_winnerIcon"></div>1 vote');
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVotesCount(renderResult, "italian")).toBe("0 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "wings")).toBe("0 votes");
|
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Final result based on 1 vote");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("shows ended vote counts of different numbers", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "wings", 20),
|
|
|
|
responseEvent("@qb:example.com", "wings", 14),
|
|
|
|
responseEvent("@xy:example.com", "wings", 15),
|
|
|
|
responseEvent("@fg:example.com", "pizza", 15),
|
|
|
|
responseEvent("@hi:example.com", "pizza", 15),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2023-01-16 01:25:33 +00:00
|
|
|
|
|
|
|
expect(renderResult.container.querySelectorAll(".mx_StyledRadioButton")).toHaveLength(0);
|
|
|
|
expect(renderResult.container.querySelectorAll('input[type="radio"]')).toHaveLength(0);
|
|
|
|
expect(endedVotesCount(renderResult, "pizza")).toBe("2 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "italian")).toBe("0 votes");
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(endedVotesCount(renderResult, "wings")).toBe('<div class="mx_PollOption_winnerIcon"></div>3 votes');
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Final result based on 5 votes");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("ignores votes that arrived after poll ended", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [
|
2022-12-12 11:24:14 +00:00
|
|
|
responseEvent("@sd:example.com", "wings", 30), // Late
|
2021-12-08 14:56:48 +00:00
|
|
|
responseEvent("@ff:example.com", "wings", 20),
|
|
|
|
responseEvent("@ut:example.com", "wings", 14),
|
|
|
|
responseEvent("@iu:example.com", "wings", 15),
|
2022-12-12 11:24:14 +00:00
|
|
|
responseEvent("@jf:example.com", "wings", 35), // Late
|
2021-12-08 14:56:48 +00:00
|
|
|
responseEvent("@wf:example.com", "pizza", 15),
|
|
|
|
responseEvent("@ld:example.com", "pizza", 15),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2021-12-08 14:56:48 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVotesCount(renderResult, "pizza")).toBe("2 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "italian")).toBe("0 votes");
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(endedVotesCount(renderResult, "wings")).toBe('<div class="mx_PollOption_winnerIcon"></div>3 votes');
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Final result based on 5 votes");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("counts votes that arrived after an unauthorised poll end event", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [
|
2022-12-12 11:24:14 +00:00
|
|
|
responseEvent("@sd:example.com", "wings", 30), // Late
|
2021-12-08 14:56:48 +00:00
|
|
|
responseEvent("@ff:example.com", "wings", 20),
|
|
|
|
responseEvent("@ut:example.com", "wings", 14),
|
|
|
|
responseEvent("@iu:example.com", "wings", 15),
|
2022-12-12 11:24:14 +00:00
|
|
|
responseEvent("@jf:example.com", "wings", 35), // Late
|
2021-12-08 14:56:48 +00:00
|
|
|
responseEvent("@wf:example.com", "pizza", 15),
|
|
|
|
responseEvent("@ld:example.com", "pizza", 15),
|
|
|
|
];
|
|
|
|
const ends = [
|
2023-02-07 21:12:39 +00:00
|
|
|
newPollEndEvent("@unauthorised:example.com", 5), // Should be ignored
|
|
|
|
newPollEndEvent("@me:example.com", 25),
|
2021-12-08 14:56:48 +00:00
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2021-12-08 14:56:48 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVotesCount(renderResult, "pizza")).toBe("2 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "italian")).toBe("0 votes");
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(endedVotesCount(renderResult, "wings")).toBe('<div class="mx_PollOption_winnerIcon"></div>3 votes');
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Final result based on 5 votes");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("ignores votes that arrived after the first end poll event", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
// From MSC3381:
|
|
|
|
// "Votes sent on or before the end event's timestamp are valid votes"
|
|
|
|
|
|
|
|
const votes = [
|
2022-12-12 11:24:14 +00:00
|
|
|
responseEvent("@sd:example.com", "wings", 30), // Late
|
2021-12-08 14:56:48 +00:00
|
|
|
responseEvent("@ff:example.com", "wings", 20),
|
|
|
|
responseEvent("@ut:example.com", "wings", 14),
|
2022-12-12 11:24:14 +00:00
|
|
|
responseEvent("@iu:example.com", "wings", 25), // Just on time
|
|
|
|
responseEvent("@jf:example.com", "wings", 35), // Late
|
2021-12-08 14:56:48 +00:00
|
|
|
responseEvent("@wf:example.com", "pizza", 15),
|
|
|
|
responseEvent("@ld:example.com", "pizza", 15),
|
|
|
|
];
|
|
|
|
const ends = [
|
2023-02-07 21:12:39 +00:00
|
|
|
newPollEndEvent("@me:example.com", 65),
|
|
|
|
newPollEndEvent("@me:example.com", 25),
|
|
|
|
newPollEndEvent("@me:example.com", 75),
|
2021-12-08 14:56:48 +00:00
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2021-12-08 14:56:48 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVotesCount(renderResult, "pizza")).toBe("2 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "poutine")).toBe("0 votes");
|
|
|
|
expect(endedVotesCount(renderResult, "italian")).toBe("0 votes");
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(endedVotesCount(renderResult, "wings")).toBe('<div class="mx_PollOption_winnerIcon"></div>3 votes');
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.getByTestId("totalVotes").innerHTML).toBe("Final result based on 5 votes");
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("highlights the winning vote in an ended poll", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
// Given I voted for pizza but the winner is wings
|
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza", 20),
|
|
|
|
responseEvent("@qb:example.com", "wings", 14),
|
|
|
|
responseEvent("@xy:example.com", "wings", 15),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2021-12-08 14:56:48 +00:00
|
|
|
|
|
|
|
// Then the winner is highlighted
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVoteChecked(renderResult, "wings")).toBe(true);
|
|
|
|
expect(endedVoteChecked(renderResult, "pizza")).toBe(false);
|
2021-12-08 14:56:48 +00:00
|
|
|
|
|
|
|
// Double-check by looking for the endedOptionWinner class
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(endedVoteDiv(renderResult, "wings").className.includes("mx_PollOption_endedOptionWinner")).toBe(true);
|
|
|
|
expect(endedVoteDiv(renderResult, "pizza").className.includes("mx_PollOption_endedOptionWinner")).toBe(false);
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("highlights multiple winning votes", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@me:example.com", "pizza", 20),
|
|
|
|
responseEvent("@xy:example.com", "wings", 15),
|
|
|
|
responseEvent("@fg:example.com", "poutine", 15),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes, ends);
|
2021-12-08 14:56:48 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(endedVoteChecked(renderResult, "pizza")).toBe(true);
|
|
|
|
expect(endedVoteChecked(renderResult, "wings")).toBe(true);
|
|
|
|
expect(endedVoteChecked(renderResult, "poutine")).toBe(true);
|
|
|
|
expect(endedVoteChecked(renderResult, "italian")).toBe(false);
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(renderResult.container.getElementsByClassName(CHECKED)).toHaveLength(3);
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("highlights nothing if poll has no votes", async () => {
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody([], ends);
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(renderResult.container.getElementsByClassName(CHECKED)).toHaveLength(0);
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("says poll is not ended if there is no end event", async () => {
|
2023-01-16 01:25:33 +00:00
|
|
|
const ends: MatrixEvent[] = [];
|
2023-02-02 20:22:26 +00:00
|
|
|
const result = await runIsPollEnded(ends);
|
|
|
|
expect(result).toBe(false);
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("says poll is ended if there is an end event", async () => {
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const result = await runIsPollEnded(ends);
|
|
|
|
expect(result).toBe(true);
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("says poll is not ended if poll is fetching responses", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const pollEvent = new MatrixEvent({
|
2023-02-02 20:22:26 +00:00
|
|
|
type: M_POLL_START.name,
|
2022-12-12 11:24:14 +00:00
|
|
|
event_id: "$mypoll",
|
|
|
|
room_id: "#myroom:example.com",
|
|
|
|
content: newPollStart([]),
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
|
2023-02-12 20:19:45 +00:00
|
|
|
await setupRoomWithPollEvents([pollEvent], [], ends, mockClient);
|
2023-02-02 20:22:26 +00:00
|
|
|
const poll = mockClient.getRoom(pollEvent.getRoomId()!)!.polls.get(pollEvent.getId()!)!;
|
|
|
|
// start fetching, dont await
|
|
|
|
poll.getResponses();
|
|
|
|
expect(isPollEnded(pollEvent, mockClient)).toBe(false);
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("Displays edited content and new answer IDs if the poll has been edited", async () => {
|
2022-02-15 08:52:51 +00:00
|
|
|
const pollEvent = new MatrixEvent({
|
2022-12-12 11:24:14 +00:00
|
|
|
type: M_POLL_START.name,
|
|
|
|
event_id: "$mypoll",
|
|
|
|
room_id: "#myroom:example.com",
|
|
|
|
content: newPollStart(
|
2022-02-15 08:52:51 +00:00
|
|
|
[
|
2022-12-12 11:24:14 +00:00
|
|
|
{ id: "o1", [M_TEXT.name]: "old answer 1" },
|
|
|
|
{ id: "o2", [M_TEXT.name]: "old answer 2" },
|
2022-02-15 08:52:51 +00:00
|
|
|
],
|
|
|
|
"old question",
|
|
|
|
),
|
|
|
|
});
|
|
|
|
const replacingEvent = new MatrixEvent({
|
2022-12-12 11:24:14 +00:00
|
|
|
type: M_POLL_START.name,
|
|
|
|
event_id: "$mypollreplacement",
|
|
|
|
room_id: "#myroom:example.com",
|
|
|
|
content: {
|
2022-02-15 08:52:51 +00:00
|
|
|
"m.new_content": newPollStart(
|
|
|
|
[
|
2022-12-12 11:24:14 +00:00
|
|
|
{ id: "n1", [M_TEXT.name]: "new answer 1" },
|
|
|
|
{ id: "n2", [M_TEXT.name]: "new answer 2" },
|
|
|
|
{ id: "n3", [M_TEXT.name]: "new answer 3" },
|
2022-02-15 08:52:51 +00:00
|
|
|
],
|
|
|
|
"new question",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
pollEvent.makeReplaced(replacingEvent);
|
2023-02-02 20:22:26 +00:00
|
|
|
const { getByTestId, container } = await newMPollBodyFromEvent(pollEvent, []);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(getByTestId("pollQuestion").innerHTML).toEqual(
|
|
|
|
'new question<span class="mx_MPollBody_edited"> (edited)</span>',
|
2022-12-12 11:24:14 +00:00
|
|
|
);
|
2023-01-16 01:25:33 +00:00
|
|
|
const inputs = container.querySelectorAll('input[type="radio"]');
|
2022-02-15 08:52:51 +00:00
|
|
|
expect(inputs).toHaveLength(3);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(inputs[0].getAttribute("value")).toEqual("n1");
|
|
|
|
expect(inputs[1].getAttribute("value")).toEqual("n2");
|
|
|
|
expect(inputs[2].getAttribute("value")).toEqual("n3");
|
2023-02-13 02:55:39 +00:00
|
|
|
const options = container.querySelectorAll(".mx_PollOption_optionText");
|
2022-02-15 08:52:51 +00:00
|
|
|
expect(options).toHaveLength(3);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(options[0].innerHTML).toEqual("new answer 1");
|
|
|
|
expect(options[1].innerHTML).toEqual("new answer 2");
|
|
|
|
expect(options[2].innerHTML).toEqual("new answer 3");
|
2022-02-15 08:52:51 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders a poll with no votes", async () => {
|
2023-01-16 01:25:33 +00:00
|
|
|
const votes: MatrixEvent[] = [];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container).toMatchSnapshot();
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders a poll with only non-local votes", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@op:example.com", "pizza", 12),
|
|
|
|
responseEvent("@op:example.com", [], 13),
|
|
|
|
responseEvent("@op:example.com", "italian", 14),
|
2021-12-03 10:26:28 +00:00
|
|
|
responseEvent("@me:example.com", "wings", 15),
|
2021-11-23 10:27:44 +00:00
|
|
|
responseEvent("@qr:example.com", "italian", 16),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container).toMatchSnapshot();
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2023-02-22 10:51:44 +00:00
|
|
|
it("renders a warning message when poll has undecryptable relations", async () => {
|
|
|
|
const votes = [
|
|
|
|
responseEvent("@op:example.com", "pizza", 12),
|
|
|
|
responseEvent("@op:example.com", [], 13),
|
|
|
|
responseEvent("@op:example.com", "italian", 14),
|
|
|
|
responseEvent("@me:example.com", "wings", 15),
|
|
|
|
responseEvent("@qr:example.com", "italian", 16),
|
|
|
|
];
|
|
|
|
|
|
|
|
jest.spyOn(votes[1], "isDecryptionFailure").mockReturnValue(true);
|
|
|
|
const { getByText } = await newMPollBody(votes);
|
|
|
|
expect(getByText("Due to decryption errors, some votes may not be counted")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders a poll with local, non-local and invalid votes", async () => {
|
2021-11-23 10:27:44 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@a:example.com", "pizza", 12),
|
|
|
|
responseEvent("@b:example.com", [], 13),
|
|
|
|
responseEvent("@c:example.com", "italian", 14),
|
|
|
|
responseEvent("@d:example.com", "italian", 14),
|
|
|
|
responseEvent("@e:example.com", "wings", 15),
|
|
|
|
responseEvent("@me:example.com", "italian", 16),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const renderResult = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
clickOption(renderResult, "italian");
|
2023-02-02 20:22:26 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
2021-12-03 10:26:28 +00:00
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders a poll that I have not voted in", async () => {
|
2021-12-03 10:26:28 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@op:example.com", "pizza", 12),
|
|
|
|
responseEvent("@op:example.com", [], 13),
|
|
|
|
responseEvent("@op:example.com", "italian", 14),
|
|
|
|
responseEvent("@yo:example.com", "wings", 15),
|
|
|
|
responseEvent("@qr:example.com", "italian", 16),
|
|
|
|
];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container).toMatchSnapshot();
|
2021-12-03 10:26:28 +00:00
|
|
|
});
|
2021-12-08 14:56:48 +00:00
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders a finished poll with no votes", async () => {
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody([], ends);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container).toMatchSnapshot();
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders a finished poll", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@op:example.com", "pizza", 12),
|
|
|
|
responseEvent("@op:example.com", [], 13),
|
|
|
|
responseEvent("@op:example.com", "italian", 14),
|
|
|
|
responseEvent("@yo:example.com", "wings", 15),
|
|
|
|
responseEvent("@qr:example.com", "italian", 16),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes, ends);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container).toMatchSnapshot();
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders a finished poll with multiple winners", async () => {
|
2021-12-08 14:56:48 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@ed:example.com", "pizza", 12),
|
|
|
|
responseEvent("@rf:example.com", "pizza", 12),
|
|
|
|
responseEvent("@th:example.com", "wings", 13),
|
|
|
|
responseEvent("@yh:example.com", "wings", 14),
|
|
|
|
responseEvent("@th:example.com", "poutine", 13),
|
|
|
|
responseEvent("@yh:example.com", "poutine", 14),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes, ends);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container).toMatchSnapshot();
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
2022-02-21 10:21:35 +00:00
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders an undisclosed, unfinished poll", async () => {
|
2022-02-21 10:21:35 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@ed:example.com", "pizza", 12),
|
|
|
|
responseEvent("@rf:example.com", "pizza", 12),
|
|
|
|
responseEvent("@th:example.com", "wings", 13),
|
|
|
|
responseEvent("@yh:example.com", "wings", 14),
|
|
|
|
responseEvent("@th:example.com", "poutine", 13),
|
|
|
|
responseEvent("@yh:example.com", "poutine", 14),
|
|
|
|
];
|
2023-01-16 01:25:33 +00:00
|
|
|
const ends: MatrixEvent[] = [];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes, ends, undefined, false);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container).toMatchSnapshot();
|
2022-02-21 10:21:35 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
it("renders an undisclosed, finished poll", async () => {
|
2022-02-21 10:21:35 +00:00
|
|
|
const votes = [
|
|
|
|
responseEvent("@ed:example.com", "pizza", 12),
|
|
|
|
responseEvent("@rf:example.com", "pizza", 12),
|
|
|
|
responseEvent("@th:example.com", "wings", 13),
|
|
|
|
responseEvent("@yh:example.com", "wings", 14),
|
|
|
|
responseEvent("@th:example.com", "poutine", 13),
|
|
|
|
responseEvent("@yh:example.com", "poutine", 14),
|
|
|
|
];
|
2023-02-07 21:12:39 +00:00
|
|
|
const ends = [newPollEndEvent("@me:example.com", 25)];
|
2023-02-02 20:22:26 +00:00
|
|
|
const { container } = await newMPollBody(votes, ends, undefined, false);
|
2023-01-16 01:25:33 +00:00
|
|
|
expect(container).toMatchSnapshot();
|
2022-02-21 10:21:35 +00:00
|
|
|
});
|
2021-11-23 10:27:44 +00:00
|
|
|
});
|
|
|
|
|
2021-12-08 14:56:48 +00:00
|
|
|
function newVoteRelations(relationEvents: Array<MatrixEvent>): Relations {
|
2023-02-02 20:22:26 +00:00
|
|
|
return newRelations(relationEvents, M_POLL_RESPONSE.name, [M_POLL_RESPONSE.altName!]);
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
function newRelations(relationEvents: Array<MatrixEvent>, eventType: string, altEventTypes?: string[]): Relations {
|
|
|
|
const voteRelations = new Relations("m.reference", eventType, mockClient, altEventTypes);
|
2021-11-23 10:27:44 +00:00
|
|
|
for (const ev of relationEvents) {
|
2021-12-08 14:56:48 +00:00
|
|
|
voteRelations.addEvent(ev);
|
2021-11-23 10:27:44 +00:00
|
|
|
}
|
2021-12-08 14:56:48 +00:00
|
|
|
return voteRelations;
|
2021-11-23 10:27:44 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
async function newMPollBody(
|
2021-11-23 10:27:44 +00:00
|
|
|
relationEvents: Array<MatrixEvent>,
|
2021-12-08 14:56:48 +00:00
|
|
|
endEvents: Array<MatrixEvent> = [],
|
2023-01-13 17:02:33 +00:00
|
|
|
answers?: PollAnswer[],
|
2022-02-21 10:21:35 +00:00
|
|
|
disclosed = true,
|
2023-02-02 20:22:26 +00:00
|
|
|
waitForResponsesLoad = true,
|
|
|
|
): Promise<RenderResult> {
|
2022-02-15 08:52:51 +00:00
|
|
|
const mxEvent = new MatrixEvent({
|
2022-12-12 11:24:14 +00:00
|
|
|
type: M_POLL_START.name,
|
|
|
|
event_id: "$mypoll",
|
|
|
|
room_id: "#myroom:example.com",
|
2023-01-16 01:25:33 +00:00
|
|
|
content: newPollStart(answers, undefined, disclosed),
|
2022-02-15 08:52:51 +00:00
|
|
|
});
|
2023-02-02 20:22:26 +00:00
|
|
|
const result = newMPollBodyFromEvent(mxEvent, relationEvents, endEvents);
|
|
|
|
// flush promises from loading relations
|
|
|
|
if (waitForResponsesLoad) {
|
|
|
|
await flushPromises();
|
|
|
|
}
|
|
|
|
return result;
|
2022-02-15 08:52:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
function getMPollBodyPropsFromEvent(mxEvent: MatrixEvent): IBodyProps {
|
2023-01-16 01:25:33 +00:00
|
|
|
return {
|
|
|
|
mxEvent,
|
|
|
|
// We don't use any of these props, but they're required.
|
|
|
|
highlightLink: "unused",
|
|
|
|
highlights: [],
|
|
|
|
mediaEventHelper: {} as unknown as MediaEventHelper,
|
|
|
|
onHeightChanged: () => {},
|
|
|
|
onMessageAllowed: () => {},
|
|
|
|
permalinkCreator: {} as unknown as RoomPermalinkCreator,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderMPollBodyWithWrapper(props: IBodyProps): RenderResult {
|
|
|
|
return render(<MPollBody {...props} />, {
|
|
|
|
wrapper: ({ children }) => (
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>{children}</MatrixClientContext.Provider>
|
|
|
|
),
|
|
|
|
});
|
2021-11-23 10:27:44 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
async function newMPollBodyFromEvent(
|
2023-01-16 01:25:33 +00:00
|
|
|
mxEvent: MatrixEvent,
|
|
|
|
relationEvents: Array<MatrixEvent>,
|
|
|
|
endEvents: Array<MatrixEvent> = [],
|
2023-02-02 20:22:26 +00:00
|
|
|
): Promise<RenderResult> {
|
|
|
|
const props = getMPollBodyPropsFromEvent(mxEvent);
|
|
|
|
|
2023-02-12 20:19:45 +00:00
|
|
|
await setupRoomWithPollEvents([mxEvent], relationEvents, endEvents, mockClient);
|
2023-02-02 20:22:26 +00:00
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
return renderMPollBodyWithWrapper(props);
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
function clickOption({ getByTestId }: RenderResult, value: string) {
|
|
|
|
fireEvent.click(getByTestId(`pollOption-${value}`));
|
2021-11-23 10:27:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
function voteButton({ getByTestId }: RenderResult, value: string): Element {
|
|
|
|
return getByTestId(`pollOption-${value}`);
|
2021-11-26 09:24:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
function votesCount({ getByTestId }: RenderResult, value: string): string {
|
2023-02-13 02:55:39 +00:00
|
|
|
return getByTestId(`pollOption-${value}`).querySelector(".mx_PollOption_optionVoteCount")!.innerHTML;
|
2021-11-23 10:27:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
function endedVoteChecked({ getByTestId }: RenderResult, value: string): boolean {
|
2023-02-13 02:55:39 +00:00
|
|
|
return getByTestId(`pollOption-${value}`).className.includes(CHECKED);
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
function endedVoteDiv({ getByTestId }: RenderResult, value: string): Element {
|
|
|
|
return getByTestId(`pollOption-${value}`).firstElementChild!;
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 01:25:33 +00:00
|
|
|
function endedVotesCount(renderResult: RenderResult, value: string): string {
|
|
|
|
return votesCount(renderResult, value);
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 15:23:35 +00:00
|
|
|
function newPollStart(answers?: PollAnswer[], question?: string, disclosed = true): PollStartEventContent {
|
2021-11-23 10:27:44 +00:00
|
|
|
if (!answers) {
|
|
|
|
answers = [
|
2022-12-12 11:24:14 +00:00
|
|
|
{ id: "pizza", [M_TEXT.name]: "Pizza" },
|
|
|
|
{ id: "poutine", [M_TEXT.name]: "Poutine" },
|
|
|
|
{ id: "italian", [M_TEXT.name]: "Italian" },
|
|
|
|
{ id: "wings", [M_TEXT.name]: "Wings" },
|
2021-11-23 10:27:44 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-02-15 08:52:51 +00:00
|
|
|
if (!question) {
|
|
|
|
question = "What should we order for the party?";
|
|
|
|
}
|
|
|
|
|
2023-01-13 17:02:33 +00:00
|
|
|
const answersFallback = answers.map((a, i) => `${i + 1}. ${M_TEXT.findIn<string>(a)}`).join("\n");
|
2022-02-15 08:52:51 +00:00
|
|
|
|
|
|
|
const fallback = `${question}\n${answersFallback}`;
|
|
|
|
|
2021-11-23 10:27:44 +00:00
|
|
|
return {
|
2022-01-17 17:06:30 +00:00
|
|
|
[M_POLL_START.name]: {
|
2022-12-12 11:24:14 +00:00
|
|
|
question: {
|
2022-02-15 08:52:51 +00:00
|
|
|
[M_TEXT.name]: question,
|
2021-11-23 10:27:44 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
kind: disclosed ? M_POLL_KIND_DISCLOSED.name : M_POLL_KIND_UNDISCLOSED.name,
|
|
|
|
answers: answers,
|
2021-11-23 10:27:44 +00:00
|
|
|
},
|
2022-02-15 08:52:51 +00:00
|
|
|
[M_TEXT.name]: fallback,
|
2021-11-23 10:27:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function responseEvent(
|
|
|
|
sender = "@alice:example.com",
|
|
|
|
answers: string | Array<string> = "italian",
|
|
|
|
ts = 0,
|
|
|
|
): MatrixEvent {
|
|
|
|
const ans = typeof answers === "string" ? [answers] : answers;
|
2022-12-12 11:24:14 +00:00
|
|
|
return new MatrixEvent({
|
|
|
|
event_id: nextId(),
|
|
|
|
room_id: "#myroom:example.com",
|
|
|
|
origin_server_ts: ts,
|
|
|
|
type: M_POLL_RESPONSE.name,
|
|
|
|
sender: sender,
|
|
|
|
content: {
|
|
|
|
"m.relates_to": {
|
|
|
|
rel_type: "m.reference",
|
|
|
|
event_id: "$mypoll",
|
|
|
|
},
|
|
|
|
[M_POLL_RESPONSE.name]: {
|
|
|
|
answers: ans,
|
2021-11-23 10:27:44 +00:00
|
|
|
},
|
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2021-11-23 10:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function expectedResponseEvent(answer: string) {
|
|
|
|
return {
|
2022-12-12 11:24:14 +00:00
|
|
|
content: {
|
2022-01-17 17:06:30 +00:00
|
|
|
[M_POLL_RESPONSE.name]: {
|
2022-12-12 11:24:14 +00:00
|
|
|
answers: [answer],
|
2021-11-23 10:27:44 +00:00
|
|
|
},
|
|
|
|
"m.relates_to": {
|
2022-12-12 11:24:14 +00:00
|
|
|
event_id: "$mypoll",
|
|
|
|
rel_type: "m.reference",
|
2021-11-23 10:27:44 +00:00
|
|
|
},
|
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
roomId: "#myroom:example.com",
|
|
|
|
eventType: M_POLL_RESPONSE.name,
|
2023-02-02 20:22:26 +00:00
|
|
|
txnId: "$123",
|
2021-11-23 10:27:44 +00:00
|
|
|
};
|
|
|
|
}
|
2022-03-22 10:32:35 +00:00
|
|
|
function expectedResponseEventCall(answer: string) {
|
2022-12-12 11:24:14 +00:00
|
|
|
const { content, roomId, eventType } = expectedResponseEvent(answer);
|
|
|
|
return [roomId, eventType, content];
|
2022-03-22 10:32:35 +00:00
|
|
|
}
|
2021-11-23 10:27:44 +00:00
|
|
|
|
2023-03-01 15:23:35 +00:00
|
|
|
function newPollEndEvent(sender = "@me:example.com", ts = 0): MatrixEvent {
|
2023-02-07 21:12:39 +00:00
|
|
|
return makePollEndEvent("$mypoll", "#myroom:example.com", sender, ts);
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
async function runIsPollEnded(ends: MatrixEvent[]) {
|
2021-12-08 14:56:48 +00:00
|
|
|
const pollEvent = new MatrixEvent({
|
2022-12-12 11:24:14 +00:00
|
|
|
event_id: "$mypoll",
|
|
|
|
room_id: "#myroom:example.com",
|
|
|
|
type: M_POLL_START.name,
|
|
|
|
content: newPollStart(),
|
2021-12-08 14:56:48 +00:00
|
|
|
});
|
|
|
|
|
2023-02-12 20:19:45 +00:00
|
|
|
await setupRoomWithPollEvents([pollEvent], [], ends, mockClient);
|
2021-12-08 14:56:48 +00:00
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
return isPollEnded(pollEvent, mockClient);
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
function runFindTopAnswer(votes: MatrixEvent[]) {
|
2021-12-08 14:56:48 +00:00
|
|
|
const pollEvent = new MatrixEvent({
|
2022-12-12 11:24:14 +00:00
|
|
|
event_id: "$mypoll",
|
|
|
|
room_id: "#myroom:example.com",
|
|
|
|
type: M_POLL_START.name,
|
|
|
|
content: newPollStart(),
|
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
return findTopAnswer(pollEvent, newVoteRelations(votes));
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 10:27:44 +00:00
|
|
|
let EVENT_ID = 0;
|
|
|
|
function nextId(): string {
|
|
|
|
EVENT_ID++;
|
|
|
|
return EVENT_ID.toString();
|
|
|
|
}
|