Skip to content

Commit 12a4d2a

Browse files
authored
Make more of the code conform to Strict TSC (#2756)
1 parent f70f6db commit 12a4d2a

33 files changed

+673
-452
lines changed

spec/integ/matrix-client-event-emitter.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ describe("MatrixClient events", function() {
201201
});
202202
client!.on(RoomEvent.Timeline, function(event, room) {
203203
timelineFireCount++;
204-
expect(room.roomId).toEqual("!erufh:bar");
204+
expect(room?.roomId).toEqual("!erufh:bar");
205205
});
206206
client!.on(RoomEvent.Name, function(room) {
207207
roomNameInvokeCount++;

spec/integ/matrix-client-event-timeline.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ describe("MatrixClient event timelines", function() {
368368
expect(tl!.getEvents().length).toEqual(4);
369369
for (let i = 0; i < 4; i++) {
370370
expect(tl!.getEvents()[i].event).toEqual(EVENTS[i]);
371-
expect(tl!.getEvents()[i]?.sender.name).toEqual(userName);
371+
expect(tl!.getEvents()[i]?.sender?.name).toEqual(userName);
372372
}
373373
expect(tl!.getPaginationToken(EventTimeline.BACKWARDS))
374374
.toEqual("start_token");
@@ -406,7 +406,7 @@ describe("MatrixClient event timelines", function() {
406406
}).then(function(tl) {
407407
expect(tl!.getEvents().length).toEqual(2);
408408
expect(tl!.getEvents()[1].event).toEqual(EVENTS[0]);
409-
expect(tl!.getEvents()[1]?.sender.name).toEqual(userName);
409+
expect(tl!.getEvents()[1]?.sender?.name).toEqual(userName);
410410
expect(tl!.getPaginationToken(EventTimeline.BACKWARDS))
411411
.toEqual("f_1_1");
412412
// expect(tl.getPaginationToken(EventTimeline.FORWARDS))
@@ -767,8 +767,8 @@ describe("MatrixClient event timelines", function() {
767767
httpBackend = testClient.httpBackend;
768768
await startClient(httpBackend, client);
769769

770-
const room = client.getRoom(roomId);
771-
const timelineSet = room.getTimelineSets()[0];
770+
const room = client.getRoom(roomId)!;
771+
const timelineSet = room.getTimelineSets()[0]!;
772772
await expect(client.getLatestTimeline(timelineSet)).rejects.toBeTruthy();
773773
});
774774

@@ -786,7 +786,7 @@ describe("MatrixClient event timelines", function() {
786786
httpBackend = testClient.httpBackend;
787787

788788
return startClient(httpBackend, client).then(() => {
789-
const room = client.getRoom(roomId);
789+
const room = client.getRoom(roomId)!;
790790
const timelineSet = room.getTimelineSets()[0];
791791
expect(client.getLatestTimeline(timelineSet)).rejects.toBeFalsy();
792792
});
@@ -849,7 +849,7 @@ describe("MatrixClient event timelines", function() {
849849
expect(tl!.getEvents().length).toEqual(4);
850850
for (let i = 0; i < 4; i++) {
851851
expect(tl!.getEvents()[i].event).toEqual(EVENTS[i]);
852-
expect(tl!.getEvents()[i]?.sender.name).toEqual(userName);
852+
expect(tl!.getEvents()[i]?.sender?.name).toEqual(userName);
853853
}
854854
expect(tl!.getPaginationToken(EventTimeline.BACKWARDS))
855855
.toEqual("start_token");
@@ -1129,8 +1129,8 @@ describe("MatrixClient event timelines", function() {
11291129
});
11301130

11311131
it("should allow fetching all threads", async function() {
1132-
const room = client.getRoom(roomId);
1133-
const timelineSets = await room?.createThreadsTimelineSets();
1132+
const room = client.getRoom(roomId)!;
1133+
const timelineSets = await room.createThreadsTimelineSets();
11341134
expect(timelineSets).not.toBeNull();
11351135
respondToThreads();
11361136
respondToThreads();
@@ -1185,14 +1185,14 @@ describe("MatrixClient event timelines", function() {
11851185
});
11861186

11871187
it("should allow fetching all threads", async function() {
1188-
const room = client.getRoom(roomId);
1188+
const room = client.getRoom(roomId)!;
11891189

11901190
respondToFilter();
11911191
respondToSync();
11921192
respondToFilter();
11931193
respondToSync();
11941194

1195-
const timelineSetsPromise = room?.createThreadsTimelineSets();
1195+
const timelineSetsPromise = room.createThreadsTimelineSets();
11961196
expect(timelineSetsPromise).not.toBeNull();
11971197
await flushHttp(timelineSetsPromise!);
11981198
respondToFilter();
@@ -1218,7 +1218,7 @@ describe("MatrixClient event timelines", function() {
12181218
const [allThreads] = timelineSets!;
12191219

12201220
respondToThreads().check((request) => {
1221-
expect(request.queryParams.filter).toEqual(JSON.stringify({
1221+
expect(request.queryParams?.filter).toEqual(JSON.stringify({
12221222
"lazy_load_members": true,
12231223
}));
12241224
});
@@ -1244,7 +1244,7 @@ describe("MatrixClient event timelines", function() {
12441244
state: [],
12451245
next_batch: null,
12461246
}).check((request) => {
1247-
expect(request.queryParams.from).toEqual(RANDOM_TOKEN);
1247+
expect(request.queryParams?.from).toEqual(RANDOM_TOKEN);
12481248
});
12491249

12501250
allThreads.getLiveTimeline().setPaginationToken(RANDOM_TOKEN, Direction.Backward);

spec/integ/matrix-client-room-timeline.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ describe("MatrixClient room timelines", function() {
160160
expect(room.timeline[1].status).toEqual(EventStatus.SENDING);
161161
// check member
162162
const member = room.timeline[1].sender;
163-
expect(member.userId).toEqual(userId);
164-
expect(member.name).toEqual(userName);
163+
expect(member?.userId).toEqual(userId);
164+
expect(member?.name).toEqual(userName);
165165

166166
httpBackend!.flush("/sync", 1).then(function() {
167167
done();
@@ -327,11 +327,11 @@ describe("MatrixClient room timelines", function() {
327327
client!.scrollback(room).then(function() {
328328
expect(room.timeline.length).toEqual(5);
329329
const joinMsg = room.timeline[0];
330-
expect(joinMsg.sender.name).toEqual("Old Alice");
330+
expect(joinMsg.sender?.name).toEqual("Old Alice");
331331
const oldMsg = room.timeline[1];
332-
expect(oldMsg.sender.name).toEqual("Old Alice");
332+
expect(oldMsg.sender?.name).toEqual("Old Alice");
333333
const newMsg = room.timeline[3];
334-
expect(newMsg.sender.name).toEqual(userName);
334+
expect(newMsg.sender?.name).toEqual(userName);
335335

336336
// still have a sync to flush
337337
httpBackend!.flush("/sync", 1).then(() => {
@@ -468,8 +468,8 @@ describe("MatrixClient room timelines", function() {
468468
]).then(function() {
469469
const preNameEvent = room.timeline[room.timeline.length - 3];
470470
const postNameEvent = room.timeline[room.timeline.length - 1];
471-
expect(preNameEvent.sender.name).toEqual(userName);
472-
expect(postNameEvent.sender.name).toEqual("New Name");
471+
expect(preNameEvent.sender?.name).toEqual(userName);
472+
expect(postNameEvent.sender?.name).toEqual("New Name");
473473
});
474474
});
475475
});

spec/integ/matrix-client-syncing.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,9 +1276,9 @@ describe("MatrixClient syncing", () => {
12761276
client!.on(RoomEvent.TimelineReset, (room) => {
12771277
resetCallCount++;
12781278

1279-
const tl = room.getLiveTimeline();
1280-
expect(tl.getEvents().length).toEqual(0);
1281-
const tok = tl.getPaginationToken(EventTimeline.BACKWARDS);
1279+
const tl = room?.getLiveTimeline();
1280+
expect(tl?.getEvents().length).toEqual(0);
1281+
const tok = tl?.getPaginationToken(EventTimeline.BACKWARDS);
12821282
expect(tok).toEqual("newerTok");
12831283
});
12841284

spec/unit/filter.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { UNREAD_THREAD_NOTIFICATIONS } from "../../src/@types/sync";
22
import { Filter, IFilterDefinition } from "../../src/filter";
3+
import { mkEvent } from "../test-utils/test-utils";
4+
import { EventType } from "../../src";
35

46
describe("Filter", function() {
57
const filterId = "f1lt3ring15g00d4ursoul";
@@ -57,4 +59,27 @@ describe("Filter", function() {
5759
});
5860
});
5961
});
62+
63+
describe("filterRoomTimeline", () => {
64+
it("should return input if no roomTimelineFilter and roomFilter", () => {
65+
const events = [mkEvent({ type: EventType.Sticker, content: {}, event: true })];
66+
expect(new Filter(undefined).filterRoomTimeline(events)).toStrictEqual(events);
67+
});
68+
69+
it("should filter using components when present", () => {
70+
const definition: IFilterDefinition = {
71+
room: {
72+
timeline: {
73+
types: [EventType.Sticker],
74+
},
75+
},
76+
};
77+
const filter = Filter.fromJson(userId, filterId, definition);
78+
const events = [
79+
mkEvent({ type: EventType.Sticker, content: {}, event: true }),
80+
mkEvent({ type: EventType.RoomMessage, content: {}, event: true }),
81+
];
82+
expect(filter.filterRoomTimeline(events)).toStrictEqual([events[0]]);
83+
});
84+
});
6085
});

spec/unit/interactive-auth.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ describe("InteractiveAuth", () => {
259259
const requestEmailToken = jest.fn();
260260

261261
const ia = new InteractiveAuth({
262-
authData: null,
263262
matrixClient: getFakeClient(),
264263
stateUpdated,
265264
doRequest,

spec/unit/models/event.spec.ts

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

17-
import { MatrixEvent } from "../../../src/models/event";
17+
import { MatrixEvent, MatrixEventEvent } from "../../../src/models/event";
18+
import { emitPromise } from "../../test-utils/test-utils";
19+
import { EventType } from "../../../src";
20+
import { Crypto } from "../../../src/crypto";
1821

1922
describe('MatrixEvent', () => {
2023
it('should create copies of itself', () => {
@@ -84,4 +87,36 @@ describe('MatrixEvent', () => {
8487
expect(ev.getWireContent().body).toBeUndefined();
8588
expect(ev.getWireContent().ciphertext).toBeUndefined();
8689
});
90+
91+
it("should abort decryption if fails with an error other than a DecryptionError", async () => {
92+
const ev = new MatrixEvent({
93+
type: EventType.RoomMessageEncrypted,
94+
content: {
95+
body: "Test",
96+
},
97+
event_id: "$event1:server",
98+
});
99+
await ev.attemptDecryption({
100+
decryptEvent: jest.fn().mockRejectedValue(new Error("Not a DecryptionError")),
101+
} as unknown as Crypto);
102+
expect(ev.isEncrypted()).toBeTruthy();
103+
expect(ev.isBeingDecrypted()).toBeFalsy();
104+
expect(ev.isDecryptionFailure()).toBeFalsy();
105+
});
106+
107+
describe("applyVisibilityEvent", () => {
108+
it("should emit VisibilityChange if a change was made", async () => {
109+
const ev = new MatrixEvent({
110+
type: "m.room.message",
111+
content: {
112+
body: "Test",
113+
},
114+
event_id: "$event1:server",
115+
});
116+
117+
const prom = emitPromise(ev, MatrixEventEvent.VisibilityChange);
118+
ev.applyVisibilityEvent({ visible: false, eventId: ev.getId(), reason: null });
119+
await prom;
120+
});
121+
});
87122
});

0 commit comments

Comments
 (0)