2021-12-10 20:17:26 +08: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.
|
|
|
|
*/
|
|
|
|
|
2022-03-12 00:03:33 +08:00
|
|
|
import { MatrixEvent, MatrixClient } from "matrix-js-sdk/src/matrix";
|
2022-01-18 01:06:30 +08:00
|
|
|
import { POLL_ANSWER, M_TEXT, M_POLL_KIND_DISCLOSED, M_POLL_START } from "matrix-events-sdk";
|
2021-12-10 20:17:26 +08:00
|
|
|
|
|
|
|
import { PollStartEventPreview } from "../../../../src/stores/room-list/previews/PollStartEventPreview";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
|
2022-03-12 00:03:33 +08:00
|
|
|
jest.spyOn(MatrixClientPeg, 'get').mockReturnValue({
|
2021-12-10 20:17:26 +08:00
|
|
|
getUserId: () => "@me:example.com",
|
2022-03-12 00:03:33 +08:00
|
|
|
} as unknown as MatrixClient);
|
2021-12-10 20:17:26 +08:00
|
|
|
|
|
|
|
describe("PollStartEventPreview", () => {
|
|
|
|
it("shows the question for a poll I created", async () => {
|
|
|
|
const pollStartEvent = newPollStartEvent("My Question", "@me:example.com");
|
|
|
|
const preview = new PollStartEventPreview();
|
|
|
|
expect(preview.getTextFor(pollStartEvent)).toBe("My Question");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows the sender and question for a poll created by someone else", async () => {
|
|
|
|
const pollStartEvent = newPollStartEvent("Your Question", "@yo:example.com");
|
|
|
|
const preview = new PollStartEventPreview();
|
|
|
|
expect(preview.getTextFor(pollStartEvent)).toBe("@yo:example.com: Your Question");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function newPollStartEvent(
|
|
|
|
question: string,
|
|
|
|
sender: string,
|
2022-01-18 01:06:30 +08:00
|
|
|
answers?: POLL_ANSWER[],
|
2021-12-10 20:17:26 +08:00
|
|
|
): MatrixEvent {
|
|
|
|
if (!answers) {
|
|
|
|
answers = [
|
2022-01-18 01:06:30 +08:00
|
|
|
{ "id": "socks", [M_TEXT.name]: "Socks" },
|
|
|
|
{ "id": "shoes", [M_TEXT.name]: "Shoes" },
|
2021-12-10 20:17:26 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MatrixEvent(
|
|
|
|
{
|
|
|
|
"event_id": "$mypoll",
|
|
|
|
"room_id": "#myroom:example.com",
|
|
|
|
"sender": sender,
|
2022-01-18 01:06:30 +08:00
|
|
|
"type": M_POLL_START.name,
|
2021-12-10 20:17:26 +08:00
|
|
|
"content": {
|
2022-01-18 01:06:30 +08:00
|
|
|
[M_POLL_START.name]: {
|
2021-12-10 20:17:26 +08:00
|
|
|
"question": {
|
2022-01-18 01:06:30 +08:00
|
|
|
[M_TEXT.name]: question,
|
2021-12-10 20:17:26 +08:00
|
|
|
},
|
2022-01-18 01:06:30 +08:00
|
|
|
"kind": M_POLL_KIND_DISCLOSED.name,
|
2021-12-10 20:17:26 +08:00
|
|
|
"answers": answers,
|
|
|
|
},
|
2022-01-18 01:06:30 +08:00
|
|
|
[M_TEXT.name]: `${question}: answers`,
|
2021-12-10 20:17:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|