2021-11-24 14:48:07 +01:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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-02-28 09:58:23 +01:00
|
|
|
import { fireEvent, render, RenderResult } from "@testing-library/react";
|
2023-08-23 11:04:25 +02:00
|
|
|
import {
|
|
|
|
Room,
|
|
|
|
MatrixEvent,
|
|
|
|
M_POLL_KIND_DISCLOSED,
|
|
|
|
M_POLL_KIND_UNDISCLOSED,
|
|
|
|
M_POLL_START,
|
|
|
|
M_TEXT,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2023-01-13 18:02:33 +01:00
|
|
|
import { PollStartEvent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent";
|
2024-03-25 13:48:48 +01:00
|
|
|
import { ReplacementEvent } from "matrix-js-sdk/src/types";
|
2021-12-09 10:10:23 +01:00
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
import { getMockClientWithEventEmitter } from "../../../test-utils";
|
2021-11-24 14:48:07 +01:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-03-22 11:32:35 +01:00
|
|
|
import PollCreateDialog from "../../../../src/components/views/elements/PollCreateDialog";
|
2022-12-12 12:24:14 +01:00
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
2021-11-24 14:48:07 +01:00
|
|
|
|
|
|
|
// Fake date to give a predictable snapshot
|
|
|
|
const realDateNow = Date.now;
|
|
|
|
const realDateToISOString = Date.prototype.toISOString;
|
|
|
|
Date.now = jest.fn(() => 2345678901234);
|
|
|
|
// eslint-disable-next-line no-extend-native
|
|
|
|
Date.prototype.toISOString = jest.fn(() => "2021-11-23T14:35:14.240Z");
|
2022-02-18 17:35:08 +01:00
|
|
|
|
2021-11-24 14:48:07 +01:00
|
|
|
afterAll(() => {
|
|
|
|
Date.now = realDateNow;
|
|
|
|
// eslint-disable-next-line no-extend-native
|
|
|
|
Date.prototype.toISOString = realDateToISOString;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("PollCreateDialog", () => {
|
2022-03-22 11:32:35 +01:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
2022-12-12 12:24:14 +01:00
|
|
|
sendEvent: jest.fn().mockResolvedValue({ event_id: "1" }),
|
2022-03-22 11:32:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockClient.sendEvent.mockClear();
|
|
|
|
});
|
|
|
|
|
2021-11-24 17:24:19 +01:00
|
|
|
it("renders a blank poll", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
expect(dialog.asFragment()).toMatchSnapshot();
|
2021-11-24 17:24:19 +01:00
|
|
|
});
|
|
|
|
|
2022-02-18 17:35:08 +01:00
|
|
|
it("autofocuses the poll topic on mount", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
|
|
|
expect(dialog.container.querySelector("#poll-topic-input")).toHaveFocus();
|
2022-02-18 17:35:08 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("autofocuses the new poll option field after clicking add option button", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
|
|
|
expect(dialog.container.querySelector("#poll-topic-input")).toHaveFocus();
|
2022-02-18 17:35:08 +01:00
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
fireEvent.click(dialog.container.querySelector("div.mx_PollCreateDialog_addOption")!);
|
2022-02-18 17:35:08 +01:00
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(dialog.container.querySelector("#poll-topic-input")).not.toHaveFocus();
|
|
|
|
expect(dialog.container.querySelector("#pollcreate_option_1")).not.toHaveFocus();
|
|
|
|
expect(dialog.container.querySelector("#pollcreate_option_2")).toHaveFocus();
|
2022-02-18 17:35:08 +01:00
|
|
|
});
|
|
|
|
|
2021-11-24 17:24:19 +01:00
|
|
|
it("renders a question and some options", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
|
|
|
expectSubmitToBeDisabled(dialog, true);
|
2021-11-24 17:24:19 +01:00
|
|
|
|
|
|
|
// When I set some values in the boxes
|
2022-12-12 12:24:14 +01:00
|
|
|
changeValue(dialog, "Question or topic", "How many turnips is the optimal number?");
|
2021-11-24 17:24:19 +01:00
|
|
|
changeValue(dialog, "Option 1", "As many as my neighbour");
|
|
|
|
changeValue(dialog, "Option 2", "The question is meaningless");
|
2023-02-28 09:58:23 +01:00
|
|
|
fireEvent.click(dialog.container.querySelector("div.mx_PollCreateDialog_addOption")!);
|
2021-11-24 17:24:19 +01:00
|
|
|
changeValue(dialog, "Option 3", "Mu");
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(dialog.asFragment()).toMatchSnapshot();
|
2021-11-24 17:24:19 +01:00
|
|
|
});
|
|
|
|
|
2022-02-17 10:13:05 +01:00
|
|
|
it("renders info from a previous event", () => {
|
|
|
|
const previousEvent: MatrixEvent = new MatrixEvent(
|
2022-12-12 12:24:14 +01:00
|
|
|
PollStartEvent.from("Poll Q", ["Answer 1", "Answer 2"], M_POLL_KIND_DISCLOSED).serialize(),
|
2022-02-17 10:13:05 +01:00
|
|
|
);
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(
|
2022-12-12 12:24:14 +01:00
|
|
|
<PollCreateDialog room={createRoom()} onFinished={jest.fn()} editingMxEvent={previousEvent} />,
|
2022-02-17 10:13:05 +01:00
|
|
|
);
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
expectSubmitToBeDisabled(dialog, false);
|
|
|
|
expect(dialog.asFragment()).toMatchSnapshot();
|
2022-02-17 10:13:05 +01:00
|
|
|
});
|
|
|
|
|
2021-11-24 14:48:07 +01:00
|
|
|
it("doesn't allow submitting until there are options", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
|
|
|
expectSubmitToBeDisabled(dialog, true);
|
2021-11-24 14:48:07 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does allow submitting when there are options and a question", () => {
|
|
|
|
// Given a dialog with no info in (which I am unable to submit)
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
|
|
|
expectSubmitToBeDisabled(dialog, true);
|
2021-11-24 14:48:07 +01:00
|
|
|
|
|
|
|
// When I set some values in the boxes
|
|
|
|
changeValue(dialog, "Question or topic", "Q");
|
|
|
|
changeValue(dialog, "Option 1", "A1");
|
|
|
|
changeValue(dialog, "Option 2", "A2");
|
|
|
|
|
|
|
|
// Then I am able to submit
|
2023-02-28 09:58:23 +01:00
|
|
|
expectSubmitToBeDisabled(dialog, false);
|
2021-11-24 14:48:07 +01:00
|
|
|
});
|
|
|
|
|
2022-02-21 11:21:35 +01:00
|
|
|
it("shows the open poll description at first", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
|
|
|
expect(dialog.container.querySelector("select")).toHaveValue(M_POLL_KIND_DISCLOSED.name);
|
|
|
|
expect(dialog.container.querySelector("p")).toHaveTextContent("Voters see results as soon as they have voted");
|
2022-02-21 11:21:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shows the closed poll description if we choose it", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
2022-02-21 11:21:35 +01:00
|
|
|
changeKind(dialog, M_POLL_KIND_UNDISCLOSED.name);
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(dialog.container.querySelector("select")).toHaveValue(M_POLL_KIND_UNDISCLOSED.name);
|
|
|
|
expect(dialog.container.querySelector("p")).toHaveTextContent(
|
|
|
|
"Results are only revealed when you end the poll",
|
|
|
|
);
|
2022-02-21 11:21:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shows the open poll description if we choose it", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
2022-02-21 11:21:35 +01:00
|
|
|
changeKind(dialog, M_POLL_KIND_UNDISCLOSED.name);
|
|
|
|
changeKind(dialog, M_POLL_KIND_DISCLOSED.name);
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(dialog.container.querySelector("select")).toHaveValue(M_POLL_KIND_DISCLOSED.name);
|
|
|
|
expect(dialog.container.querySelector("p")).toHaveTextContent("Voters see results as soon as they have voted");
|
2022-02-21 11:21:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shows the closed poll description when editing a closed poll", () => {
|
|
|
|
const previousEvent: MatrixEvent = new MatrixEvent(
|
2022-12-12 12:24:14 +01:00
|
|
|
PollStartEvent.from("Poll Q", ["Answer 1", "Answer 2"], M_POLL_KIND_UNDISCLOSED).serialize(),
|
2022-02-21 11:21:35 +01:00
|
|
|
);
|
|
|
|
previousEvent.event.event_id = "$prevEventId";
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(
|
2022-12-12 12:24:14 +01:00
|
|
|
<PollCreateDialog room={createRoom()} onFinished={jest.fn()} editingMxEvent={previousEvent} />,
|
2022-02-21 11:21:35 +01:00
|
|
|
);
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(dialog.container.querySelector("select")).toHaveValue(M_POLL_KIND_UNDISCLOSED.name);
|
|
|
|
expect(dialog.container.querySelector("p")).toHaveTextContent(
|
|
|
|
"Results are only revealed when you end the poll",
|
|
|
|
);
|
2022-02-21 11:21:35 +01:00
|
|
|
});
|
|
|
|
|
2021-11-24 17:24:19 +01:00
|
|
|
it("displays a spinner after submitting", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
2021-11-24 17:24:19 +01:00
|
|
|
changeValue(dialog, "Question or topic", "Q");
|
|
|
|
changeValue(dialog, "Option 1", "A1");
|
|
|
|
changeValue(dialog, "Option 2", "A2");
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(dialog.container.querySelector(".mx_Spinner")).toBeFalsy();
|
2021-11-24 14:48:07 +01:00
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
fireEvent.click(dialog.container.querySelector("button")!);
|
|
|
|
expect(dialog.container.querySelector(".mx_Spinner")).toBeDefined();
|
2021-11-24 14:48:07 +01:00
|
|
|
});
|
2022-02-17 10:13:05 +01:00
|
|
|
|
|
|
|
it("sends a poll create event when submitted", () => {
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />);
|
2022-02-17 10:13:05 +01:00
|
|
|
changeValue(dialog, "Question or topic", "Q");
|
|
|
|
changeValue(dialog, "Option 1", "A1");
|
|
|
|
changeValue(dialog, "Option 2", "A2");
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
fireEvent.click(dialog.container.querySelector("button")!);
|
2022-03-22 11:32:35 +01:00
|
|
|
const [, , eventType, sentEventContent] = mockClient.sendEvent.mock.calls[0];
|
|
|
|
expect(M_POLL_START.matches(eventType)).toBeTruthy();
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(sentEventContent).toEqual({
|
|
|
|
[M_TEXT.name]: "Q\n1. A1\n2. A2",
|
|
|
|
[M_POLL_START.name]: {
|
|
|
|
answers: [
|
|
|
|
{
|
|
|
|
id: expect.any(String),
|
|
|
|
[M_TEXT.name]: "A1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: expect.any(String),
|
|
|
|
[M_TEXT.name]: "A2",
|
2022-02-17 10:13:05 +01:00
|
|
|
},
|
2022-12-12 12:24:14 +01:00
|
|
|
],
|
|
|
|
kind: M_POLL_KIND_DISCLOSED.name,
|
|
|
|
max_selections: 1,
|
|
|
|
question: {
|
|
|
|
body: "Q",
|
|
|
|
format: undefined,
|
|
|
|
formatted_body: undefined,
|
|
|
|
msgtype: "m.text",
|
|
|
|
[M_TEXT.name]: "Q",
|
2022-02-17 10:13:05 +01:00
|
|
|
},
|
|
|
|
},
|
2022-12-12 12:24:14 +01:00
|
|
|
});
|
2022-02-17 10:13:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("sends a poll edit event when editing", () => {
|
|
|
|
const previousEvent: MatrixEvent = new MatrixEvent(
|
2022-12-12 12:24:14 +01:00
|
|
|
PollStartEvent.from("Poll Q", ["Answer 1", "Answer 2"], M_POLL_KIND_DISCLOSED).serialize(),
|
2022-02-17 10:13:05 +01:00
|
|
|
);
|
|
|
|
previousEvent.event.event_id = "$prevEventId";
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(
|
2022-12-12 12:24:14 +01:00
|
|
|
<PollCreateDialog room={createRoom()} onFinished={jest.fn()} editingMxEvent={previousEvent} />,
|
2022-02-17 10:13:05 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
changeValue(dialog, "Question or topic", "Poll Q updated");
|
|
|
|
changeValue(dialog, "Option 2", "Answer 2 updated");
|
2022-02-21 11:21:35 +01:00
|
|
|
changeKind(dialog, M_POLL_KIND_UNDISCLOSED.name);
|
2023-02-28 09:58:23 +01:00
|
|
|
fireEvent.click(dialog.container.querySelector("button")!);
|
2022-02-17 10:13:05 +01:00
|
|
|
|
2022-03-22 11:32:35 +01:00
|
|
|
const [, , eventType, sentEventContent] = mockClient.sendEvent.mock.calls[0];
|
|
|
|
expect(M_POLL_START.matches(eventType)).toBeTruthy();
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(sentEventContent).toEqual({
|
|
|
|
"m.new_content": {
|
|
|
|
[M_TEXT.name]: "Poll Q updated\n1. Answer 1\n2. Answer 2 updated",
|
|
|
|
[M_POLL_START.name]: {
|
|
|
|
answers: [
|
|
|
|
{
|
|
|
|
id: expect.any(String),
|
|
|
|
[M_TEXT.name]: "Answer 1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: expect.any(String),
|
|
|
|
[M_TEXT.name]: "Answer 2 updated",
|
2022-02-17 10:13:05 +01:00
|
|
|
},
|
2022-12-12 12:24:14 +01:00
|
|
|
],
|
|
|
|
kind: M_POLL_KIND_UNDISCLOSED.name,
|
|
|
|
max_selections: 1,
|
|
|
|
question: {
|
|
|
|
body: "Poll Q updated",
|
|
|
|
format: undefined,
|
|
|
|
formatted_body: undefined,
|
|
|
|
msgtype: "m.text",
|
|
|
|
[M_TEXT.name]: "Poll Q updated",
|
2022-02-17 10:13:05 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-12-12 12:24:14 +01:00
|
|
|
"m.relates_to": {
|
|
|
|
event_id: previousEvent.getId(),
|
|
|
|
rel_type: "m.replace",
|
|
|
|
},
|
|
|
|
});
|
2022-02-17 10:13:05 +01:00
|
|
|
});
|
2022-12-21 22:29:11 +01:00
|
|
|
|
|
|
|
it("retains poll disclosure type when editing", () => {
|
|
|
|
const previousEvent: MatrixEvent = new MatrixEvent(
|
|
|
|
PollStartEvent.from("Poll Q", ["Answer 1", "Answer 2"], M_POLL_KIND_DISCLOSED).serialize(),
|
|
|
|
);
|
|
|
|
previousEvent.event.event_id = "$prevEventId";
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
const dialog = render(
|
2022-12-21 22:29:11 +01:00
|
|
|
<PollCreateDialog room={createRoom()} onFinished={jest.fn()} editingMxEvent={previousEvent} />,
|
|
|
|
);
|
|
|
|
|
|
|
|
changeValue(dialog, "Question or topic", "Poll Q updated");
|
2023-02-28 09:58:23 +01:00
|
|
|
fireEvent.click(dialog.container.querySelector("button")!);
|
2022-12-21 22:29:11 +01:00
|
|
|
|
|
|
|
const [, , eventType, sentEventContent] = mockClient.sendEvent.mock.calls[0];
|
|
|
|
expect(M_POLL_START.matches(eventType)).toBeTruthy();
|
|
|
|
// didnt change
|
2024-03-25 13:48:48 +01:00
|
|
|
expect((sentEventContent as ReplacementEvent<any>)["m.new_content"][M_POLL_START.name].kind).toEqual(
|
|
|
|
M_POLL_KIND_DISCLOSED.name,
|
|
|
|
);
|
2022-12-21 22:29:11 +01:00
|
|
|
});
|
2021-11-24 14:48:07 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
function createRoom(): Room {
|
2023-06-05 19:12:23 +02:00
|
|
|
return new Room("roomid", MatrixClientPeg.safeGet(), "@name:example.com", {});
|
2021-11-24 14:48:07 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
function changeValue(wrapper: RenderResult, labelText: string, value: string) {
|
|
|
|
fireEvent.change(wrapper.container.querySelector(`input[label="${labelText}"]`)!, {
|
|
|
|
target: { value: value },
|
|
|
|
});
|
2021-11-24 14:48:07 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
function changeKind(wrapper: RenderResult, value: string) {
|
|
|
|
fireEvent.change(wrapper.container.querySelector("select")!, { target: { value: value } });
|
2022-02-21 11:21:35 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
function expectSubmitToBeDisabled(wrapper: RenderResult, disabled: boolean) {
|
|
|
|
if (disabled) {
|
|
|
|
expect(wrapper.container.querySelector('button[type="submit"]')).toHaveAttribute("aria-disabled", "true");
|
|
|
|
} else {
|
|
|
|
expect(wrapper.container.querySelector('button[type="submit"]')).not.toHaveAttribute("aria-disabled", "true");
|
|
|
|
}
|
2021-11-24 14:48:07 +01:00
|
|
|
}
|