|
| 1 | +/* |
| 2 | +Copyright 2022 - 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/// <reference types="cypress" /> |
| 18 | + |
| 19 | +import { HomeserverInstance } from "../../plugins/utils/homeserver"; |
| 20 | +import { MatrixClient } from "../../global"; |
| 21 | + |
| 22 | +describe("Poll history", () => { |
| 23 | + let homeserver: HomeserverInstance; |
| 24 | + |
| 25 | + type CreatePollOptions = { |
| 26 | + title: string; |
| 27 | + options: { |
| 28 | + "id": string; |
| 29 | + "org.matrix.msc1767.text": string; |
| 30 | + }[]; |
| 31 | + }; |
| 32 | + const createPoll = async ({ title, options }: CreatePollOptions, roomId, client: MatrixClient) => { |
| 33 | + return await client.sendEvent(roomId, "org.matrix.msc3381.poll.start", { |
| 34 | + "org.matrix.msc3381.poll.start": { |
| 35 | + question: { |
| 36 | + "org.matrix.msc1767.text": title, |
| 37 | + "body": title, |
| 38 | + "msgtype": "m.text", |
| 39 | + }, |
| 40 | + kind: "org.matrix.msc3381.poll.disclosed", |
| 41 | + max_selections: 1, |
| 42 | + answers: options, |
| 43 | + }, |
| 44 | + "org.matrix.msc1767.text": "poll fallback text", |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + const botVoteForOption = async ( |
| 49 | + bot: MatrixClient, |
| 50 | + roomId: string, |
| 51 | + pollId: string, |
| 52 | + optionId: string, |
| 53 | + ): Promise<void> => { |
| 54 | + // We can't use the js-sdk types for this stuff directly, so manually construct the event. |
| 55 | + await bot.sendEvent(roomId, "org.matrix.msc3381.poll.response", { |
| 56 | + "m.relates_to": { |
| 57 | + rel_type: "m.reference", |
| 58 | + event_id: pollId, |
| 59 | + }, |
| 60 | + "org.matrix.msc3381.poll.response": { |
| 61 | + answers: [optionId], |
| 62 | + }, |
| 63 | + }); |
| 64 | + }; |
| 65 | + |
| 66 | + const endPoll = async (bot: MatrixClient, roomId: string, pollId: string): Promise<void> => { |
| 67 | + // We can't use the js-sdk types for this stuff directly, so manually construct the event. |
| 68 | + await bot.sendEvent(roomId, "org.matrix.msc3381.poll.end", { |
| 69 | + "m.relates_to": { |
| 70 | + rel_type: "m.reference", |
| 71 | + event_id: pollId, |
| 72 | + }, |
| 73 | + "org.matrix.msc1767.text": "The poll has ended", |
| 74 | + }); |
| 75 | + }; |
| 76 | + |
| 77 | + function openPollHistory(): void { |
| 78 | + cy.get('.mx_HeaderButtons [aria-label="Room info"]').click(); |
| 79 | + cy.get(".mx_RoomSummaryCard").within(() => { |
| 80 | + cy.contains("Polls history").click(); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + beforeEach(() => { |
| 85 | + cy.window().then((win) => { |
| 86 | + win.localStorage.setItem("mx_lhs_size", "0"); // Collapse left panel for these tests |
| 87 | + }); |
| 88 | + cy.startHomeserver("default").then((data) => { |
| 89 | + homeserver = data; |
| 90 | + |
| 91 | + cy.enableLabsFeature("feature_poll_history"); |
| 92 | + |
| 93 | + cy.initTestUser(homeserver, "Tom"); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + afterEach(() => { |
| 98 | + cy.stopHomeserver(homeserver); |
| 99 | + }); |
| 100 | + |
| 101 | + it("Should display active and past polls", () => { |
| 102 | + let bot: MatrixClient; |
| 103 | + cy.getBot(homeserver, { displayName: "BotBob" }).then((_bot) => { |
| 104 | + bot = _bot; |
| 105 | + }); |
| 106 | + |
| 107 | + const pollParams1 = { |
| 108 | + title: "Does the polls feature work?", |
| 109 | + options: ["Yes", "No", "Maybe"].map((option) => ({ |
| 110 | + "id": option, |
| 111 | + "org.matrix.msc1767.text": option, |
| 112 | + })), |
| 113 | + }; |
| 114 | + |
| 115 | + const pollParams2 = { |
| 116 | + title: "Which way", |
| 117 | + options: ["Left", "Right"].map((option) => ({ |
| 118 | + "id": option, |
| 119 | + "org.matrix.msc1767.text": option, |
| 120 | + })), |
| 121 | + }; |
| 122 | + |
| 123 | + cy.createRoom({}).as("roomId"); |
| 124 | + |
| 125 | + cy.get<string>("@roomId").then((roomId) => { |
| 126 | + cy.inviteUser(roomId, bot.getUserId()); |
| 127 | + cy.visit("/#/room/" + roomId); |
| 128 | + // wait until Bob joined |
| 129 | + cy.contains(".mx_TextualEvent", "BotBob joined the room").should("exist"); |
| 130 | + }); |
| 131 | + |
| 132 | + // active poll |
| 133 | + cy.get<string>("@roomId") |
| 134 | + .then(async (roomId) => { |
| 135 | + const { event_id: pollId } = await createPoll(pollParams1, roomId, bot); |
| 136 | + await botVoteForOption(bot, roomId, pollId, pollParams1.options[1].id); |
| 137 | + return pollId; |
| 138 | + }) |
| 139 | + .as("pollId1"); |
| 140 | + |
| 141 | + // ended poll |
| 142 | + cy.get<string>("@roomId") |
| 143 | + .then(async (roomId) => { |
| 144 | + const { event_id: pollId } = await createPoll(pollParams2, roomId, bot); |
| 145 | + await botVoteForOption(bot, roomId, pollId, pollParams1.options[1].id); |
| 146 | + await endPoll(bot, roomId, pollId); |
| 147 | + return pollId; |
| 148 | + }) |
| 149 | + .as("pollId2"); |
| 150 | + |
| 151 | + openPollHistory(); |
| 152 | + |
| 153 | + // these polls are also in the timeline |
| 154 | + // focus on the poll history dialog |
| 155 | + cy.get(".mx_Dialog").within(() => { |
| 156 | + // active poll is in active polls list |
| 157 | + // open poll detail |
| 158 | + cy.contains(pollParams1.title).click(); |
| 159 | + |
| 160 | + // vote in the poll |
| 161 | + cy.contains("Yes").click(); |
| 162 | + cy.get('[data-testid="totalVotes"]').should("have.text", "Based on 2 votes"); |
| 163 | + |
| 164 | + // navigate back to list |
| 165 | + cy.contains("Active polls").click(); |
| 166 | + |
| 167 | + // go to past polls list |
| 168 | + cy.contains("Past polls").click(); |
| 169 | + |
| 170 | + cy.contains(pollParams2.title).should("exist"); |
| 171 | + }); |
| 172 | + |
| 173 | + // end poll1 while dialog is open |
| 174 | + cy.all([cy.get<string>("@roomId"), cy.get<string>("@pollId1")]).then(async ([roomId, pollId]) => { |
| 175 | + return endPoll(bot, roomId, pollId); |
| 176 | + }); |
| 177 | + |
| 178 | + cy.get(".mx_Dialog").within(() => { |
| 179 | + // both ended polls are in past polls list |
| 180 | + cy.contains(pollParams2.title).should("exist"); |
| 181 | + cy.contains(pollParams1.title).should("exist"); |
| 182 | + |
| 183 | + cy.contains("Active polls").click(); |
| 184 | + |
| 185 | + // no more active polls |
| 186 | + cy.contains("There are no active polls in this room").should("exist"); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments