Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 73e8387

Browse files
author
Kerry
authored
test functional EventUtils (#8386)
* extract poll event test util Signed-off-by: Kerry Archibald <[email protected]> * test isContentActionable Signed-off-by: Kerry Archibald <[email protected]> * test canEditContent Signed-off-by: Kerry Archibald <[email protected]> * test functional eventutils Signed-off-by: Kerry Archibald <[email protected]> * copyrights Signed-off-by: Kerry Archibald <[email protected]>
1 parent c70816d commit 73e8387

File tree

5 files changed

+413
-37
lines changed

5 files changed

+413
-37
lines changed

src/utils/EventUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function canEditContent(mxEvent: MatrixEvent): boolean {
8282
M_POLL_START.matches(mxEvent.getType()) ||
8383
(
8484
(msgtype === MsgType.Text || msgtype === MsgType.Emote) &&
85-
body &&
85+
!!body &&
8686
typeof body === 'string'
8787
)
8888
);

test/stores/room-list/previews/PollStartEventPreview-test.ts

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,27 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { MatrixEvent, MatrixClient } from "matrix-js-sdk/src/matrix";
18-
import { POLL_ANSWER, M_TEXT, M_POLL_KIND_DISCLOSED, M_POLL_START } from "matrix-events-sdk";
17+
import { MatrixClient } from "matrix-js-sdk/src/matrix";
1918

2019
import { PollStartEventPreview } from "../../../../src/stores/room-list/previews/PollStartEventPreview";
2120
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
21+
import { makePollStartEvent } from "../../../test-utils";
2222

2323
jest.spyOn(MatrixClientPeg, 'get').mockReturnValue({
2424
getUserId: () => "@me:example.com",
2525
} as unknown as MatrixClient);
2626

2727
describe("PollStartEventPreview", () => {
2828
it("shows the question for a poll I created", async () => {
29-
const pollStartEvent = newPollStartEvent("My Question", "@me:example.com");
29+
const pollStartEvent = makePollStartEvent("My Question", "@me:example.com");
3030
const preview = new PollStartEventPreview();
3131
expect(preview.getTextFor(pollStartEvent)).toBe("My Question");
3232
});
3333

3434
it("shows the sender and question for a poll created by someone else", async () => {
35-
const pollStartEvent = newPollStartEvent("Your Question", "@yo:example.com");
35+
const pollStartEvent = makePollStartEvent("Your Question", "@yo:example.com");
3636
const preview = new PollStartEventPreview();
3737
expect(preview.getTextFor(pollStartEvent)).toBe("@yo:example.com: Your Question");
3838
});
3939
});
4040

41-
function newPollStartEvent(
42-
question: string,
43-
sender: string,
44-
answers?: POLL_ANSWER[],
45-
): MatrixEvent {
46-
if (!answers) {
47-
answers = [
48-
{ "id": "socks", [M_TEXT.name]: "Socks" },
49-
{ "id": "shoes", [M_TEXT.name]: "Shoes" },
50-
];
51-
}
52-
53-
return new MatrixEvent(
54-
{
55-
"event_id": "$mypoll",
56-
"room_id": "#myroom:example.com",
57-
"sender": sender,
58-
"type": M_POLL_START.name,
59-
"content": {
60-
[M_POLL_START.name]: {
61-
"question": {
62-
[M_TEXT.name]: question,
63-
},
64-
"kind": M_POLL_KIND_DISCLOSED.name,
65-
"answers": answers,
66-
},
67-
[M_TEXT.name]: `${question}: answers`,
68-
},
69-
},
70-
);
71-
}
72-

test/test-utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './beacon';
22
export * from './client';
33
export * from './location';
44
export * from './platform';
5+
export * from './poll';
56
export * from './room';
67
export * from './test-utils';
78
export * from './video';

test/test-utils/poll.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2022 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+
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
18+
import { M_TEXT, M_POLL_START, POLL_ANSWER, M_POLL_KIND_DISCLOSED } from "matrix-events-sdk";
19+
20+
export const makePollStartEvent = (
21+
question: string,
22+
sender: string,
23+
answers?: POLL_ANSWER[],
24+
): MatrixEvent => {
25+
if (!answers) {
26+
answers = [
27+
{ "id": "socks", [M_TEXT.name]: "Socks" },
28+
{ "id": "shoes", [M_TEXT.name]: "Shoes" },
29+
];
30+
}
31+
32+
return new MatrixEvent(
33+
{
34+
"event_id": "$mypoll",
35+
"room_id": "#myroom:example.com",
36+
"sender": sender,
37+
"type": M_POLL_START.name,
38+
"content": {
39+
[M_POLL_START.name]: {
40+
"question": {
41+
[M_TEXT.name]: question,
42+
},
43+
"kind": M_POLL_KIND_DISCLOSED.name,
44+
"answers": answers,
45+
},
46+
[M_TEXT.name]: `${question}: answers`,
47+
},
48+
},
49+
);
50+
};

0 commit comments

Comments
 (0)