Skip to content

Commit

Permalink
unit test paginating /notifications (#3283)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry authored Apr 16, 2023
1 parent 87398ac commit d40d5c8
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions spec/unit/matrix-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
MatrixScheduler,
Method,
Room,
EventTimelineSet,
} from "../../src";
import { supportsMatrixCall } from "../../src/webrtc/call";
import { makeBeaconEvent } from "../test-utils/beacon";
Expand Down Expand Up @@ -2743,4 +2744,120 @@ describe("MatrixClient", function () {
expect(mockSecretStorage.isStored).toHaveBeenCalledWith("m.megolm_backup.v1");
});
});

describe("paginateEventTimeline()", () => {
describe("notifications timeline", () => {
const unsafeNotification = {
actions: ["notify"],
room_id: "__proto__",
event: testUtils.mkMessage({
user: userId,
room: "!roomId:server.org",
msg: "I am nefarious",
}),
profile_tag: null,
read: true,
ts: 12345,
};

const goodNotification = {
actions: ["notify"],
room_id: "!roomId:server.org",
event: testUtils.mkMessage({
user: userId,
room: "!roomId:server.org",
msg: "I am nice",
}),
profile_tag: null,
read: true,
ts: 12345,
};

const highlightNotification = {
actions: ["notify", { set_tweak: "highlight" }],
room_id: "!roomId:server.org",
event: testUtils.mkMessage({
user: userId,
room: "!roomId:server.org",
msg: "I am highlighted",
}),
profile_tag: null,
read: true,
ts: 12345,
};

const setNotifsResponse = (notifications: any[] = []): void => {
const response: HttpLookup = {
method: "GET",
path: "/notifications",
data: { notifications: JSON.parse(JSON.stringify(notifications)) },
};
httpLookups = [response];
};

beforeEach(() => {
makeClient();

// this is how notif timeline is set up in react-sdk
const notifTimelineSet = new EventTimelineSet(undefined, {
timelineSupport: true,
pendingEvents: false,
});
notifTimelineSet.getLiveTimeline().setPaginationToken("", EventTimeline.BACKWARDS);
client.setNotifTimelineSet(notifTimelineSet);

setNotifsResponse();
});

it("should throw when trying to paginate forwards", async () => {
const timeline = client.getNotifTimelineSet()!.getLiveTimeline();
await expect(
async () => await client.paginateEventTimeline(timeline, { backwards: false }),
).rejects.toThrow("paginateNotifTimeline can only paginate backwards");
});

it("defaults limit to 30 events", async () => {
jest.spyOn(client.http, "authedRequest");
const timeline = client.getNotifTimelineSet()!.getLiveTimeline();
await client.paginateEventTimeline(timeline, { backwards: true });

expect(client.http.authedRequest).toHaveBeenCalledWith(Method.Get, "/notifications", {
limit: "30",
only: "highlight",
});
});

it("filters out unsafe notifications", async () => {
setNotifsResponse([unsafeNotification, goodNotification, highlightNotification]);

const timelineSet = client.getNotifTimelineSet()!;
const timeline = timelineSet.getLiveTimeline();
await client.paginateEventTimeline(timeline, { backwards: true });

// badNotification not added to timeline
const timelineEvents = timeline.getEvents();
expect(timelineEvents.length).toEqual(2);
});

it("sets push actions on events and add to timeline", async () => {
setNotifsResponse([goodNotification, highlightNotification]);

const timelineSet = client.getNotifTimelineSet()!;
const timeline = timelineSet.getLiveTimeline();
await client.paginateEventTimeline(timeline, { backwards: true });

const [highlightEvent, goodEvent] = timeline.getEvents();
expect(highlightEvent.getPushActions()).toEqual({
notify: true,
tweaks: {
highlight: true,
},
});
expect(goodEvent.getPushActions()).toEqual({
notify: true,
tweaks: {},
});
});
});
});
});

0 comments on commit d40d5c8

Please sign in to comment.